开发者

Issue with AES/CBC/ISO10126Padding mode in bouncycastle java

When I run this code the "encrypted string:" printed on the console is different each time I run. i.e. the encryption and decryption mode is AES/CBC/ISO10126Padding However the encrypted string does decrypt successfully. When I run the same code in 'AES' mode the "encrypted string:" printed on the console is the same each time. Please can you tell me why in the case of AES/CBC/ISO10126Padding the output is diff each time i run it.

import java.io.FileOutputStream;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;



//import org.apache.log4j.Logger;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.util.encoders.Base64;


public class AES_Encrypt_Decrypt_Bouncy_Castle {

    public static SecretKeySpec getKey() {
        try {
            String keyFromfile = "ZoyNMZzsOYh7BxwcgJseBji95hmVTlgKe/9KFY44Jzg=";

            byte[] keyBytes = Base64.decode(keyFromfile.getBytes());
            byte[] finalKeyBytes = new byte[32];

            for (int i = 0; i < 32; i++) {
                finalKeyBytes[i] = keyBytes[i];
            }
            System.out.println("keyBytes="+keyBytes.length);
            System.out.println("Key is "+ keyBytes);
            for(int j=32;j<keyBytes.length;j++){
                System.out.println("keyBytes="+keyBytes[j]);
            }           
            SecretKeySpec skeySpec = new SecretKeySpec(finalKeyBytes, "AES");
            return skeySpec;
        } catch (Exception e) {
            return null;
        }

    }


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

        SecretKeySpec skeySpec = getKey();
        byte [] iv = Base64.decode("AWNCK2F/9llI0Rs+dQB36Q==");
        IvParameterSpec initializationVector = new IvParameterSpec(iv);
        Cipher cip开发者_开发问答her = Cipher.getInstance("AES/CBC/ISO10126Padding", new BouncyCastleProvider());  
        cipher.init(Cipher.ENCRYPT_MODE, skeySpec, initializationVector);
        String message = "Brian12345678";
        byte[] encrypted = cipher.doFinal(message.getBytes());
        System.out.println("Decoded plain text = "+new String (encrypted));
        byte[] encodedEncryptedText = Base64.encode(encrypted);
        System.out.println("encrypted string: " + new String (encodedEncryptedText));
        Cipher cipher2 = Cipher.getInstance("AES/CBC/ISO10126Padding", new BouncyCastleProvider());
        cipher2.init(Cipher.DECRYPT_MODE, skeySpec,initializationVector);
        byte[] original = cipher2.doFinal(Base64.decode(encodedEncryptedText));
        String originalString = new String(original);
        System.out.println("Original string: " + originalString);

    }

}


From the wikipedia padding page:

ISO 10126 (withdrawn, 2007 [2] [3] ) specifies that the padding should be done at the end of that last block with random bytes, and the padding boundary should be specified by the last byte.

That could explain why the encrypted data is different, but can decrypt successfully.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜