1

メッセージを暗号化および復号化するために、Java で 2 つの関数を作成しました。パラメータとして AES/CBC/PKCS7Padding を選択しました。暗号化中、関数は正しいパディングを追加できません (PKCS7Padding のように、メッセージをパディングするために 0 を追加するべきではありません)。そのため、復号化中にメッセージの末尾にある 0 を削除できません。コードを使用すると、私の問題はより明確になります

    public static  byte[] encryptStringToData(String message, String key){
        // transform the message from string to bytes
        byte[] array_to_encrypt, bkey;
        try {
            array_to_encrypt = message.getBytes("UTF8");
            bkey = key.getBytes("UTF8");
        } catch (UnsupportedEncodingException e1) {
            array_to_encrypt = null;
            bkey = null;
            return null;
        }

        bkey = Arrays.copyOf(bkey, 32);
        BlockCipher engine = new AESEngine();
        engine.init(true, new KeyParameter(bkey, 0, 32));
        PKCS7Padding pad = new PKCS7Padding() ;

        BufferedBlockCipher c = new PaddedBufferedBlockCipher(new CBCBlockCipher(engine),pad); 

        c.init(true, new ParametersWithIV(new KeyParameter(bkey), new byte[16]));

        byte[] encrypted_array = new byte[c.getOutputSize(array_to_encrypt.length)];

        int outputLen = c.processBytes(array_to_encrypt, 0, array_to_encrypt.length, encrypted_array, 0);
        try
        {
            c.doFinal(encrypted_array, outputLen);
        }
        catch (CryptoException ce)
        {
            System.err.println(ce);
            System.exit(1);
        }
        return encrypted_array; 
    }


public static  String decryptDataToString(byte[] message, String key){
        Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
        String decrypted =""; 
        try { 
            byte[] keyBytes = key.getBytes("UTF8"); 

            BlockCipher engine = new AESEngine();
            BufferedBlockCipher c = new PaddedBufferedBlockCipher(new CBCBlockCipher(engine));
            keyBytes = Arrays.copyOf(keyBytes, 32); // use only first 256 bit

            c.init(false, new ParametersWithIV(new KeyParameter(keyBytes), new byte[16]));
            byte[] cipherText = new byte[c.getOutputSize(message.length)];

            int outputLen = c.processBytes(message, 0, message.length, cipherText, 0);
            c.doFinal(cipherText, outputLen);
            decrypted = new String(cipherText,"UTF8");
        }
        catch (CryptoException ce)
        {
            System.err.println(ce);
            System.exit(1);
        } catch (UnsupportedEncodingException e) { 
            e.printStackTrace();
        }

        return decrypted;

    }

結果 (16 進出力)

クレア メッセージ:3132333435 サイズ: 5 暗号化されたメッセージ:561dd9f43ec183fe351776a46276991c サイズ: 16 復号化されたメッセージ:31323333435000000000000000000000 サイズ: 16

4

1 に答える 1

1

BouncyCastle の使用を開始するにはどうすればよいですか? を確認してください。 余分なバイトを削除するコードは次のとおりです。

if (outputLength == output.length) {
    return output;
} else {
    byte[] truncatedOutput = new byte[outputLength];
    System.arraycopy(
            output, 0,
            truncatedOutput, 0,
            outputLength
        );
    return truncatedOutput;
}

そしてあなたのコードでは次のように翻訳されます:

outputLen += c.doFinal(cipherText, outputLen);
if (outputLen == cipherText.length) {
    decrypted = new String(cipherText,"UTF8");
} else {
    byte[] truncatedOutput = new byte[outputLen];
    System.arraycopy(
            cipherText, 0,
            truncatedOutput, 0,
            outputLen 
        );
    decrypted = new String(truncatedOutput,"UTF8");
}
于 2012-05-22T07:41:15.867 に答える