gpt4 book ai didi

java - cipher 对象的 doFinal 方法返回 java.lang.ArrayIndexOutOfBoundsException

转载 作者:行者123 更新时间:2023-12-01 14:10:04 25 4
gpt4 key购买 nike

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");
}

}
}

我想运行我的类(class)吗?但是当我运行这个时,我得到了如下所示的异常。

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;

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

关于java - cipher 对象的 doFinal 方法返回 java.lang.ArrayIndexOutOfBoundsException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18589000/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com