javax.crypto.BadPaddingException:未知块类型 [英] javax.crypto.BadPaddingException: unknown block type
问题描述
private static SecureRandom random = new SecureRandom();
static {
Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
}
protected synchronized void generateKeys()throws InvalidKeyException,IllegalBlockSizeException,
BadPaddingException,NoSuchAlgorithmException,NoSuchProviderException,
NoSuchPaddingException {
KeyPairGenerator generator = KeyPairGenerator.getInstance(RSA,BC);
generator.initialize(256,random);
KeyPair对= generator.generateKeyPair();
键pubKey = pair.getPublic();
Key privKey = pair.getPrivate();
//存储公钥
尝试{
storeKey(pubKey,Constants.KEY_PATH.concat(Constants.SERVER_PREFIX.concat( - publickey)));
} catch(Exception e){
e.printStackTrace();
DBLogger.logMessage(e.toString(),Status.KEY_GENERATION_ERROR);
}
//存储私钥
尝试{
storeKey(privKey,Constants.KEY_PATH.concat(Constants.SERVER_PREFIX.concat( - privatekey)) );
} catch(Exception e){
e.printStackTrace();
DBLogger.logMessage(e.toString(),Status.KEY_GENERATION_ERROR);
}
}
protected synchronized String encryptUsingPublicKey(String plainText)throws IllegalBlockSizeException,BadPaddingException,
NoSuchAlgorithmException,NoSuchProviderException,NoSuchPaddingException,InvalidKeyException,
FileNotFoundException,IOException ,ClassNotFoundException {
密码密码= Cipher.getInstance(RSA / ECB / PKCS1Padding,BC);
cipher.init(Cipher.ENCRYPT_MODE,readKey(Constants.KEY_PATH.concat(Constants.SERVER_PREFIX.concat( - publickey))),随机);
byte [] cipherText = cipher.doFinal(plainText.getBytes());
System.out.println(cipher:+ new String(cipherText));
return new String(cipherText);
}
protected synchronized String decryptUsingPrivatekey(String cipherText)throws NoSuchAlgorithmException,
NoSuchProviderException,NoSuchPaddingException,InvalidKeyException,FileNotFoundException,
IOException,ClassNotFoundException,IllegalBlockSizeException,BadPaddingException {
密码密码= Cipher.getInstance(RSA / ECB / PKCS1Padding,BC);
cipher.init(Cipher.DECRYPT_MODE,readKey(Constants.KEY_PATH.concat(Constants.SERVER_PREFIX.concat( - privatekey))));
byte [] plainText = cipher.doFinal(cipherText.getBytes());
System.out.println(plain:+ new String(plainText));
return new String(plainText);
}
public static void main(String [] args){
KeyGenerator keyGenerator = new KeyGenerator();
try {
keyGenerator.deleteAllKeys(Constants.KEY_PATH);
keyGenerator.generateKeys();
String cipherText = keyGenerator.encryptUsingPrivateKey(dilshan);
keyGenerator.decryptUsingPublickey(cipherText);
// String cipherText = keyGenerator.encryptUsingPublicKey(dilshan1);
// keyGenerator.decryptUsingPrivatekey(cipherText);
} catch(Exception e){
e.printStackTrace();
DBLogger.logMessage(e.toString(),Status.KEY_GENERATION_ERROR);
}
}
这在大多数时间内效果很好。但有时会产生以下错误。偶尔会发生这种情况。大多数时间这个工作,所以我没有问题是代码。我相信这是与文件系统的序列化/序列化过程有关的。帮助是赞赏
注意:我正在使用bouncycastle。
错误如下,
javax.crypto.BadPaddingException:未知块类型
在org.bouncycastle.jcajce.provider.asymmetric.rsa.CipherSpi.engineDoFinal(未知来源)
在javax.crypto.Cipher.doFinal(DashoA13 * ..)
在com.dilshan.ttp.web.KeyGenerator.decryptUsingPublickey(KeyGenerator.java:105)
在com.dilshan.ttp .web.KeyGenerator.main(KeyGenerator.java:150)
发生在,
byte [] plainText = cipher.doFinal(cipherText.getBytes());
在decryptUsingPrivatekey方法中。
密文是二进制数据。如果使用默认编码将其转换为 String
,那么很可能遇到字符串无法表示的字节序列。因此,在解密期间,当您将 String
转换回字节数组时,最终不会出现相同的字节,解密失败。
为了解决这个问题,不要将密文转换成一个字符串,而是传递字节[]。
I am trying to simulate asymmetric key system. I use following code to generate key pairs, encrypt, decrypt passwords. I have a distributed environment and for the moment I save the keys generated in a file system. I know that is not secure but its just for testing purposes.
private static SecureRandom random = new SecureRandom();
static {
Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
}
protected synchronized void generateKeys() throws InvalidKeyException, IllegalBlockSizeException,
BadPaddingException, NoSuchAlgorithmException, NoSuchProviderException,
NoSuchPaddingException {
KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA", "BC");
generator.initialize(256, random);
KeyPair pair = generator.generateKeyPair();
Key pubKey = pair.getPublic();
Key privKey = pair.getPrivate();
//store public key
try {
storeKey(pubKey, Constants.KEY_PATH.concat(Constants.SERVER_PREFIX.concat("-publickey")));
} catch (Exception e) {
e.printStackTrace();
DBLogger.logMessage(e.toString(), Status.KEY_GENERATION_ERROR);
}
//store private key
try {
storeKey(privKey, Constants.KEY_PATH.concat(Constants.SERVER_PREFIX.concat("-privatekey")));
} catch (Exception e) {
e.printStackTrace();
DBLogger.logMessage(e.toString(), Status.KEY_GENERATION_ERROR);
}
}
protected synchronized String encryptUsingPublicKey(String plainText) throws IllegalBlockSizeException, BadPaddingException,
NoSuchAlgorithmException, NoSuchProviderException, NoSuchPaddingException, InvalidKeyException,
FileNotFoundException, IOException, ClassNotFoundException {
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding", "BC");
cipher.init(Cipher.ENCRYPT_MODE, readKey(Constants.KEY_PATH.concat(Constants.SERVER_PREFIX.concat("-publickey"))), random);
byte[] cipherText = cipher.doFinal(plainText.getBytes());
System.out.println("cipher: " + new String(cipherText));
return new String(cipherText);
}
protected synchronized String decryptUsingPrivatekey(String cipherText) throws NoSuchAlgorithmException,
NoSuchProviderException, NoSuchPaddingException, InvalidKeyException, FileNotFoundException,
IOException, ClassNotFoundException, IllegalBlockSizeException, BadPaddingException {
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding", "BC");
cipher.init(Cipher.DECRYPT_MODE, readKey(Constants.KEY_PATH.concat(Constants.SERVER_PREFIX.concat("-privatekey"))));
byte[] plainText = cipher.doFinal(cipherText.getBytes());
System.out.println("plain : " + new String(plainText));
return new String(plainText);
}
public static void main(String[] args) {
KeyGenerator keyGenerator = new KeyGenerator();
try {
keyGenerator.deleteAllKeys(Constants.KEY_PATH);
keyGenerator.generateKeys();
String cipherText = keyGenerator.encryptUsingPrivateKey("dilshan");
keyGenerator.decryptUsingPublickey(cipherText);
// String cipherText = keyGenerator.encryptUsingPublicKey("dilshan1");
// keyGenerator.decryptUsingPrivatekey(cipherText);
} catch (Exception e) {
e.printStackTrace();
DBLogger.logMessage(e.toString(), Status.KEY_GENERATION_ERROR);
}
}
This works perfectly well in most of the time. But some times it generates following error. This happens occasionally. Most of the time this works so I have no issue is with the code. I belive this is something to do with the serialization/serialization process to file system. Help is appreciated.
Note : I am using bouncycastle.
Error is as follows,
javax.crypto.BadPaddingException: unknown block type
at org.bouncycastle.jcajce.provider.asymmetric.rsa.CipherSpi.engineDoFinal(Unknown Source)
at javax.crypto.Cipher.doFinal(DashoA13*..)
at com.dilshan.ttp.web.KeyGenerator.decryptUsingPublickey(KeyGenerator.java:105)
at com.dilshan.ttp.web.KeyGenerator.main(KeyGenerator.java:150)
Happens at,
byte[] plainText = cipher.doFinal(cipherText.getBytes());
in decryptUsingPrivatekey method.
The cipher text is binary data. If you convert it to a String
using the default encoding it is very likely that you encounter byte sequences that cannot be represented by a character. Thus, during decryption when you convert the String
back to a byte array you don't end up with the same bytes and the decryption fails.
To solve that, don't convert the cipher text to a string, instead carry the byte[] around.
这篇关于javax.crypto.BadPaddingException:未知块类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!