JavaScript RSA演算法簡單實現)

來源:互聯網
上載者:User
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">  
  2.  <HTML>  
  3.  <HEAD>  
  4.  <TITLE> New Document </TITLE>  
  5.  <META NAME="Generator" CONTENT="EditPlus">  
  6.  <META NAME="Author" CONTENT="">  
  7.  <META NAME="Keywords" CONTENT="">  
  8.  <META NAME="Description" CONTENT="">  
  9.  </HEAD>  
  10.   
  11. <BODY>  
  12. <SCRIPT LANGUAGE="JavaScript">  
  13. <!--  
  14. // ==================================================================  
  15. // JavaScript RSA演算法簡單實現  
  16. // Know Bugs:  
  17. //   1. Math.pow存在計算精度問題,因此不能選取較大的質數,否則會溢出  
  18. // By midea0978  
  19. // date:2005-08-10  
  20. // 參考文獻  
  21. //    1.RSA演算法研究 http://www.cnblogs.com/midea0978/articles/65244.html  
  22. //    2.PKCS #1: RSA Cryptography Specifications Version 2.0  
  23. //           http://www.faqs.org/rfcs/rfc2437.html   
  24. // ==================================================================  
  25. RSAAlgorithm()  
  26. function RSAAlgorithm(){  
  27.  //1.首先選擇兩個簡單的質數p,q  
  28.  var p=5  
  29.  var q=13  
  30. var n=p*q   
  31.  //隨機播放餘數r,保證r與(p-1)*(q-1)=48互質  
  32.  var r=7   
  33.  //得到private key是p,q,r  
  34.  document.write("----------------------------------<br>");  
  35.  document.write("<B>Private Key:</B><br>");  
  36.  document.write("p= "+p+"<br>");  
  37.  document.write("q= "+q+"<br>");  
  38.  document.write("r= "+r+"<br>");  
  39.  document.write("----------------------------------<br>");  
  40.  //2.計算public key  
  41.  //找到m,保證rm ==1 mod (p-1)*(q-1)  
  42.  var num=0  
  43.  for(a=0;a<500;a++){  
  44.   if(r*a%((p-1)*(q-1))==1){  
  45.    num=a;  
  46.    break;  
  47.   }  
  48.  }  
  49.  var m=num   
  50.  if(m>0){  
  51.   document.write("<br>找到m= "+num+" ,計算餘數="+(num*r)%((p-1)*(q-1))+",成功!<br>")  
  52.  }  
  53.  else{  
  54.   document.write("找不到滿足條件的m,請調整參數");  
  55.  }  
  56.  document.write("----------------------------------<br>");  
  57.  document.write("<B>Public Key:<br></B>");  
  58.  document.write("m= "+m+"<br>");  
  59.  document.write("n= "+n+"<br>");  
  60.  document.write("----------------------------------<br>");  
  61.  document.write("<br>");  
  62.  document.write("<B>#示範加密過程#</B><br>");  
  63.  var text=20 //明文  
  64.  document.write("明文="+text+"<br>");  
  65.  //接下來, 計算 etext == text^r mod n, (0 <= b < n)  
  66.  var etext=Math.pow(text,r)%n;  
  67.  document.write("加密後的內容:"+etext+"<br>");  
  68.  document.write("<br>");  
  69.  document.write("#示範解密過程#<br>");  
  70.  //text=etext^m mod pq  
  71.  var text=Math.pow(etext,m)%n;  
  72.  document.write("解密後的內容:"+text+"<br>");  
  73. }  
  74. /**//*  
  75. JAVA下面的簡單實現  
  76. import java.math.BigInteger;  
  77. public class RSAAlgorithm {  
  78.     public static void main(String[] args) {  
  79.   BigInteger p = new BigInteger("5");    //P  
  80.   BigInteger q = new BigInteger("13");    //Q  
  81.         BigInteger n = new BigInteger("65");    //N  
  82.         BigInteger r = new BigInteger("7");  
  83.   
  84.         BigInteger biginteger = new BigInteger("20");  
  85.         BigInteger bigintegerbiginteger1 = biginteger.modPow(r, n);  
  86.         System.out.println("加密資料:" + biginteger1);  
  87.         BigInteger m = r.modInverse(new BigInteger("48"));  
  88.         System.out.println("m:" + m);  
  89.         BigInteger biginteger2 = biginteger1.modPow(m, n);  
  90.         System.out.println("解密資料:" + biginteger2);  
  91.     }  
  92. }  
  93. */  
  94. //-->  
  95. </SCRIPT>  
  96. </BODY>  
  97. </HTML> 
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.