1.之前项目里与第三方交互需要用到rsa加密,当时研究了一点点,这里做下记录吧,大家觉得有什么问题,勿喷。

2.rsa找的插件,直接贴代码吧

<script type="text/javascript">
var PUBLICK_KEY = 'MIGeMA0GCSqGSIb3DQEBAQUAA4GMADCBiAKBgH9ZK++fC83DV62txcrJ3miwio4y5GlWy0nDrIl7OqshrJC3BVvfesPWijYhCLS53U0RRPc/TCICegWuKTzGWYTRG3o17n7mGd6XE/Lf61a3HPhHL3GaVQgJrXryNedfS+v7vISXEZzhfjNVAPnlJQBERIG7ERxnCX7kxYSXp8cvAgMBAAE=';
var private_key = 'MIICWAIBAAKBgH9ZK++fC83DV62txcrJ3miwio4y5GlWy0nDrIl7OqshrJC3BVvfesPWijYhCLS53U0RRPc/TCICegWuKTzGWYTRG3o17n7mGd6XE/Lf61a3HPhHL3GaVQgJrXryNedfS+v7vISXEZzhfjNVAPnlJQBERIG7ERxnCX7kxYSXp8cvAgMBAAECf1UQO0jT9A59bROV/4h1Txqe0e4qEGhMkX20NouexJGK7Lm9htbXcClgJPIDHs2ZWTkhT+/XWANfpCInM2RlGWWnCk9+TaIID/2LL+Ct9F8l1F08GO0eW+4LOMeNzdVbFYtyFmUwykMMZqL8GKkM+xp4LdfCPM2a1XPDm4YT7+ECQQDhrZCq8B34zWlLgiFIralxga1XvfH9xH3jv/QqSeyBwiBQUTcrU0Kw6iyrP5yID4vuFGeP1xZ0TJy0mP2y+7ZfAkEAkHVz+NvBvFnmKl5JjoJ3OVQp50RYXUKegQW+gi7EuzqZeRG3TAcIF6H4nK5vG7/j8XX/UIiG7spFmOE+AeyBMQJAL6M9qHerO/vQQDol4HuYCve+qYjU7iZuFIdOeSbFn9Zkj8+0/5ZBu6qS50s0NZxig00XV0ul18LcSVsKIp/zrQJAcF6c1rEC8+OuUbjEWRGNck/QxK/6WJkwuMZiGdwNdmKv7Whlf8NcfR2mSSK7FQfgZPxXnSqGY4t+NdzIIjD5kQJASvx5snutTqbRu/i5PCijtmK69pL5NzdYNfvTjfK3ip+ungUDzPOOpvj8kQ1LE88/wgobjIdl3gH2+nVS7KAidg==';

function encryptRsa(publickkey,str){

    var encrypt = new JSEncrypt();
    encrypt.setPublicKey(publickkey);
    var encrypted = encrypt.encrypt(str);

    var decrypt = new JSEncrypt();
    decrypt.setPrivateKey(private_key);
    var uncrypted = decrypt.decrypt(encrypted);
    
    console.log("加密结果:" + encrypted);
    console.log("解密结果:" + uncrypted);
    var data = encrypt.debase64(encrypted);
}

3.当时后台没时间联调,只能自己对接口,怎么都不对,后来发现是插件里把结果base64加密了。自己解开就可以了。

4.完整的例子看我的github吧:
https://github.com/nanshan11/...

5.附带md5的加密方法,还有几种字符转16进制的方法(严格说是字符的Unicode编码转16进制)


南山
32 声望0 粉丝