我已经在整个网络上寻找这个,但找不到明确的答案:
我有一些使用 DESCryptoServiceProvider (Mode = CRC, Padding = none) 的 C# 代码,我正在使用 openssl 库使用 C 来做同样的事情。
到目前为止在 C#
public static byte[] DESEncrypt(byte[] Source, byte[] Key)
{
byte[] DESKey = new byte[8];
Array.Copy(Key, 0, DESKey, 0, 8);
DisplayByteArray("DESKey", DESKey);
System.Security.Cryptography.DESCryptoServiceProvider DES = new System.Security.Cryptography.DESCryptoServiceProvider();
DES.Mode = System.Security.Cryptography.CipherMode.CBC;
byte[] IV = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
DES.IV = IV;
DES.Padding = System.Security.Cryptography.PaddingMode.None;
DES.Key = DESKey;
System.IO.MemoryStream ms = new System.IO.MemoryStream();
System.Security.Cryptography.CryptoStream cs = new System.Security.Cryptography.CryptoStream(ms, DES.CreateEncryptor(), System.Security.Cryptography.CryptoStreamMode.Write);
cs.Write(Source, 0, Source.Length);
byte[] outdata = ms.ToArray();
cs.Close();
ms.Close();
return outdata;
}
在 C 中
int DesEncrypt(BYTE *pOriginal, BYTE *oEncrypted, int nDataLength, BYTE *pKey)
{
DES_cblock ivec = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
DES_cblock key[8];
memcpy(key,&pKey[0], 8);
memset(oEncrypted, 0, sizeof(oEncrypted));
printf("\nDESkey: ");DisplayByteArray((BYTE *)&key[0], 8);
DES_key_schedule ks;
memcpy((DES_cblock *)key, &ks, 8);
if (DES_set_key_checked((DES_cblock *)key, &ks))
printf("\nChecked OKAY");
else
printf("\nCheck FAILED");
DES_cbc_encrypt(pOriginal, oEncrypted, nDataLength, &ks, &ivec, DES_ENCRYPT);
}
我究竟做错了什么?