3DES 加密与第三方不匹配 API
3DES encryption not matching third party API
我的 C# 3DES 加密与我正在使用的第三方不匹配 API。我的代码有什么问题吗?
static void Main(string[] args)
{
String sharedSec = "654A7EA2C9914A0B972937F4EA45FED3";
byte[] byteArraySharedSec = Convert.FromBase64String(sharedSec);
TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
tdes.KeySize = 192;
tdes.Key = byteArraySharedSec;
tdes.Mode = CipherMode.ECB;
tdes.Padding = PaddingMode.PKCS7;
ICryptoTransform ict = tdes.CreateEncryptor();
ICryptoTransform dct = tdes.CreateDecryptor();
Console.WriteLine("SharedSec: {0}", sharedSec);
Console.WriteLine("byteArraySharedSec: {0}\n", ToReadableByteArray(byteArraySharedSec));
long unixTimestamp = 1299481200;
byte[] unixTimestampByte = BitConverter.GetBytes(unixTimestamp);
Console.WriteLine("Timestamp: {0}, length: {1} ", ToReadableByteArray(unixTimestampByte), unixTimestampByte.Length);
byte[] result = ict.TransformFinalBlock(unixTimestampByte, 0, unixTimestampByte.Length);
Console.WriteLine("After 3DES encrypting: {0}, length {1}\n\n ", ToReadableByteArray(result), result.Length);
}
static public string ToReadableByteArray(byte[] bytes)
{
return string.Join(",", bytes);
}
输出(您可以看到 byteArraySharedSec 是正确的,但加密不是):
SharedSec: 654A7EA2C9914A0B972937F4EA45FED3
byteArraySharedSec: 235,158,0,236,64,54,11,223,117,224,13,1,247,189,189,223,177,120,16,14,57,20,64,247
Timestamp: 112,130,116,77,0,0,0,0, length: 8
After 3DES encrypting:
213,60,183,244,171,116,202,205,233,17,226,8,70,9,111,43, length 16
API 医生给出了这个例子:
The 3DES encryption uses:
192 bit key
ECB Mode
PKCS7 Padding
Example SHARED_SECRET: 654A7EA2C9914A0B972937F4EA45FED3
Convert SHARED_SECRET to byte array by Base64 decoding. This is the 3DES key: { 235, 158, 0, 236, 64, 54, 11, 223, 117, 224, 13, 1, 247, 189, 189, 223, 177, 120, 16, 14, 57, 20, 64, 247}
Example timestamp (7AM 7th March 2011 GMT): 1299481200
3DES encrypt (ECB mode, PKCS7 Padding) the timestamp using the 3DES key : 128 bit (16 byte) result for this example { 82, 191, 213, 179, 179, 73, 1, 218, 247, 68, 254, 199, 19, 159, 1, 138}
您加密的值与示例中提供的值不同。
将 timestamp
视为字符串,并使用 UTF8
编码来获取其字节表示应该会得到相同的结果:
...
byte[] unixTimestampByte = Encoding.UTF8.GetBytes(unixTimestamp.ToString());
...
我的 C# 3DES 加密与我正在使用的第三方不匹配 API。我的代码有什么问题吗?
static void Main(string[] args)
{
String sharedSec = "654A7EA2C9914A0B972937F4EA45FED3";
byte[] byteArraySharedSec = Convert.FromBase64String(sharedSec);
TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
tdes.KeySize = 192;
tdes.Key = byteArraySharedSec;
tdes.Mode = CipherMode.ECB;
tdes.Padding = PaddingMode.PKCS7;
ICryptoTransform ict = tdes.CreateEncryptor();
ICryptoTransform dct = tdes.CreateDecryptor();
Console.WriteLine("SharedSec: {0}", sharedSec);
Console.WriteLine("byteArraySharedSec: {0}\n", ToReadableByteArray(byteArraySharedSec));
long unixTimestamp = 1299481200;
byte[] unixTimestampByte = BitConverter.GetBytes(unixTimestamp);
Console.WriteLine("Timestamp: {0}, length: {1} ", ToReadableByteArray(unixTimestampByte), unixTimestampByte.Length);
byte[] result = ict.TransformFinalBlock(unixTimestampByte, 0, unixTimestampByte.Length);
Console.WriteLine("After 3DES encrypting: {0}, length {1}\n\n ", ToReadableByteArray(result), result.Length);
}
static public string ToReadableByteArray(byte[] bytes)
{
return string.Join(",", bytes);
}
输出(您可以看到 byteArraySharedSec 是正确的,但加密不是):
SharedSec: 654A7EA2C9914A0B972937F4EA45FED3
byteArraySharedSec: 235,158,0,236,64,54,11,223,117,224,13,1,247,189,189,223,177,120,16,14,57,20,64,247
Timestamp: 112,130,116,77,0,0,0,0, length: 8
After 3DES encrypting: 213,60,183,244,171,116,202,205,233,17,226,8,70,9,111,43, length 16
API 医生给出了这个例子:
The 3DES encryption uses:
192 bit key
ECB Mode
PKCS7 Padding
Example SHARED_SECRET: 654A7EA2C9914A0B972937F4EA45FED3
Convert SHARED_SECRET to byte array by Base64 decoding. This is the 3DES key: { 235, 158, 0, 236, 64, 54, 11, 223, 117, 224, 13, 1, 247, 189, 189, 223, 177, 120, 16, 14, 57, 20, 64, 247}
Example timestamp (7AM 7th March 2011 GMT): 1299481200
3DES encrypt (ECB mode, PKCS7 Padding) the timestamp using the 3DES key : 128 bit (16 byte) result for this example { 82, 191, 213, 179, 179, 73, 1, 218, 247, 68, 254, 199, 19, 159, 1, 138}
您加密的值与示例中提供的值不同。
将 timestamp
视为字符串,并使用 UTF8
编码来获取其字节表示应该会得到相同的结果:
...
byte[] unixTimestampByte = Encoding.UTF8.GetBytes(unixTimestamp.ToString());
...