我試圖加密和使用JKS密鑰庫file.But而解密我收到以下錯誤......錯誤javax.crypto.IllegalBlockSizeException:解密
這裏是我的加密和解密類解密字符串:
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;
}
}
這裏是我的課我在哪裏調用這些方法: -
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)
這個錯誤而解密在public String decrypte (String encCardNo)
方法線
byte[] cipherData = cipher.doFinal(encCardNo.getBytes());
。請解釋我該如何解決這個問題。
@ Artjom B. ya man這是實際的代碼。通過字符串的學生,我得到這個例外.. :( –
那麼,馬滕的答案是正確的。我正在看加密函數,而不是解密函數。 –