2

これは、BlowFish 暗号化/復号化で直面している問題です。

以下のコードは、BlowFish 暗号化/復号化のテストに使用されます。

// Code below omits comments for Brevity

import javax.crypto.*;
import javax.crypto.spec.SecretKeySpec;

import java.math.BigInteger;

public class JBoss {

    public static void main(String[] args) throws Exception {

        if ((args.length != 2)
                || !(args[0].equals("-e") | args[0].equals("-d"))) {
            System.out
                    .println("Usage:\n\tjava JBoss <-e|-d> <encrypted_password>");
            return;
        }

        String mode = args[0];

        byte[] kbytes = "jaas is the way".getBytes();
        SecretKeySpec key = new SecretKeySpec(kbytes, "Blowfish");
        Cipher cipher = Cipher.getInstance("Blowfish");

        String out = null;

        if (mode.equals("-e")) {
            String secret = args[1];
            cipher.init(Cipher.ENCRYPT_MODE, key);
            byte[] encoding = cipher.doFinal(secret.getBytes());
            out = new BigInteger(encoding).toString(16);
        } else {
            BigInteger secret = new BigInteger(args[1], 16);
            cipher.init(Cipher.DECRYPT_MODE, key);
            byte[] encoding = cipher.doFinal(secret.toByteArray());
            out = new String(encoding);
        }
        System.out.println(out);
    }
}

今、文字列を暗号化しようとすると

u7mzqw2

私は次のように値を取得します

-7ccb7ff0c2858a

解読しようとすると

-7ccb7ff0c2858a

以下のようなエラーが表示されます。

    java JBoss -d -7ccb7ff0c2858a
Exception in thread "main" javax.crypto.IllegalBlockSizeException: Input length must be multiple of 8 when decrypting with padded cipher
        at com.sun.crypto.provider.SunJCE_f.b(DashoA13*..)
        at com.sun.crypto.provider.SunJCE_f.b(DashoA13*..)
        at com.sun.crypto.provider.BlowfishCipher.engineDoFinal(DashoA13*..)
        at javax.crypto.Cipher.doFinal(DashoA13*..)
        at JBoss.main(JBoss.java:41)

コード全体はこちら

以下のものが機能するため、私が間違っていなければ、7文字の長さの元の値または/ 8 = 0以外の暗号化された値とは何の関係もありません

Java JBoss -e qwerty

-40e961f375c2eee6

Java JBoss -d -40e961f375c2eee6

クォーター

私は何が欠けていますか??

4

3 に答える 3

1

これが私の観察です:私はコードを少し変更して、個々のバイトと対応する16進値を持ついくつかのsopsを追加しました。

入力: asdfda

16::10

60 :: 3c

105 :: 69

57 :: 39

-60 ::-3c

110 :: 6e

19 :: 13

-52 ::-34

エンコードされた値:103c6939c46e13cc

ご覧のとおり、左側の項目はバイトで、右側には基数16の値を持つ個々のbigintegerがあり、下部にはエンコードされた値があります。大きなパターンマッチングが表示される場合があります。-tiveの値を除いて。-60のように値-3cに対応しますが、1バイトの圧縮と同様に、値はc4になります(yourslefを参照)。

ここで、u7mzqw2として暗号化する値でテストしました。何が起こるか見てみましょう。

入力: u7mzqw2

-1 ::-1

-125 ::-7d

52 :: 34

-128 ::-80

15 :: f

61 :: 3d

122 :: 7a

118 :: 76

エンコードされた値:-7ccb7ff0c2858a

今、あなたはパターンマッチングを見ますか、今あなたは見ません、なぜですか?ほら、16進数が-1でなければならない-1をよく見てみましょう。??? 、いいえ、0XFFですが、0xFFをバイトで表すことはできますか?いいえ、できません。バイトと-1を自分で読んでください

更新:私が困惑したのは、これらのエンコーディングがどのように評価されたかでしたか?まだ探しています、これを特定するのを手伝ってください

于 2013-01-23T11:26:22.023 に答える
0

これを試して、

public static void main(String[] args) throws Exception {
     // generate key
     KeyGenerator keyGen = KeyGenerator.getInstance("Blowfish");
     SecretKey secretKey = keyGen.generateKey();
     // get Cipher and init it for encryption
     Cipher cipher = Cipher.getInstance("Blowfish/CBC/PKCS5Padding");
     cipher.init(Cipher.ENCRYPT_MODE, secretKey);
     String data="u7mzqw2";

     // encrypt data
     byte[] cipherText = cipher.doFinal(data.getBytes());
     // get the initialization vector from the cipher
     byte[] ivBytes = cipher.getIV();
     IvParameterSpec iv = new IvParameterSpec(ivBytes);

     byte[] keyBytes = secretKey.getEncoded();
     // create a SecretKeySpec from key material
     SecretKeySpec secretKeySpec = new SecretKeySpec(keyBytes, "Blowfish");
     // get Cipher and init it for encryption
     cipher = Cipher.getInstance("Blowfish/CBC/PKCS5Padding");
     cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, iv);
     byte[] plainText = cipher.doFinal(cipherText);
     System.out.println(new String(plainText));
}
于 2013-01-23T15:54:50.203 に答える
-1

secret.toByteArray() の行に続くコードを見ると、問題の原因である長さ 7 バイトの配列が与えられています。

byte[] encoding = cipher.doFinal(secret.toByteArray()); //During decoding logic

問題はBlowfishCipher、 CipherCore の実装にあるようです。

public BlowfishCipher()
{
        core = new CipherCore(new BlowfishCrypt(),
                              BlowfishConstants.BLOWFISH_BLOCK_SIZE);
}

http://www.docjar.com/html/api/com/sun/crypto/provider/BlowfishConstants.java.html

BlowfishConstants.BLOWFISH_BLOCK_SIZE = 8; を使用しています。// バイト数

http://hg.openjdk.java.net/jdk6/jdk6/jdk/raw-file/2d585507a41b/src/share/classes/com/sun/crypto/provider/CipherCore.java

if ((paddingLen > 0) && (paddingLen != blockSize) &&
            (padding != null) && decrypting) {
            throw new IllegalBlockSizeException
                ("Input length must be multiple of " + blockSize +
                 "when decrypting with padded cipher");
        }
于 2013-01-23T09:26:59.257 に答える