参考:
https://github.com/ziyan/javascript-rsa他用的是php,java自己实现一下是很快的,使用方法如下,亲测可用。
js加密:
var __pem = "-----BEGIN PUBLIC KEY-----你的公钥-----END PUBLIC KEY-----";
var __key = RSA.getPublicKey(__pem);
var pwd = RSA.encrypt(_password,__key);
java解密:
private String decryptPassword(String encryptPassword) {
String password = encryptPassword;
try {
Cipher cipher = Cipher.getInstance("RSA/None/PKCS1Padding", new BouncyCastleProvider());
cipher.init(Cipher.DECRYPT_MODE, PRIVATE);
password = new String(cipher.doFinal(Base64.decode(password)));
} catch (Exception e) {
e.printStackTrace();
}
return password;
}
private static final PrivateKey PRIVATE = getRSAPrivateKey();
private static PrivateKey getRSAPrivateKey() {
PrivateKey privateKey = null;
try {
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
byte[] keyBytes = Base64.decode("你的密钥");
PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(keyBytes);
privateKey = keyFactory.generatePrivate(pkcs8KeySpec);
} catch (Exception e) {
e.printStackTrace();
}
return privateKey;
}