【问题标题】:why am i seeing inconsistencies between two blowfish implementations?为什么我看到两个河豚实现之间的不一致?
【发布时间】:2012-08-26 01:26:24
【问题描述】:

我首先使用 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 );
    }

这是给我与上述 C++ 代码相同结果的 openSSL 代码

#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);

【问题讨论】:

  • oBlowFishEncrypt方法长什么样子?
  • 更新了看看,本质上它使用的是标准的 openssl 调用,类似于这里的链接:stackoverflow.com/questions/5225996/…
  • 抱歉贴错行,现已更正,请查看
  • 不是我选择使用河豚,只是事物的本质:)
  • CBlowfish 不是 OpenSSL。

标签: java c++ encryption blowfish


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-04-29
    • 1970-01-01
    • 1970-01-01
    • 2012-06-14
    • 2016-02-02
    • 1970-01-01
    • 2012-01-03
    • 1970-01-01
    相关资源
    最近更新 更多