I was trying to create a self encrypt way and decrypt way by converting it on varbinary, but the problem is that when I tried to decrpyt it, the result is way more different than the encrypted string.
var name = "JUAN KARLOS"; //Random name
var a = name.ToArray();
byte[] b = new byte[a.Length];
byte[] c = System.Text.Encoding.UTF8.GetBytes(a);
c.CopyTo(b, 0);
string d = BitConverter.ToString(b);
d = d.Replace("-", "");
d = @"0x" d;
This is the way I'm doing with encrypting the result is:
0x4A55414E204B41524C4F53
and this is how I do my decryption:
byte[] e = System.Text.Encoding.UTF8.GetBytes(d);
string f = Convert.ToBase64String(e);
string g = String.Format(f);
and the result is:
MHg0QTU1NDE0RTIwNEI0MTUyNEM0RjUz
Did it change the value because I replaced the "-" to ""?
CodePudding user response:
You can use this code to get the result:
public static byte[] StringToByteArray(string hex)
{
return Enumerable.Range(2, hex.Length - 2)
.Where(x => x % 2 == 0)
.Select(x => Convert.ToByte(hex.Substring(x, 2), 16))
.ToArray();
}
Main
var name = "JUAN KARLOS"; //Random name
var a = name.ToArray();
byte[] b = new byte[a.Length];
byte[] c = System.Text.Encoding.UTF8.GetBytes(a);
c.CopyTo(b, 0);
string d = BitConverter.ToString(b);
d = d.Replace("-", "");
d = @"0x" d;
//d="0x4A55414E204B41524C4F53"
byte[] t = StringToByteArray(d);
string tt = System.Text.Encoding.UTF8.GetString(t);
The result will be:
JUAN KARLOS