标签:加密 整理 加密算法 ring encrypted air private 今天 new
加密算法在各个网站运用很平常,今天整理代码的时候看到了我们项目中运用了RSA加密,就了解了一下。
先简单说一下RSA加密算法原理,RSA算法基于一个十分简单的数论事实:将两个大质数相乘十分容易,但是想要对其乘积进行因式分解却极其困难,因此可以将乘积公开作为加密密钥。
RSAPublicKey rsap; rsap = (RSAPublicKey) RSAUtil.getKeyPair().getPublic(); //模 String module = rsap.getModulus().toString(16); //公钥指数 String empoent = rsap.getPublicExponent().toString(16); request.setAttribute("m", module); request.setAttribute("e", empoent);
这是java代码,简单来说就是将模和公钥指数传回到前台页面上,按我的理解module就是上面所说的n,empoent也就是上面说的e1。
接着是Javascript的处理,先引入3个js文件RSA.js,BigInt.js,Barrett.js(网上可以随便找到)
function doEncrypt(){ var result = $("#password").val(); setMaxDigits(130); //3个参数,分别是公钥指数,私钥指数,module (一般module是1024位的长度,私钥肯定是不会传到前台的) key = new RSAKeyPair("12345","","12a3d32ad"); //生成密文 result = encryptedString(key, encodeURIComponent(result)); $("#encrypt").attr("value", 1); $("#pwd").attr("value", result); }
之后就把生成的公钥生成的公钥传回去就行了。
RSAUtils.decryptByPrivateKey(mi, priKey);
然后把传回去的密文通过私钥进行解密就行了,大致过程就是这样。
标签:加密 整理 加密算法 ring encrypted air private 今天 new
原文地址:http://www.cnblogs.com/henuyuxiang/p/6831893.html