Javascript RSA Algorithm)

Source: Internet
Author: 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. <Body>
  11. <Script language = "JavaScript">
  12. <! --
  13. // ================================================ ======================================
  14. // Simple implementation of the Javascript RSA Algorithm
  15. // Know bugs:
  16. // 1. Math. Pow has a computing accuracy problem. Therefore, you cannot select a large prime number. Otherwise, it will overflow.
  17. // By midea0978
  18. // Date: 2005-08-10
  19. // References
  20. // 1. RSA Algorithm Research http://www.cnblogs.com/midea0978/articles/65244.html
  21. // 2. PKCS #1: RSA cryptography Specifications version 2.0
  22. // Http://www.faqs.org/rfcs/rfc2437.html
  23. // ================================================ ======================================
  24. Rsaalgorithm ()
  25. Function rsaalgorithm (){
  26. // 1. Select two simple prime numbers p and q
  27. VaR P = 5
  28. VaR q = 13
  29. VaR n = p * q
  30. // Randomly select the remainder R to ensure the interconnectivity between R and P-1) * (q-1) = 48
  31. VaR r = 7
  32. // Obtain that the private key is P, Q, R.
  33. Document. Write ("---------------------------------- <br> ");
  34. Document. Write ("<B> private key: </B> <br> ");
  35. Document. Write ("P =" + P + "<br> ");
  36. Document. Write ("q =" + q + "<br> ");
  37. Document. Write ("r =" + R + "<br> ");
  38. Document. Write ("---------------------------------- <br> ");
  39. // 2. Calculate the Public Key
  40. // Find M, ensure Rm = 1 Mod (p-1) * (q-1)
  41. VaR num = 0
  42. For (A = 0; A <500; A ++ ){
  43. If (R * a % (p-1) * (q-1) = 1 ){
  44. Num =;
  45. Break;
  46. }
  47. }
  48. VaR M = num
  49. If (M> 0 ){
  50. Document. write ("<br> find M =" + num + ", calculate the remainder =" + (Num * r) % (p-1) * (q-1) + ", succeeded! <Br> ")
  51. }
  52. Else {
  53. Document. Write ("the M meeting the condition cannot be found. Please adjust the parameter ");
  54. }
  55. Document. Write ("---------------------------------- <br> ");
  56. Document. Write ("<B> Public Key: <br> </B> ");
  57. Document. Write ("m =" + M + "<br> ");
  58. Document. Write ("n =" + N + "<br> ");
  59. Document. Write ("---------------------------------- <br> ");
  60. Document. Write ("<br> ");
  61. Document. Write ("<B> # demonstrate the encryption process # </B> <br> ");
  62. VaR text = 20 // plaintext
  63. Document. Write ("plaintext =" + TEXT + "<br> ");
  64. // Next, calculate etext = text ^ r mod N, (0 <= B <n)
  65. VaR etext = math. Pow (text, R) % N;
  66. Document. Write ("encrypted content:" + etext + "<br> ");
  67. Document. Write ("<br> ");
  68. Document. Write ("# demonstrate the decryption process # <br> ");
  69. // Text = etext ^ m mod PQ
  70. VaR text = math. Pow (etext, m) % N;
  71. Document. Write ("decrypted content:" + TEXT + "<br> ");
  72. }
  73. /**//*
  74. Java
  75. Import java. Math. biginteger;
  76. Public class rsaalgorithm {
  77. Public static void main (string [] ARGs ){
  78. Biginteger P = new biginteger ("5"); // P
  79. Biginteger q = new biginteger ("13"); // Q
  80. Biginteger n = new biginteger ("65"); // n
  81. Biginteger r = new biginteger ("7 ");
  82. Biginteger = new biginteger ("20 ");
  83. Biginteger bigintegerbiginteger1 = biginteger. modpow (R, N );
  84. System. Out. println ("encrypted data:" + biginteger1 );
  85. Biginteger M = R. modinverse (New biginteger ("48 "));
  86. System. Out. println ("M:" + M );
  87. Biginteger biginteger2 = biginteger1.modpow (m, n );
  88. System. Out. println ("decrypt data:" + biginteger2 );
  89. }
  90. }
  91. */
  92. // -->
  93. </SCRIPT>
  94. </Body>
  95. </Html>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.