错误javax.crypto.IllegalBlockSizeException:正在解密
Error javax.crypto.IllegalBlockSizeException: in Decryption
我正在尝试使用 jks Keystore file.But 加密和解密字符串,但解密时出现以下错误...
这里是我的class加密和解密:
package com.Encrypt;
import java.io.FileInputStream;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.KeyPair;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.UnrecoverableEntryException;
import java.security.UnrecoverableKeyException;
import java.security.KeyStore.PrivateKeyEntry;
import java.security.cert.Certificate;
import java.security.cert.CertificateException;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import org.apache.commons.codec.binary.Base64;
public class Encrypt {
public String encyptCard(String card) throws KeyStoreException, NoSuchAlgorithmException, CertificateException, IOException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException, UnrecoverableKeyException{
FileInputStream is = new FileInputStream("C:/Users/admin/Desktop/keystore/ksjksformat.jks");
String keystpassw = "9801461740";
String alias = "ksjksformat";
KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
ks.load(is,keystpassw.toCharArray() );
Certificate cert = ks.getCertificate(alias);
PublicKey publicKey = cert.getPublicKey();
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
byte[] cipherData = cipher.doFinal(card.getBytes());
String cipherData1 = Base64.encodeBase64String(cipherData);
return cipherData1;
}
public String decrypte (String encCardNo) throws KeyStoreException, NoSuchAlgorithmException, CertificateException, IOException, UnrecoverableEntryException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException{
FileInputStream is = new FileInputStream("C:/Users/admin/Desktop/keystore/ksjksformat.jks");
String keystpassw = "9801461740";
String alias = "ksjksformat";
KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
ks.load(is,keystpassw.toCharArray() );
Key key = ks.getKey(alias, keystpassw.toCharArray());
Certificate cert = ks.getCertificate(alias);
PublicKey publicKey = cert.getPublicKey();
new KeyPair(publicKey, (PrivateKey) (key));
KeyStore.ProtectionParameter protParam = new KeyStore.PasswordProtection(keystpassw.toCharArray());
KeyStore.PrivateKeyEntry pkentry = (PrivateKeyEntry) ks.getEntry(alias, protParam);
PrivateKey myPrivateKey =pkentry.getPrivateKey();
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.DECRYPT_MODE, myPrivateKey);
byte[] cipherData = cipher.doFinal(encCardNo.getBytes());
String decrypted =Base64.decodeBase64(cipherData).toString();
return decrypted;
}
}
这是我的 class 调用这些方法的地方:-
package com.Encrypt;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.UnrecoverableEntryException;
import java.security.cert.CertificateException;
import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
public class CryptoHelper {
public static void main(String[] argv) throws InvalidKeyException, KeyStoreException, NoSuchAlgorithmException, CertificateException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException, IOException, UnrecoverableEntryException {
Encrypt obj = new Encrypt();
String answerEnc = obj.encyptCard("student");
System.out.println("encrypted data----------->"+answerEnc);
String Orginial_data = obj.decrypte(answerEnc);
System.out.println("Decrypted data-------->"+Orginial_data);
}
}
现在我收到这个错误:-
Exception in thread "main" javax.crypto.IllegalBlockSizeException: Data must not be longer than 256 bytes
at com.sun.crypto.provider.RSACipher.a(DashoA13*..)
at com.sun.crypto.provider.RSACipher.engineDoFinal(DashoA13*..)
at javax.crypto.Cipher.doFinal(DashoA13*..)
at com.Encrypt.Encrypt.decrypte(Encrypt.java:56)
at com.Encrypt.CryptoHelper.main(CryptoHelper.java:17)
此错误发生在第
行解密时
byte[] cipherData = cipher.doFinal(encCardNo.getBytes());
在 public String decrypte (String encCardNo)
方法中。请解释我该如何解决这个问题。
你需要在解密之前解码,而不是之后。
作为在发布您自己的答案时接受我的答案的一项特殊服务,对 decrypte
:
进行了一些重写
public String decrypte(final String encCardNo) throws IllegalBlockSizeException,
BadPaddingException {
// --- setup (should be stored differently)
final char[] keystpassw = "9801461740".toCharArray();
final String alias = "ksjksformat";
// --- retrieve private key from store
final PrivateKey key;
try (final FileInputStream is = new FileInputStream(
"C:/Users/admin/Desktop/keystore/ksjksformat.jks")) {
final KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
ks.load(is, keystpassw);
key = (PrivateKey) ks.getKey(alias, keystpassw);
} catch (final KeyStoreException | IOException | NoSuchAlgorithmException | UnrecoverableKeyException | CertificateException e) {
throw new IllegalStateException("Could not load key from key store", e);
}
// --- initialize cipher
final Cipher cipher;
try {
// should use OAEP instead
cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
cipher.init(Cipher.DECRYPT_MODE, key);
} catch (final NoSuchAlgorithmException | NoSuchPaddingException e) {
throw new IllegalStateException(
"RSA PKCS v1.5 should always be available", e);
} catch (final InvalidKeyException e) {
throw new IllegalStateException("Key is not an RSA private key", e);
}
// --- decode
final byte[] decoded;
try {
decoded = Base64.getDecoder().decode(encCardNo);
} catch (final IllegalArgumentException e) {
throw new IllegalArgumentException("Invalid encoded ciphertext", e);
}
// --- decrypt
final byte[] cipherData = cipher.doFinal(decoded);
final String cardNo = new String(cipherData, StandardCharsets.US_ASCII);
// --- clean up
try {
key.destroy();
} catch (final DestroyFailedException e) {
// we tried, possibly log this
}
Arrays.fill(cipherData, (byte) 0);
return cardNo;
}
@Maarten Bodewes答案正确我们必须先解码再解密..
谢谢@Maarten Bodewes
这是返回正确输出的解密方法的代码。
`
public String decrypte (String encCardNo) throws KeyStoreException, NoSuchAlgorithmException, CertificateException, IOException, UnrecoverableEntryException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException{
FileInputStream is = new FileInputStream("C:/Users/admin/Desktop/keystore/ksjksformat.jks");
String keystpassw = "9801461740";
String alias = "ksjksformat";
KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
ks.load(is,keystpassw.toCharArray() );
Key key = ks.getKey(alias, keystpassw.toCharArray());
Certificate cert = ks.getCertificate(alias);
PublicKey publicKey = cert.getPublicKey();
new KeyPair(publicKey, (PrivateKey) (key));
KeyStore.ProtectionParameter protParam = new KeyStore.PasswordProtection(keystpassw.toCharArray());
KeyStore.PrivateKeyEntry pkentry = (PrivateKeyEntry) ks.getEntry(alias, protParam);
PrivateKey myPrivateKey =pkentry.getPrivateKey();
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.DECRYPT_MODE, myPrivateKey);
byte[] decoded = Base64.decodeBase64(encCardNo);
byte[] cipherData = cipher.doFinal(decoded);
return new String(cipherData);
}`
我正在尝试使用 jks Keystore file.But 加密和解密字符串,但解密时出现以下错误...
这里是我的class加密和解密:
package com.Encrypt;
import java.io.FileInputStream;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.KeyPair;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.UnrecoverableEntryException;
import java.security.UnrecoverableKeyException;
import java.security.KeyStore.PrivateKeyEntry;
import java.security.cert.Certificate;
import java.security.cert.CertificateException;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import org.apache.commons.codec.binary.Base64;
public class Encrypt {
public String encyptCard(String card) throws KeyStoreException, NoSuchAlgorithmException, CertificateException, IOException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException, UnrecoverableKeyException{
FileInputStream is = new FileInputStream("C:/Users/admin/Desktop/keystore/ksjksformat.jks");
String keystpassw = "9801461740";
String alias = "ksjksformat";
KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
ks.load(is,keystpassw.toCharArray() );
Certificate cert = ks.getCertificate(alias);
PublicKey publicKey = cert.getPublicKey();
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
byte[] cipherData = cipher.doFinal(card.getBytes());
String cipherData1 = Base64.encodeBase64String(cipherData);
return cipherData1;
}
public String decrypte (String encCardNo) throws KeyStoreException, NoSuchAlgorithmException, CertificateException, IOException, UnrecoverableEntryException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException{
FileInputStream is = new FileInputStream("C:/Users/admin/Desktop/keystore/ksjksformat.jks");
String keystpassw = "9801461740";
String alias = "ksjksformat";
KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
ks.load(is,keystpassw.toCharArray() );
Key key = ks.getKey(alias, keystpassw.toCharArray());
Certificate cert = ks.getCertificate(alias);
PublicKey publicKey = cert.getPublicKey();
new KeyPair(publicKey, (PrivateKey) (key));
KeyStore.ProtectionParameter protParam = new KeyStore.PasswordProtection(keystpassw.toCharArray());
KeyStore.PrivateKeyEntry pkentry = (PrivateKeyEntry) ks.getEntry(alias, protParam);
PrivateKey myPrivateKey =pkentry.getPrivateKey();
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.DECRYPT_MODE, myPrivateKey);
byte[] cipherData = cipher.doFinal(encCardNo.getBytes());
String decrypted =Base64.decodeBase64(cipherData).toString();
return decrypted;
}
}
这是我的 class 调用这些方法的地方:-
package com.Encrypt;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.UnrecoverableEntryException;
import java.security.cert.CertificateException;
import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
public class CryptoHelper {
public static void main(String[] argv) throws InvalidKeyException, KeyStoreException, NoSuchAlgorithmException, CertificateException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException, IOException, UnrecoverableEntryException {
Encrypt obj = new Encrypt();
String answerEnc = obj.encyptCard("student");
System.out.println("encrypted data----------->"+answerEnc);
String Orginial_data = obj.decrypte(answerEnc);
System.out.println("Decrypted data-------->"+Orginial_data);
}
}
现在我收到这个错误:-
Exception in thread "main" javax.crypto.IllegalBlockSizeException: Data must not be longer than 256 bytes
at com.sun.crypto.provider.RSACipher.a(DashoA13*..)
at com.sun.crypto.provider.RSACipher.engineDoFinal(DashoA13*..)
at javax.crypto.Cipher.doFinal(DashoA13*..)
at com.Encrypt.Encrypt.decrypte(Encrypt.java:56)
at com.Encrypt.CryptoHelper.main(CryptoHelper.java:17)
此错误发生在第
行解密时byte[] cipherData = cipher.doFinal(encCardNo.getBytes());
在 public String decrypte (String encCardNo)
方法中。请解释我该如何解决这个问题。
你需要在解密之前解码,而不是之后。
作为在发布您自己的答案时接受我的答案的一项特殊服务,对 decrypte
:
public String decrypte(final String encCardNo) throws IllegalBlockSizeException,
BadPaddingException {
// --- setup (should be stored differently)
final char[] keystpassw = "9801461740".toCharArray();
final String alias = "ksjksformat";
// --- retrieve private key from store
final PrivateKey key;
try (final FileInputStream is = new FileInputStream(
"C:/Users/admin/Desktop/keystore/ksjksformat.jks")) {
final KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
ks.load(is, keystpassw);
key = (PrivateKey) ks.getKey(alias, keystpassw);
} catch (final KeyStoreException | IOException | NoSuchAlgorithmException | UnrecoverableKeyException | CertificateException e) {
throw new IllegalStateException("Could not load key from key store", e);
}
// --- initialize cipher
final Cipher cipher;
try {
// should use OAEP instead
cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
cipher.init(Cipher.DECRYPT_MODE, key);
} catch (final NoSuchAlgorithmException | NoSuchPaddingException e) {
throw new IllegalStateException(
"RSA PKCS v1.5 should always be available", e);
} catch (final InvalidKeyException e) {
throw new IllegalStateException("Key is not an RSA private key", e);
}
// --- decode
final byte[] decoded;
try {
decoded = Base64.getDecoder().decode(encCardNo);
} catch (final IllegalArgumentException e) {
throw new IllegalArgumentException("Invalid encoded ciphertext", e);
}
// --- decrypt
final byte[] cipherData = cipher.doFinal(decoded);
final String cardNo = new String(cipherData, StandardCharsets.US_ASCII);
// --- clean up
try {
key.destroy();
} catch (final DestroyFailedException e) {
// we tried, possibly log this
}
Arrays.fill(cipherData, (byte) 0);
return cardNo;
}
@Maarten Bodewes答案正确我们必须先解码再解密..
谢谢@Maarten Bodewes
这是返回正确输出的解密方法的代码。
`
public String decrypte (String encCardNo) throws KeyStoreException, NoSuchAlgorithmException, CertificateException, IOException, UnrecoverableEntryException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException{
FileInputStream is = new FileInputStream("C:/Users/admin/Desktop/keystore/ksjksformat.jks");
String keystpassw = "9801461740";
String alias = "ksjksformat";
KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
ks.load(is,keystpassw.toCharArray() );
Key key = ks.getKey(alias, keystpassw.toCharArray());
Certificate cert = ks.getCertificate(alias);
PublicKey publicKey = cert.getPublicKey();
new KeyPair(publicKey, (PrivateKey) (key));
KeyStore.ProtectionParameter protParam = new KeyStore.PasswordProtection(keystpassw.toCharArray());
KeyStore.PrivateKeyEntry pkentry = (PrivateKeyEntry) ks.getEntry(alias, protParam);
PrivateKey myPrivateKey =pkentry.getPrivateKey();
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.DECRYPT_MODE, myPrivateKey);
byte[] decoded = Base64.decodeBase64(encCardNo);
byte[] cipherData = cipher.doFinal(decoded);
return new String(cipherData);
}`