4

私はこの投稿の多くを、ソフトウェアに aes 256 暗号化を実装する目的で実行しましたが、問題なく動作します

ここで重要な点は、上記のリンクで説明されている実装全体がAESEngine クラスを使用していることです。クラス コードとjavadoc リファレンスを見ると、AESEngine は 256 ビット ブロック暗号ではなく 128 ビットです。

コードとドキュメントを検索しても、192 ビットまたは 256 ビットの実装が見つかりませんでした。彼らはどこにいますか?

完全を期すために、これは私の実際の暗号化クラスのコアです。

    private void init(String passphrase) {
        try {
            String algorithm = "PBEWithSHA256And256BitAES-CBC-BC"; 

            encryptCipher = createCipher();
            decryptCipher = createCipher();    

            randomGenerator = new RandomGenerator();

            PBEKeySpec keySpec = new PBEKeySpec(passphrase.toCharArray(), KEY_SALT, ITERATIONS);    

            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(algorithm);
            key = keyFactory.generateSecret(keySpec);    

        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException("NoSuchAlgorithmException occured while trying to generate the crypto key. This error should never occur, check the application code", e);
        } catch (InvalidKeySpecException e) {
            throw new RuntimeException("InvalidKeySpecException occured while trying to generate the crypto key. This error should never occur, check the application code", e);
        }
    }    

    private BufferedBlockCipher createCipher() {
        return new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESFastEngine()), new PKCS7Padding());
    }    

    public byte[] encrypt(byte[] data) {
        if (data == null)
            throw new NullPointerException("Cannot encrypt null data");    

        byte[] iv = randomGenerator.generateRandom(IV_SIZE);    

        byte[] encrypted;

        synchronized (encryptCipher) {
            encrypted = runCipher(encryptCipher, true, data, iv);
        }    

        return DataUtil.append(iv, encrypted);
    }    

    public byte[] decrypt(byte[] data) {
        if (data == null)
            throw new NullPointerException("Cannot decrypt null data");    

        byte[] iv = DataUtil.extract(data, 0, IV_SIZE);
        byte[] cipherText = DataUtil.extract(data, IV_SIZE, data.length - IV_SIZE);

        byte[] decrypted;    

        synchronized (decryptCipher) {
            decrypted = runCipher(decryptCipher, false, cipherText, iv);
        }

        return decrypted;
    }

    private byte[] runCipher(BufferedBlockCipher cipher, boolean forEncryption, byte[] data, byte[] iv) {
        String operation = forEncryption ? "encrypt" : "decrypt";

        try {
            KeyParameter keyParam = new KeyParameter(key.getEncoded());
            ParametersWithIV cipherParams = new ParametersWithIV(keyParam, iv);

            cipher.init(forEncryption, cipherParams);

            byte[] result = new byte[cipher.getOutputSize(data.length)];
            int len = cipher.processBytes(data, 0, data.length, result, 0);
            len += cipher.doFinal(result, len);

            //Remove padding se estiver decriptografando
            if(!forEncryption)
                result = DataUtil.extract(result, 0, len);

            return result;
        } catch (DataLengthException e) {
            throw new RuntimeException("DataLengthException occured while trying to " + operation + " data with length " + data.length + ". This error should never occur, check the application code", e);
        } catch (IllegalStateException e) {
            throw new RuntimeException("IllegalStateException occured while trying to " + operation + " data with length " + data.length + ". This error should never occur, check the application code", e);
        } catch (InvalidCipherTextException e) {
            throw new IllegalArgumentException("InvalidCipherTextException occured while trying to " + operation + " data with length " + data.length, e);
        }
    }
4

2 に答える 2

5

256 ビットのブロック サイズで AES のような暗号化を行いたい場合は、次を使用する必要があります。

http://www.docjar.org/docs/api/org/bouncycastle/crypto/engines/RijndaelEngine.html

しかし、それはおそらくあなたが望むものではありません。AES-256 の 256 はキー サイズとほぼ同じです。この鍵サイズは、基礎となる 128 ビット AES ブロック暗号によって使用されます。AES は、Rijndael の標準化された 128 ビット ブロック バージョンです。

于 2012-09-24T22:40:56.240 に答える
0

AES は、 WikipediaNISTの 3 つのキー サイズをサポートしています。

おそらく、128ビットに固定されているブロックサイズを参照しています。

また、コードを調べてみましたが、128、192、および 256 の異なるキー サイズを想定して記述されています。コードからコピー - 貼り付け - 「AES は 128 ビットの固定ブロック サイズとキー サイズ 128/192/256 ビットを指定しました。このコードは、それらが唯一の可能な値であると仮定して書かれています。」

于 2015-02-22T14:39:14.237 に答える