gpt4 book ai didi

java - 填充JAVA加密blockSizeException

转载 作者:行者123 更新时间:2023-12-01 17:46:43 24 4
gpt4 key购买 nike

我认为这个问题已经被问了一百万遍了,但是我找不到一个可以解决我的问题的答案。

我继承了一段代码。它可以很好地与字符串XXXX100一起使用,但不能与XXXX1000一起使用。它可以很好地加密(它生成一个没有错误的字符串),但不能解密更长的字符串。它抛出:

javax.crypto.IllegalBlockSizeException:使用填充密码解密时,输入长度必须是8的倍数

我需要对要解密的输入文本做其他事情,在某个地方设置参数...还是其他?

package projectlocal.encryptionutilities;

import java.io.UnsupportedEncodingException;
import java.security.GeneralSecurityException;
import java.util.Base64;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.PBEParameterSpec;

public class SimpleCrypt {

/** encryption master password */
private static final char[] AUTHENTIC = "8zrahH9HiDeJM60W0gmZ".toCharArray();
/** encryption algorithm */
//private static final String ALGORITHM = "PBEWithMD5AndDES";
private static final String ALGORITHM = "PBEWithMD5AndDES";
/** charset */
private static final String UTF8_CHARSET = "UTF-8";
/** encryption seed */

private static final byte[] SALT = {
(byte) 0xde, (byte) 0x33, (byte) 0x10, (byte) 0x12,
(byte) 0xde, (byte) 0x33, (byte) 0x10, (byte) 0x12,
};

/** create private constructor to prevent instantiation */
private SimpleCrypt() {/* ignore */}

public static String encrypt(final String property) throws GeneralSecurityException, UnsupportedEncodingException {
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ALGORITHM);
SecretKey key = keyFactory.generateSecret(new PBEKeySpec(AUTHENTIC));
Cipher pbeCipher = Cipher.getInstance(ALGORITHM);
pbeCipher.init(Cipher.ENCRYPT_MODE, key, new PBEParameterSpec(SALT, 20));
return Base64.getMimeEncoder().encodeToString(pbeCipher.doFinal(property.getBytes()));
}

public static String decrypt(final String property) throws UnsupportedEncodingException, GeneralSecurityException {
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ALGORITHM);
SecretKey key = keyFactory.generateSecret(new PBEKeySpec(AUTHENTIC));
Cipher pbeCipher = Cipher.getInstance(ALGORITHM);
pbeCipher.init(Cipher.DECRYPT_MODE, key, new PBEParameterSpec(SALT, 20));
return new String(pbeCipher.doFinal(Base64.getMimeDecoder().decode(property)), UTF8_CHARSET);
}

}

最佳答案

在其他地方。上面的代码工作正常。另一种例程是截断较长的字符串。

感谢您帮助我改善此问题。

关于java - 填充JAVA加密blockSizeException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60855722/

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