- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
- <HTML>
- <HEAD>
- <TITLE> New Document </TITLE>
- <META NAME="Generator" CONTENT="EditPlus">
- <META NAME="Author" CONTENT="">
- <META NAME="Keywords" CONTENT="">
- <META NAME="Description" CONTENT="">
- </HEAD>
-
- <BODY>
- <SCRIPT LANGUAGE="JavaScript">
- <!--
- // ==================================================================
- // JavaScript RSA算法简单实现
- // Know Bugs:
- // 1. Math.pow存在计算精度问题,因此不能选取较大的质数,否则会溢出
- // By midea0978
- // date:2005-08-10
- // 参考文献
- // 1.RSA算法研究 http://www.cnblogs.com/midea0978/articles/65244.html
- // 2.PKCS #1: RSA Cryptography Specifications Version 2.0
- // http://www.faqs.org/rfcs/rfc2437.html
- // ==================================================================
- RSAAlgorithm()
- function RSAAlgorithm(){
- //1.首先选择两个简单的质数p,q
- var p=5
- var q=13
- var n=p*q
- //随机选择余数r,保证r与(p-1)*(q-1)=48互质
- var r=7
- //得到private key是p,q,r
- document.write("----------------------------------<br>");
- document.write("<B>Private Key:</B><br>");
- document.write("p= "+p+"<br>");
- document.write("q= "+q+"<br>");
- document.write("r= "+r+"<br>");
- document.write("----------------------------------<br>");
- //2.计算public key
- //找到m,保证rm ==1 mod (p-1)*(q-1)
- var num=0
- for(a=0;a<500;a++){
- if(r*a%((p-1)*(q-1))==1){
- num=a;
- break;
- }
- }
- var m=num
- if(m>0){
- document.write("<br>找到m= "+num+" ,计算余数="+(num*r)%((p-1)*(q-1))+",成功!<br>")
- }
- else{
- document.write("找不到满足条件的m,请调整参数");
- }
- document.write("----------------------------------<br>");
- document.write("<B>Public Key:<br></B>");
- document.write("m= "+m+"<br>");
- document.write("n= "+n+"<br>");
- document.write("----------------------------------<br>");
- document.write("<br>");
- document.write("<B>#演示加密过程#</B><br>");
- var text=20 //明文
- document.write("明文="+text+"<br>");
- //接下来, 计算 etext == text^r mod n, (0 <= b < n)
- var etext=Math.pow(text,r)%n;
- document.write("加密后的内容:"+etext+"<br>");
- document.write("<br>");
- document.write("#演示解密过程#<br>");
- //text=etext^m mod pq
- var text=Math.pow(etext,m)%n;
- document.write("解密后的内容:"+text+"<br>");
- }
- /**//*
- JAVA下面的简单实现
- import java.math.BigInteger;
- public class RSAAlgorithm {
- public static void main(String[] args) {
- BigInteger p = new BigInteger("5"); //P
- BigInteger q = new BigInteger("13"); //Q
- BigInteger n = new BigInteger("65"); //N
- BigInteger r = new BigInteger("7");
-
- BigInteger biginteger = new BigInteger("20");
- BigInteger bigintegerbiginteger1 = biginteger.modPow(r, n);
- System.out.println("加密数据:" + biginteger1);
- BigInteger m = r.modInverse(new BigInteger("48"));
- System.out.println("m:" + m);
- BigInteger biginteger2 = biginteger1.modPow(m, n);
- System.out.println("解密数据:" + biginteger2);
- }
- }
- */
- //-->
- </SCRIPT>
- </BODY>
- </HTML>
posted @
2012-01-13 16:08
暗痛
阅读(
945)
评论()
编辑
收藏
举报