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