0

我首先使用 javax.crypto.spec.SecretKeySpec 使用密钥“rubicon”加密“06.93308”,然后使用 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);
4

1 回答 1

1

“rubicon”不是 16 字节长。你必须SIZE相应地进行调整。

于 2012-08-31T11:31:45.633 回答