Cryptographic use of RSA in Javaweb

Source: Internet
Author: User

Encryption algorithm in the use of the site is very common, today, when the code to see the application of RSA encryption in our project, we understand a bit.

First of all, the RSA encryption algorithm principle, the RSA algorithm based on a very simple number theory fact: it is easy to multiply two large prime numbers, but it is extremely difficult to factorization the product, so it is possible to expose the product as an encryption key.

RSA's algorithm involves three parameters, N, E1, E2. which N is a two large prime number p, The product of Q, the number of bits occupied by the binary representation of N, is the so-called key length. E1 and E2 are a pair of related values, E1 can be arbitrarily taken, but require E1 with (p-1) * (q-1) coprime, then E2, request (E2*E1) mod ((p-1) * (q-1)) = 1. (N,E1), (N,E2) is the key pair. which (N,E1) is a public key(N,E2) is the private key.
= (Rsapublickey) Rsautil.getkeypair (). Getpublic (); // die String module = Rsap.getmodulus (). toString (+); // Public Key Index String empoent = Rsap.getpublicexponent (). toString (+), Request.setattribute ("M", module); Request.setattribute ("E", empoent);

This is the Java code, simply to pass the module and public key index back to the foreground page, according to my understanding module is the above said N,empoent is the above said E1.

Then the processing of JavaScript, first introduced 3 js file Rsa.js,bigint.js,barrett.js (online can be found anywhere)

function  Doencrypt () {  var result = $ ("#password"). Val (); Setmaxdigits ();    // 3 parameters, namely the Public key index, the private key index, module  (the general module is 1024 bits in length, the private key will not       be transmitted to the foreground)new rsakeypair ("12345", ""  , "12A3D32AD");   // generate ciphertext result = encryptedstring (key, encodeURIComponent (result));  $ ("#encrypt"). attr ("value", 1); $ ("#pwd"). attr ("value", result);}

Then the public key generated by the generated public key is passed back.

Rsautils.decryptbyprivatekey (MI, prikey);  

Then the ciphertext passed back through the private key to decrypt the line, the general process is this.

Cryptographic use of RSA in Javaweb

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.