我在Java中使用DESede encryptation和我要讓使用OpenSSL的API在C++相同encryptation。
Java代碼:
String secretKey = "abcdefghijklmnopqrstuvwx";
byte[] bytes = secretKey.getBytes("UTF-8");
SecretKey key = new SecretKeySpec(bytes, "DESede");
Cipher ecipher = Cipher.getInstance("DESede");
ecipher.init(Cipher.ENCRYPT_MODE, key);
String input = "holahola1";
byte[] utf8 = input.getBytes("UTF8");
byte[] enc = ecipher.doFinal(utf8); // Encrypt
輸出:[94,-45,64,-105,99,-55,99,-42,95,122,72,117,-119,95%, -43,40]
C++代碼:
unsigned char intext[256], outtext[256];
memset(intext, 0, sizeof(intext));
memset(outtext, 0, sizeof(outtext));
char *output = (char *)outtext;
DES_key_schedule keyschedc1;
DES_key_schedule keyschedc2;
DES_key_schedule keyschedc3;
DES_cblock keyc1;
DES_cblock keyc2;
DES_cblock keyc3;
DES_string_to_key("abcdefgh", &keyc1);
DES_string_to_key("ijklmnop", &keyc2);
DES_string_to_key("qrstuvwx", &keyc3);
DES_set_key((DES_cblock *)keyc1, &keyschedc1);
DES_set_key((DES_cblock *)keyc2, &keyschedc2);
DES_set_key((DES_cblock *)keyc3, &keyschedc3);
strcpy((char *)intext, "holahola1");
for (int i=0; i<16; i += 8)
{
DES_ecb3_encrypt((DES_cblock *)(intext + i),
(DES_cblock *)(outtext + i),
&keyschedc1, &keyschedc2,
&keyschedc3,DES_ENCRYPT);
}
輸出:[30 55 9 -58 -18 -65 -66 -32 123 72 30 110 120 69 101 -81]
什麼錯誤?有人能幫我嗎?謝謝!。
只是好奇,爲什麼會有人使用DES加密? – TJD 2012-01-27 18:45:34
我正在使用新的Jabber客戶端,我需要保持與舊客戶端的兼容性。 – Chris 2012-01-27 18:51:15
您在C++代碼中使用單個DES。 – 2012-01-27 23:40:03