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 decryptedString=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;
}
}