JS客户端RSA加密,Java服务端解密
常用语网页客户端对密码加密,在后端java解密还原
java代码依赖
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.4</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-jdk15on</artifactId>
<version>1.52</version>
</dependency>
js代码依赖 jsencrypt.js
js代码:
// java后台生成的 var publicKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDTu5u08Wel08uWM02GYYRlFXfmEHH7DAbSWufUdU8NlZrE/4BAOwqPMu/vMLdCi4GRHDSmWhoqjC5/7oKoALl6nFCAObtSl6RiWdc8KvcN1D45PASs8M/YPY+oa8iNYZA/drtgXEw4NniC0EB47miGVL4POyLOE3dJlk4LD/AjtQIDAQAB"; var encrypt = new JSEncrypt(); encrypt.setPublicKey(publicKey); // 加密 alert(encrypt.encrypt("hello"));
java代码:
import java.security.KeyPair; import java.security.KeyPairGenerator; import java.security.SecureRandom; import java.security.Security; import java.security.interfaces.RSAPrivateKey; import java.security.interfaces.RSAPublicKey; import javax.crypto.Cipher; import org.apache.commons.codec.binary.Base64; public class RSAUtils { private static final KeyPair keyPair = initKey(); private static KeyPair initKey(){ try { Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider()); SecureRandom random = new SecureRandom(); KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA", "BC"); generator.initialize(1024, random); return generator.generateKeyPair(); }catch (Exception e) { throw new RuntimeException(e); } } /** * 生成public key * @return */ public static String generateBase64PublicKey(){ RSAPublicKey key = (RSAPublicKey)keyPair.getPublic(); return new String(Base64.encodeBase64(key.getEncoded())); } /** * 解密 * @param string * @return */ public static String decryptBase64(String string) { return new String(decrypt(Base64.decodeBase64(string))); } private static byte[] decrypt(byte[] string) { try { Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider()); Cipher cipher = Cipher.getInstance("RSA/None/PKCS1Padding", "BC"); RSAPrivateKey pbk = (RSAPrivateKey)keyPair.getPrivate(); cipher.init(Cipher.DECRYPT_MODE, pbk); byte[] plainText = cipher.doFinal(string); return plainText; }catch (Exception e) { throw new RuntimeException(e); } } public static void main(String[] args) { // 生成public key System.out.println(generateBase64PublicKey()); // 解密 System.out.println(decryptBase64("wAfY9JkoKay9SxcPIs1FcG+t6sR+wYwAs/mh9DpfcBraxzqoZdb9LyaAigzFQ0EKck9OyHL0dhv+Uxuw5hHw6CPT0B2Z0i1gwrjDUNaL1gWvqt1pDJVGrIYPLJSjs9xktFhY1jbxQgXGjyCt06Rwid5sJknw90AUO0CyQulfipg=")); } }