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;
}
}
答案 0 :(得分:1)
使用以下方法加密后进行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;
否则它被加密两次而不是加密+解密。