doFinal 方法 of cipher 对象返回 java.lang.ArrayIndexOutOfBoundsExc


public class SymmetricCipherTest {
    private static final String DEFAULT_ENCRYPTION_ALGORITHM = "PBEWithMD5AndTripleDES";
    public final String ENCODE_INDICATOR_START = "ENC(";
    public final String ENCODE_INDICATOR_END = ")";
    public final String APP_ENCRYPTION_KEY_FILE = "application/.encryption.key";
    public static final int INTERATION = 15;
    private static final byte[] SALT = { (byte) 0xd7, (byte) 0x73, (byte) 0x21, (byte) 0x8c, (byte) 0x7e, (byte) 0xc8, (byte) 0xee,
            (byte) 0x99 };
//  private static SymmetricCipherTest instance = initApplicaitonKey();
    private static Base64 base64 = new Base64();
    private static Cipher encrypter;
    private static Cipher decrypter;
//  private final Base64 base64 = new Base64();
    private static final int KEYLENGTH = 256;
    public final String ERROR_KEY_GENERATION = "Encryption key generation failed. Please verify the logs.";
    public static void main(String[] args) throws InvalidKeySpecException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException {
        String applicationKey="abcdefghijklmnopqrstu";
        String password="HellowWorld";
        try{
            SecretKeyFactory kf = SecretKeyFactory.getInstance(DEFAULT_ENCRYPTION_ALGORITHM);
            PBEKeySpec keySpec = new PBEKeySpec(applicationKey.toCharArray());
            SecretKey key = kf.generateSecret(keySpec);
            Cipher ciph = Cipher.getInstance(DEFAULT_ENCRYPTION_ALGORITHM);
            PBEParameterSpec params = new PBEParameterSpec(SALT, INTERATION);
            ciph.init(Cipher.ENCRYPT_MODE, key, params);
            encrypter=ciph;
            String encriptedString=new String(base64.encode(encrypter.doFinal(password.getBytes())));
            System.out.println(encriptedString);
            Cipher ciph1 = Cipher.getInstance(DEFAULT_ENCRYPTION_ALGORITHM);
            ciph1.init(Cipher.DECRYPT_MODE, key, params);
            decrypter=ciph;
            String decryiptedString=new String(base64.decode(decrypter.doFinal(encriptedString.getBytes())));
            System.out.println(decryiptedString);
        }catch(NoSuchAlgorithmException e){
            System.out.println("No such algorithm");
        }
    }
}

是我想跑的班级。但是当我运行这个时,我正在把异常作为波纹管。

EEb9pXx45M1WV16D4b9EmA==
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -64
    at org.apache.commons.codec.binary.Base64.isBase64(Base64.java:137)
    at org.apache.commons.codec.binary.Base64.discardNonBase64(Base64.java:478)
    at org.apache.commons.codec.binary.Base64.decodeBase64(Base64.java:374)
    at org.apache.commons.codec.binary.Base64.decode(Base64.java:220)
    at com.vxl.appanalytix.webapp.listener.SymmetricCipherTest.main(SymmetricCipherTest.java:54)

SymmetricCipherTest 中的第 54 行是"String decryiptedString=new String(base64.decode(decrypter.doFinal(encriptedString.getBytes(((((;"。我不明白为什么我得到这个例外,因为我对这个话题很陌生。

更新:

public class SymmetricCipherTest {
    private static final String DEFAULT_ENCRYPTION_ALGORITHM = "PBEWithMD5AndTripleDES";
    public final String ENCODE_INDICATOR_START = "ENC(";
    public final String ENCODE_INDICATOR_END = ")";
    public final String APP_ENCRYPTION_KEY_FILE = "application/.encryption.key";
    public static final int INTERATION = 15;
    private static final byte[] SALT = { (byte) 0xd7, (byte) 0x73, (byte) 0x21, (byte) 0x8c, (byte) 0x7e, (byte) 0xc8, (byte) 0xee,
            (byte) 0x99 };
//  private static SymmetricCipherTest instance = initApplicaitonKey();
    private static Base64 base64 = new Base64();
    private static Cipher encrypter;
    private static Cipher decrypter;
//  private final Base64 base64 = new Base64();
    public final String ERROR_KEY_GENERATION = "Encryption key generation failed. Please verify the logs.";
    public static void main(String[] args) throws InvalidKeySpecException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException, DecoderException, NoSuchAlgorithmException {
        String applicationKey="abcdefghijklmnopqrstu";
        String password="HellowWorld";
            encrypter=getCipherObject(applicationKey);
            String encriptedString=new String(base64.encode(encrypter.doFinal(password.getBytes())));
            System.out.println(encriptedString);
            decrypter=getCipherObject(applicationKey);
             String decryiptedString=new String(decrypter.doFinal(base64.decode(encriptedString.getBytes())));
            System.out.println(decryiptedString);
    }
    public static Cipher getCipherObject(String applicationKey) throws NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException{
        SecretKeyFactory kf = SecretKeyFactory.getInstance(DEFAULT_ENCRYPTION_ALGORITHM);
        PBEKeySpec keySpec = new PBEKeySpec(applicationKey.toCharArray());
        SecretKey key = kf.generateSecret(keySpec);
        Cipher ciph = Cipher.getInstance(DEFAULT_ENCRYPTION_ALGORITHM);
        PBEParameterSpec params = new PBEParameterSpec(SALT, INTERATION);
        ciph.init(Cipher.ENCRYPT_MODE, key, params);
        return ciph;
    }
}
加密

后进行 base64 编码:

String encriptedString=new String(base64.encode(encrypter.doFinal(password.getBytes())));

所以你需要在解密之前做 base64 解码.. 目前你尝试解密 base64 编码的字符串。尝试类似操作:

  String decryiptedString=new String(decrypter.doFinal(base64.decode(encriptedString.getBytes()));

此外,该行

decrypter=ciph;

需要

decrypter=ciph1;

否则,它会加密两次,而不是加密 + 解密。

相关内容

最新更新