• 前后端分离通过RSA加密解密传输数据


    Provider p = new BouncyCastleProvider();
            KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("RSA", p);
            keyPairGen.initialize(1024, new SecureRandom());
            KeyPair keyPair = keyPairGen.generateKeyPair();
    //打印公钥
            System.out.println(keyPair.getPublic());
    //打印私钥
            System.out.println(keyPair.getPrivate());
    

      运行上面的代码,可以得到密钥对

    RSA Public Key
                modulus: 8caaed0ab3f431f7d2f00fa807b1de68f2c544df9db903481fa8d02ca79d592a92e55821af62859bff457ddf3a1eadea8376ff7efdd3b1a10dd9d385a678a369547bd2f87ccc91faae407013456522f21ffd5fcc6db2c8dcf4978492f8595305a683ee4074d2a06daa3d683f1d911910561df9e01355b7899aa479d934357223
        public exponent: 10001
    
    RSA Private CRT Key
                modulus: 8caaed0ab3f431f7d2f00fa807b1de68f2c544df9db903481fa8d02ca79d592a92e55821af62859bff457ddf3a1eadea8376ff7efdd3b1a10dd9d385a678a369547bd2f87ccc91faae407013456522f21ffd5fcc6db2c8dcf4978492f8595305a683ee4074d2a06daa3d683f1d911910561df9e01355b7899aa479d934357223
        public exponent: 10001
       private exponent: 3bafcb24df769e3d29a3f300b9431fbc2846cc12963a0aca6fa491b060eec306ed8da2f7a81385ddcb531135c6d37db8245039d9089aeaa40bdc8184143edc1712fedfcbe982cf7c3070afc6589975ca2df47bb4051b5b7800b4ff753702d5cf08cb2d9ad222cfc7a79eaf214e0ef6e199e988dcc49b81f1b902293fbc1a9a01
                 primeP: d7efc6d32cec18ff137e92eb10e32350a170d4631f64043943adf274c45bf082fbbb48cca9bccd32db5b8df72e28204da3bbe9fac087f14fb83d1cb4d29a2629
                 primeQ: a6c42433f4b66915877d8445e1bab8e537452a1e2d776a9c5c8a1f221eda35dcd33232cd0e918930df06c3022494d2043c71a65fbfdfaf204d284cec10a5676b
         primeExponentP: c9f0a2071186c6a59d2c8ce72a46d0daf1ef9ba00b5634a0fd0b2aa84a8e14b1da0b3ebcafac3256724fffc6feb265eb55c941bacfdd122e54854a584c6ffe69
         primeExponentQ: 9eee6afa410bded62fcc45553c2d71acb1a1eee3d7bce9139f75daf9efa15297e38701b73f5d1ffc3d70fa447e8c7e229f60488914199f930f358b29a72b9b85
         crtCoefficient: 9a8326c21bfdd6121d9c36d8bca080fd18f204d111684bce45e94befedcff4d2b83d466b46f19916ac0c8ed8e13884786b4a20a53393946f4567b86970ab84f3
    
    22342342342342
    
    Process finished with exit code 0
    

      其中,RSA 公钥是两个参数是要发给前端客户的

    RSA Public Key
                modulus: 8caaed0ab3f431f7d2f00fa807b1de68f2c544df9db903481fa8d02ca79d592a92e55821af62859bff457ddf3a1eadea8376ff7efdd3b1a10dd9d385a678a369547bd2f87ccc91faae407013456522f21ffd5fcc6db2c8dcf4978492f8595305a683ee4074d2a06daa3d683f1d911910561df9e01355b7899aa479d934357223
        public exponent: 10001
    

      

    前端JavaScript代码, 依赖 security.js
    https://files.cnblogs.com/files/xiaojf/security.js
    <html>
    <body>
    <script src="security.js" type="text/javascript"></script>
    <script>
        var key = RSAUtils.getKeyPair('10001', '', 'c6e442535e1dd6968e4ccd7735299278d989cb938a2f97c1081c4e6796895a3063510592e2e90ed427d5a604428ce46391dcb2ba6b5f4a86af1347237d1de489a0dc2f68a1f9a265d1ec350fccd8a76be5004211cee5bf05a083afa17cf335871b141e5c4329f69d1a3546613e0fa7833b7a253c460e5bb0c075dacfccfd6d0d');
        console.log(RSAUtils.encryptedString(key, '22342342342342'));
    </script>
    </body>
    </html>
    

      运行页面后,可以得到加密后的字符串

    9448472ec3294304fdec01a3700e918b5797eb4698f7e80f8877a4acccd157b6630b25cdd581c2bf57cd7f55ed42a2caea914eadba29950fceb9af1a1fcdb13f67ce030a1f0dd91ba45af885b28b085cfe423a9cc7a47333433214d24a4e4d954dfe65d59d896502a15b636ff0c7a23339ddbb066c8a7f699c4ebd3a2415225b
    

      

    拷贝到Java后台进行解密,操作,可以得到结果

    String hexModulus = "c6e442535e1dd6968e4ccd7735299278d989cb938a2f97c1081c4e6796895a3063510592e2e90ed427d5a604428ce46391dcb2ba6b5f4a86af1347237d1de489a0dc2f68a1f9a265d1ec350fccd8a76be5004211cee5bf05a083afa17cf335871b141e5c4329f69d1a3546613e0fa7833b7a253c460e5bb0c075dacfccfd6d0d";
            String hexPrivateExponent = "1ce625c15c66146a983d82cd493c95242ae35603ba73b4f810682c838d0f4bbb242d5c2bc9cee12b41bff1108b369885fadaa05f0f68bafeb915445e5cd006645eb816d9a8d89b155aeb8478a60325cf4d7e69c12b3076a4cf31b8c24530e7f13533826bc7e87ddf65b7ccc65951bd7c34238b9f08a1e8a1c2c4dd762c50309";
    // 这就是上面html输出的密文
            String encryptPassword = "9448472ec3294304fdec01a3700e918b5797eb4698f7e80f8877a4acccd157b6630b25cdd581c2bf57cd7f55ed42a2caea914eadba29950fceb9af1a1fcdb13f67ce030a1f0dd91ba45af885b28b085cfe423a9cc7a47333433214d24a4e4d954dfe65d59d896502a15b636ff0c7a23339ddbb066c8a7f699c4ebd3a2415225b"; Provider provider = new BouncyCastleProvider(); KeyFactory keyFac = KeyFactory.getInstance("RSA", provider); RSAPrivateKeySpec priKeySpec = new RSAPrivateKeySpec(new BigInteger(hexModulus, 16), new BigInteger(hexPrivateExponent, 16)); // 生成用于解密的私钥 RSAPrivateKey pk = (RSAPrivateKey) keyFac.generatePrivate(priKeySpec); // 解密 Cipher cipher = Cipher.getInstance("RSA", provider); cipher.init(2, pk); byte[] rawPasswordByte = cipher.doFinal(Hex.decodeHex(encryptPassword.toCharArray())); System.out.println(StringUtils.reverse(new String(rawPasswordByte)));

      得到结果

    22342342342342
    

      

  • 相关阅读:
    Linux shell 学习总结
    linux shell 比较总结
    NSURL基本操作 HA
    Mac node.js install HA
    nodejs学习资料收集 HA
    xcode技巧 HA
    google web app/enxtions 学习资料收集 HA
    Failed to upload *.app on Device 可能的解决方法 HA
    iphone开发常见问题小集2 HA
    cocos2d收集 HA
  • 原文地址:https://www.cnblogs.com/xiaojf/p/12363684.html
Copyright © 2020-2023  润新知