我在Java / Scala中使用MCRYPT重写了一个C程序,但我得到了Java和C的不同解码字符串。
我做错了什么?
char * decode(char *string) {
MCRYPT mcrypt_blowfish;
mcrypt_blowfish = mcrypt_module_open("blowfish-compat", NULL, "ecb", NULL);
unsigned char hardKey[] = { 0xEF, 0x3A, 0xB2, 0x9C, 0xD1, 0x9F, 0x0C, 0xAC, 0x57, 0x59, 0xC7, 0xAB, 0xD1, 0x2C, 0xC9, 0x2B, 0xA3, 0xFE, 0x0A, 0xFE, 0xBF, 0x96, 0x0D, 0x63, 0xFE, 0xBD, 0x0F, 0x45};
mcrypt_generic_init(mcrypt_blowfish, hardKey, 28, NULL);
mdecrypt_generic(mcrypt_blowfish, string, 512);
mcrypt_generic_deinit(mcrypt_blowfish);
mcrypt_module_close(mcrypt_blowfish);
string[512] = 0;
return string;
}
Scala代码
def decode (string: String ): String = {
val keyBytes: Array[Char] = Array[Char](0xEF, 0x3A, 0xB2, 0x9C, 0xD1, 0x9F, 0x0C, 0xAC, 0x57, 0x59, 0xC7, 0xAB, 0xD1, 0x2C, 0xC9, 0x2B,0xA3, 0xFE, 0x0A, 0xFE, 0xBF, 0x96, 0x0D, 0x63,0xFE, 0xBD, 0x0F, 0x45)
val cipher: Cipher = Cipher.getInstance("Blowfish/ECB/NoPadding")
cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(new String (keyBytes).getBytes, "Blowfish"))
new String(cipher.doFinal(string.getBytes.reverse))
}