我使用javascript字符串进行RSA加密验证
我使用以下页面来创建私钥和公钥
http://travistidwell.com/jsencrypt/demo/
公钥和私钥
我想用java解码
我已经多次尝试但有问题
请帮帮我
谢谢你
String sprivatekey = "MIICWwIBAAKBgHWiNo/ql4zfT/DA3tr+atZZef01wrwSiPaD3M3efki7/o1NZxvP"
+ "PyjcIrGT/qMrVGE9ncj4+y+vb8C1K2gyegKJwS8vF74Dvlzt2VL/3BZpfmZ1WACt"
+ "5xNzme10lxyH2LERXOrUzr5YHsgraa+wabS90amja02/BrkY9NYi7QqXAgMBAAEC"
+ "gYBAKUQdZ1VWDw+pP2nOhOp9E7zH8aTPtERkQTQUTgEPZwrGw6szrezDOp7pwNEo"
+ "6p9RkqwXTEalkckgkTCl2rJVIcjjGsselQf0RXYAKeLUkojhl+ypVrFbMN6/IYd/"
+ "/FkV57eSY2eYZ2+wRAz18zrEtSS/wWaiRMwV6Tb9ON72IQJBANoqDT0S9R2FoQ0F"
+ "uDvXB6IlGh0/QbewYDs5/An/rLi1ZexAtBTqn9DJ9MjR7Jp2OpYaGWBgE/0J+8Dy"
+ "n+Bow7kCQQCKCNpjeMd3RQc8uQmdSFEK8b1vWxi+phB95oKfsqA1Zf7IJDvxInhJ"
+ "BkAWHK6dzWCwYR23sJrF/shkXPm3YQjPAkBVxmjUFWsIA20RuKj55g5bQukkjKVi"
+ "+larkp20tLCNIs0C+WGUQ7c4GN9EQb0b8dYRBAvsJ3pUZ6Ny9jHDwo0JAkATV2A6"
+ "de8FIE/+SzM0rbBlZzATDWGvPResfIMrG11lfhAwPRuFEz5lhRGvx38UL3Xv4PvI"
+ "BF/7hFLR9VEsR9VzAkEAzAX2TggCaPaYFpLWIZH2NSKjuOrKXLI3zEwIaVjM55dd"
+ "Kcj97JyOzC5vurPpw6TZOWiGJ3bZRswJXcCxWianMw==";
String spublickey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCS6nsbqyNtrv7dMm9mje98L8So"
+ "W/wgL9CzahjSHq56YcV4qyapuT9HOglCbD12xyM9bi0bvg5wkyJ37oMWNvxsiYh9"
+ "u0eLZlzOIR+2CmjeL2OZDqGqn7HFCdfbbX9mdTkm4mE+pVOsWIuts2ZegMeU9EWz"
+ "M91ZGA0mBug6VaavswIDAQAB";
String plainText="Tacdsfddfdfd";
String cipherText="YlXqYjr+rNUnh9Cta6wMWamqNsVGuRc01NA3qjtazdtcKJoJ5yfPQmq9OJo3Qc9k2JztzmFvSDyV83Bru8fl/xcflFjb/H+IL966lXGVh83QTQaVGTwMkV37iYvz+oCJmOmq6f+O34/0u0qilvgNgfAevYZY2ZWTbaGYvS1pXmQ=";
我使用java字符串键读入PrivateKey
方法读取私钥
public static RSAPrivateKey getPrivateKeyFromString(String key)
throws IOException, GeneralSecurityException {
RSAPrivateKey pk;
byte[] encodedPrivateKey = Base64.decodeBase64(key);
try {
public static final String ALGORITHM = "RSA";
ASN1Sequence primitive = (ASN1Sequence) ASN1Sequence
.fromByteArray(encodedPrivateKey);
Enumeration<?> e = primitive.getObjects();
BigInteger v = ((DERInteger) e.nextElement()).getValue();
int version = v.intValue();
if (version != 0 && version != 1) {
throw new IllegalArgumentException(
"wrong version for RSA private key");
}
BigInteger modulus = ((DERInteger) e.nextElement()).getValue();
BigInteger privateExponent = ((DERInteger) e.nextElement())
.getValue();
RSAPrivateKeySpec spec = new RSAPrivateKeySpec(modulus,
privateExponent);
KeyFactory kf = KeyFactory.getInstance("RSA");
pk = (RSAPrivateKey) kf.generatePrivate(spec);
} catch (IOException e2) {
throw new IllegalStateException();
} catch (NoSuchAlgorithmException e) {
throw new IllegalStateException(e);
} catch (InvalidKeySpecException e) {
throw new IllegalStateException(e);
}
return pk;
}
方法解密java
public static final String ALGORITHM = "RSA";
public static byte[] encrypt(String text, PublicKey key) {
byte[] cipherText = null;
try {
// get an RSA cipher object and print the provider
final Cipher cipher = Cipher.getInstance(ALGORITHM);
// encrypt the plain text using the public key
cipher.init(Cipher.ENCRYPT_MODE, key);
cipherText = cipher.doFinal(text.getBytes());
} catch (Exception e) {
e.printStackTrace();
}
return cipherText;
}
public static String decrypt(byte[] text, PrivateKey key) {
byte[] dectyptedText = null;
try {
// SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
// get an RSA cipher object and print the provider
final Cipher cipher = Cipher.getInstance(ALGORITHM);
// random.nextBytes(text);
// decrypt the text using the private key
cipher.init(Cipher.DECRYPT_MODE, key);
dectyptedText = cipher.doFinal(text);
} catch (Exception ex) {
ex.printStackTrace();
}
return new String(dectyptedText);
}
呼叫解密
public static void main(String[] args) {
String sprivatekey = "MIICWwIBAAKBgHWiNo/ql4zfT/DA3tr+atZZef01wrwSiPaD3M3efki7/o1NZxvP"
+ "PyjcIrGT/qMrVGE9ncj4+y+vb8C1K2gyegKJwS8vF74Dvlzt2VL/3BZpfmZ1WACt"
+ "5xNzme10lxyH2LERXOrUzr5YHsgraa+wabS90amja02/BrkY9NYi7QqXAgMBAAEC"
+ "gYBAKUQdZ1VWDw+pP2nOhOp9E7zH8aTPtERkQTQUTgEPZwrGw6szrezDOp7pwNEo"
+ "6p9RkqwXTEalkckgkTCl2rJVIcjjGsselQf0RXYAKeLUkojhl+ypVrFbMN6/IYd/"
+ "/FkV57eSY2eYZ2+wRAz18zrEtSS/wWaiRMwV6Tb9ON72IQJBANoqDT0S9R2FoQ0F"
+ "uDvXB6IlGh0/QbewYDs5/An/rLi1ZexAtBTqn9DJ9MjR7Jp2OpYaGWBgE/0J+8Dy"
+ "n+Bow7kCQQCKCNpjeMd3RQc8uQmdSFEK8b1vWxi+phB95oKfsqA1Zf7IJDvxInhJ"
+ "BkAWHK6dzWCwYR23sJrF/shkXPm3YQjPAkBVxmjUFWsIA20RuKj55g5bQukkjKVi"
+ "+larkp20tLCNIs0C+WGUQ7c4GN9EQb0b8dYRBAvsJ3pUZ6Ny9jHDwo0JAkATV2A6"
+ "de8FIE/+SzM0rbBlZzATDWGvPResfIMrG11lfhAwPRuFEz5lhRGvx38UL3Xv4PvI"
+ "BF/7hFLR9VEsR9VzAkEAzAX2TggCaPaYFpLWIZH2NSKjuOrKXLI3zEwIaVjM55dd"
+ "Kcj97JyOzC5vurPpw6TZOWiGJ3bZRswJXcCxWianMw==";
String spublickey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCS6nsbqyNtrv7dMm9mje98L8So"
+ "W/wgL9CzahjSHq56YcV4qyapuT9HOglCbD12xyM9bi0bvg5wkyJ37oMWNvxsiYh9"
+ "u0eLZlzOIR+2CmjeL2OZDqGqn7HFCdfbbX9mdTkm4mE+pVOsWIuts2ZegMeU9EWz"
+ "M91ZGA0mBug6VaavswIDAQAB";
try {
PrivateKey privateKey = RSAKey.getPrivateKeyFromString(sprivatekey);
//PublicKey publicKey = RSAKey.getPublicKeyFromString(spublickey);
String v = "YlXqYjr+rNUnh9Cta6wMWamqNsVGuRc01NA3qjtazdtcKJoJ5yfPQmq9OJo3Qc9k2JztzmFvSDyV83Bru8fl/xcflFjb/H+IL966lXGVh83QTQaVGTwMkV37iYvz+oCJmOmq6f+O34/0u0qilvgNgfAevYZY2ZWTbaGYvS1pXmQ=";
//Call method 1
String plainText1 = decrypt(v.getBytes(), privateKey);
System.out.println("Decrypted: " + plainText1);
//Call method 2
String plainText2 = decrypt(v, privateKey);
System.out.println("Decrypted: " + plainText2);
} catch (Exception e) {
e.printStackTrace();
}
}
错误调用方法1
javax.crypto.IllegalBlockSizeException: Data must not be longer than 128 bytes
at com.sun.crypto.provider.RSACipher.doFinal(RSACipher.java:346)
at com.sun.crypto.provider.RSACipher.engineDoFinal(RSACipher.java:391)
at javax.crypto.Cipher.doFinal(Cipher.java:2087)
at com.evnit.futil.security.EncryptionUtil.decrypt(EncryptionUtil.java:146)
at com.evnit.futil.security.EncryptionUtil.main(EncryptionUtil.java:201)
错误调用方法2
javax.crypto.BadPaddingException: Decryption error
at sun.security.rsa.RSAPadding.unpadV15(RSAPadding.java:380)
at sun.security.rsa.RSAPadding.unpad(RSAPadding.java:291)
at com.sun.crypto.provider.RSACipher.doFinal(RSACipher.java:365)
at com.sun.crypto.provider.RSACipher.engineDoFinal(RSACipher.java:391)
at javax.crypto.Cipher.doFinal(Cipher.java:2087)
at com.evnit.futil.security.EncryptionUtil.decrypt(EncryptionUtil.java:167)
at com.evnit.futil.security.EncryptionUtil.main(EncryptionUtil.java:204)