javax.crypto.BadPaddingException:给定的最终块在解密时未正确填充错误
我做加密和解密的数据如下,但得到的错误javax.crypto.BadPaddingException:给定的最终块在解密时未正确填充错误,java,encryption,Java,Encryption,我做加密和解密的数据如下,但得到的错误 protected Cipher aes_Gen_with_Key(byte[] key) { Cipher cipher=null; try { byte[] key_hash = (key).toString().getBytes("UTF-8"); key_hash = Arrays.copyOf(key_hash, 32); // use only first
protected Cipher aes_Gen_with_Key(byte[] key)
{
Cipher cipher=null;
try
{
byte[] key_hash = (key).toString().getBytes("UTF-8");
key_hash = Arrays.copyOf(key_hash, 32); // use only first 256 bit
SecretKeySpec secretKeySpec = new SecretKeySpec(key_hash, "AES");
cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);
} catch (Exception e) {
System.out.println("Error Occured");
}
return cipher;
}
protected Cipher aes_Dec_with_Key(byte[] key,byte[] iv)
{
Cipher cipher=null;
try
{
byte[] key_hash = (key).toString().getBytes("UTF-8");
key_hash = Arrays.copyOf(key_hash, 32); // use only first 256 bit
SecretKeySpec secretKeySpec = new SecretKeySpec(key_hash, "AES");
cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE,secretKeySpec,new IvParameterSpec(iv));
}
catch (Exception e) {
System.out.println(e);
}
return cipher;
}
通过以上两个函数,我得到了加密和解密所用的密码,但得到了javax.crypto.BadPaddingException:给定的最后一个块没有作为错误正确填充。解密字节数组的长度为752,解密时的IV长度为16字节。有人能建议吗
这里有几个更相关的代码块。
对无效使用java命名约定表示歉意
// Key Class
import java.io.Serializable;
@SuppressWarnings("serial")
public class Key implements Serializable{
byte[] gsmodp_hash=null;
byte[] iv_pass=null;
byte[] Nonce_Enc=null;
byte[] iv_non=null;
public Key() {
}
public Key(byte[] gsmodp_hash,byte[] iv_pass,byte[] Nonce_Enc,byte[] iv_non) {
this.gsmodp_hash=gsmodp_hash;
this.iv_pass=iv_pass;
this.Nonce_Enc=Nonce_Enc;
this.iv_non=iv_non;
}
}
// Client side code
JSONObject auth_step_obj=new JSONObject();
try {
BigInteger gsmodp=get_modu_frm_server(receivePacket);
BigInteger R2=get_R2_frm_server(receivePacket);
BigInteger N2=get_N2_frm_server(receivePacket);
N2=crypto.dec_NonceG(N2);
BigInteger a=crypto.get_RandLong();
BigInteger gamodp= crypto.dh_GenerationG(a, crypto.g, crypto.p);
BigInteger key=crypto.dh_GenerationG(a, gsmodp, crypto.p);
// Got hash of g^abmodp
byte[] dhkey=crypto.sha256G(key.toString());
key=null;
//Mixing passwords
SecretKey secretkey=(SecretKey) userCredentials.get("password");
byte[] mixed_hash=crypto.passwordMixerG(R2, secretkey.getEncoded());
//Working Fine Till now
// Getting Cipher for encrypting gsmodp using password and nonce
Cipher cipher_password=crypto.aes_Gen_with_Key(mixed_hash);
Cipher cipher_key=crypto.aes_Gen_with_Key(dhkey);
// Generating quantities for JSON Object
byte[] gsmodp_hash=cipher_password.doFinal(gamodp.toString().getBytes());
byte[] gsmodp_hash_iv=cipher_password.getIV();
byte[] nonce_enc=cipher_key.doFinal(N2.toString().getBytes());
byte[] nonce_enc_iv=cipher_key.getIV();
Key authetication_parameters=new Key(gsmodp_hash,gsmodp_hash_iv,nonce_enc,nonce_enc_iv);
auth_step_obj.put("obj",authetication_parameters);
} catch (Exception e) {
// TODO Auto-generated catch block
System.out.println("Hi:sec_DH_Step");
}
// This sends JSONObject to calling method which generates UDP packet and sends to server
return auth_step_obj;
}
// Server side code
// Once packet received on server following happens
ds.receive(receivePacket);
SecretKey userKey=cryptoObj.get_from_KeyStoreG(user_name);
// Adding R2 and userKey byte by byte
byte[] mixed_hash=cryptoObj.passwordMixerG(R2, userKey.getEncoded());
JSONObject authentication_nonce=new JSONObject();
authentication_nonce=cryptoObj.readRecievedPacket(receivePacket);
Key obj=(Key)authentication_nonce.get("obj");
Cipher cipher=cryptoObj.aes_Dec_with_Key(mixed_hash,obj.iv_pass);
// I am getting error on do final
System.out.println(new String(cipher.doFinal(obj.gsmodp_hash)));
您提供的代码工作得很好。错误必须存在于您未与我们共享的代码中(即实际使用
密码
对象的代码)
我编写了下面的代码来测试您的问题代码:
public static void main(String[] args) throws Exception {
Random random = new Random();
byte[] key = new byte[32];
random.nextBytes(key);
byte[] plaintext = new byte[100];
random.nextBytes(plaintext);
Cipher enc = aes_Gen_with_Key(key);
byte[] ciphertext = enc.doFinal(plaintext);
byte[] iv = enc.getIV();
Cipher dec = aes_Dec_with_Key(key, iv);
byte[] recoveredPlaintext = dec.doFinal(ciphertext);
System.out.println(Arrays.equals(plaintext, recoveredPlaintext));
}
注意,我将您的两个方法设置为静态。您也应该这样做,因为它们不使用任何实例变量。我建议查看Java命名约定。.正在不断编辑代码以查找错误。我会小心的。谢谢,谢谢。我在我的问题下添加了几个代码块。我试图在客户端代码本身上运行解密,并且工作正常,所以我认为错误在于服务器代码中的数据被从密钥类对象复制到字节数组以进行egtting解密。请让我知道,如果需要更多的清晰度。谢谢