我正在使用密钥“rubicon”加密“06.93308”,首先使用javax.crypto.spec.SecretKeySpec,另一个使用openSSL在C ++中加密。但是两者都给了我不同的加密值。 java版本给我十六进制A834BDD6C3478B8C
,而OpenSSL给我D06D7CB756744903
这是非常不同的。目的是获得与java等价物相同的结果。关于我做错了什么想法?
java代码如下:
char[] password = new char[] { 'r', 'u', 'b', 'i', 'c', 'o', 'n' };
byte[] raw = encrypt(password,"06.93308" );
private static byte[] encrypt(char[] password, String plaintext) throws Exception {
byte[] bytes = new byte[password.length];
for (int i = 0; i < password.length; ++i) {
bytes[i] = (byte) password[i];
}
SecretKeySpec skeySpec = new SecretKeySpec(bytes, "Blowfish");
Cipher cipher = Cipher.getInstance("Blowfish/ECB/NoPadding");
cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
byte[] encrypted = cipher.doFinal(plaintext.getBytes());
return encrypted;
}
c ++方面如下:
CBlowFish oBlowFish((byte *)"rubicon", 8);
char encryptedPrice[17] = "\0\0\0\0\0\0\0\0";
char myBidPrice[] = "06.93308";
encrypt(myBidPrice,encryptedPrice);
void encrypt(char bidPrice[],char encryptedPrice[])
{
oBlowFish.Encrypt((unsigned char*)bidPrice, (unsigned char*)encryptedPrice,8 );
}
这是openSSL代码,它给出了与上述C ++代码相同的结果
#define SIZE 16
unsigned char *out = (unsigned char *)calloc(SIZE+1, sizeof(char));
BF_KEY *key = (BF_KEY *)calloc(1, sizeof(BF_KEY));
BF_set_key(key, SIZE, (const unsigned char*)"rubicon" );
BF_ecb_encrypt(in, out, key, BF_ENCRYPT);
printf("%s\n",out);
答案 0 :(得分:1)
“rubicon”不是16个字节长。您必须相应地调整SIZE
。