C# 尝试解码输入字符串以使用AES进行解密
我正在与一家供应商合作,该供应商向我们提供AES加密消息示例,我们有一个共享密钥,我正在测试加密/说明 他们提供的解密消息如下所示。通常我会期望字符串,但这是他们提供的:C# 尝试解码输入字符串以使用AES进行解密,c#,encryption,aes,C#,Encryption,Aes,我正在与一家供应商合作,该供应商向我们提供AES加密消息示例,我们有一个共享密钥,我正在测试加密/说明 他们提供的解密消息如下所示。通常我会期望字符串,但这是他们提供的: d4 ee 84 87 f4 e2 0d c2 ef 07 e4 2c 9f b2 48 9e 我有点困惑,那是什么符号 我的加密/解密方法使用字符串作为输入,字符串作为输出,但在内部我使用字节数组: byte[] plainBytes = Encoding.Unicode.GetBytes(plainText); byt
d4 ee 84 87 f4 e2 0d c2 ef 07 e4 2c 9f b2 48 9e
byte[] plainBytes = Encoding.Unicode.GetBytes(plainText);
byte[] theBytes = new byte[15];
string hexstring = "d4ee8487f4e20dc2ef07e42c9fb2489e";
for (int i = 0; i < 15; i++)
{
string thisByte = hexstring.Substring(i * 2, 2);
int intValue = Convert.ToInt16(thisByte, 16);
theBytes[i] = BitConverter.GetBytes(intValue)[0];
}
如何将上述输入转换为字节数组?它是字节的十六进制表示形式
string bytes = "d4 ee 84 87 f4 e2 0d c2 ef 07 e4 2c 9f b2 48 9e";
var buf = bytes.Split(' ').Select(s => Convert.ToByte(s, 16)).ToArray();
如果十六进制字符串不包含任何空格
string bytes = "d4ee8487f4e20dc2ef07e42c9fb2489e";
var buf = Regex.Matches(bytes, ".{2}").Cast<Match>()
.Select(x => Convert.ToByte(x.Value, 16)).ToArray();
顺便说一句:
您应该注意,如果您正在使用Encoding.Unicode.GetString
,则无法将每个字节数组安全地转换为字符串
var buf = new byte[] { 0xff, 0xff, 0xff };
string str = Encoding.Unicode.GetString(buf);
var buf2 = Encoding.Unicode.GetBytes(str);
buf2
将不等于buf
。要将字节数组转换为字符串,请使用convert.ToBase64String
或位转换器.ToString
它是字节的十六进制表示形式
string bytes = "d4 ee 84 87 f4 e2 0d c2 ef 07 e4 2c 9f b2 48 9e";
var buf = bytes.Split(' ').Select(s => Convert.ToByte(s, 16)).ToArray();
如果十六进制字符串不包含任何空格
string bytes = "d4ee8487f4e20dc2ef07e42c9fb2489e";
var buf = Regex.Matches(bytes, ".{2}").Cast<Match>()
.Select(x => Convert.ToByte(x.Value, 16)).ToArray();
顺便说一句:
您应该注意,如果您正在使用Encoding.Unicode.GetString
,则无法将每个字节数组安全地转换为字符串
var buf = new byte[] { 0xff, 0xff, 0xff };
string str = Encoding.Unicode.GetString(buf);
var buf2 = Encoding.Unicode.GetBytes(str);
buf2
将不等于buf
。要将字节数组转换为字符串,请使用convert.ToBase64String
或BitConverter.ToString
此代码运行后,字节将包含您的字节数组:
byte[] plainBytes = Encoding.Unicode.GetBytes(plainText);
byte[] theBytes = new byte[15];
string hexstring = "d4ee8487f4e20dc2ef07e42c9fb2489e";
for (int i = 0; i < 15; i++)
{
string thisByte = hexstring.Substring(i * 2, 2);
int intValue = Convert.ToInt16(thisByte, 16);
theBytes[i] = BitConverter.GetBytes(intValue)[0];
}
byte[]字节=新字节[15];
字符串hexstring=“d4ee8487f4e20dc2ef07e42c9fb2489e”;
对于(int i=0;i<15;i++)
{
string thisByte=hexstring.Substring(i*2,2);
int intValue=Convert.ToInt16(这个字节,16);
字节[i]=位转换器.GetBytes(intValue)[0];
}
此代码运行后,字节将包含您的字节数组:
byte[] plainBytes = Encoding.Unicode.GetBytes(plainText);
byte[] theBytes = new byte[15];
string hexstring = "d4ee8487f4e20dc2ef07e42c9fb2489e";
for (int i = 0; i < 15; i++)
{
string thisByte = hexstring.Substring(i * 2, 2);
int intValue = Convert.ToInt16(thisByte, 16);
theBytes[i] = BitConverter.GetBytes(intValue)[0];
}
byte[]字节=新字节[15];
字符串hexstring=“d4ee8487f4e20dc2ef07e42c9fb2489e”;
对于(int i=0;i<15;i++)
{
string thisByte=hexstring.Substring(i*2,2);
int intValue=Convert.ToInt16(这个字节,16);
字节[i]=位转换器.GetBytes(intValue)[0];
}
将十六进制转换为字符串会导致任何问题吗?双方必须使用相同的编码/解码正确吗?将十六进制转换为字符串会导致任何问题吗?
它已经是字符串了。我刚刚将其转换为字节数组,将十六进制转换为字符串会导致任何问题吗?双方必须使用相同的编码/解码正确吗?将十六进制转换为字符串会导致任何问题吗?
它已经是字符串了。我刚把它转换成字节数组,有人告诉我它是ECB。在2015年,你以前从未见过十六进制表示法?请花20分钟读一读,手工做一个简单的例子,然后你将终生记住它。@ja72谢谢,这是我的建议helpful@ArtjomB. 这是我的错误,我更改了加密消息,丢失了一个字节。现在是16,我更新了。好的,你有钥匙吗?它是什么格式的?我听说它是ECB。在2015年,你以前从未见过十六进制的表示法?请花20分钟读一读,手工做一个简单的例子,然后你将终生记住它。@ja72谢谢,这是我的建议helpful@ArtjomB. 这是我的错误,我更改了加密消息,丢失了一个字节。现在是16,我更新了。好的,你有钥匙吗?它是什么格式的?