RSA Front desk JS encryption, background C # decryption

Source: Internet
Author: User
Tags modulus asymmetric encryption

First, demand:

In order to secure, the project in the foreground login password needs to be encrypted to the background, the background C # decryption login password.

Second, the solution

Using the asymmetric encryption algorithm RSA to achieve the purpose of the front desk landing page A load will send an AJAX request for the background generated public key, for the foreground encryption, the user click on the log on the encryption process and submit encrypted data to the background, the background C # language using encapsulated RSA algorithm tool for password decryption.

The following is the cryptographic code for the C#RSA algorithm:

        private static RSAParameters rsap = new RSAParameters () {modulus = convert.frombase64string (@ "Sb6+4rto                                                 2syeiz8kjggm647pim+djkwvspnwcq01d2cwpjigv2c41h39fjyuzgakzrifjsvubpg4y/pfehun+ Lackst6mu7qcbs7lzub8v97xz5fddpaq/gwxo9mrimmfdmw7z88wruklgtvwkqyspbemc22rjua1utr3azae7u= "), Exponent = Conve Rt. FromBase64String (@ "Aqab"), P = convert.frombase64string (@ "8yucfvcufr3z2ldawhauo4r3na3wzqhab3j7axv/rj9uexqwwzog8 ibuzv2fuhmxjnfxyrrsqywwdpxee6olww== "), Q = convert.frombase64string (@" UW6NLPZKBL4DO7K4RUDCSZ9UIVQNU0CBM7JVUYGWJ ts+pu1ho5s0auuekqy5al6p4xifjwiccslvpxsluwislw== "), DP = convert.frombase64string (@" Rdsf0ad4i3e8hncxgn28nlzgj8hu 6ilwocgxz+4c+/ob++cgo5coqvxo6xwrwhskca2b6av4fazcnzymazl9lw== "), DQ = convert.frombase64string (@" DVVT+FKMIs9IZEP jp+drktm94whgcnyuxp9aii2ixrhqyfvhbyjg18dk54lypbectlu2+gj1ngyffxxi/epldw== "), Inverseq = Convert.FromBase64Strin G (@ "Z8qry0+yyfzfnfpmtltumpyycxubk+gpwnfp2hoytabya/h7g4dcre6io9uzkgw4pab5k75mjwdbgvib5ngfiq== "), D = convert.frombase64string (@" W1ZWo                                           Lelwajnlho2ydfhizlakx1y/reb/jvuqysyu96salvnpitn0qoucar/+y3edrx+ewypupbz48v0c2vgydhwib        Ribseyn+vhounj319r5kuz8wlfw/w6/6bsclqbwq8odsj1ckwx/eejh4iipqj8hbtsmot0anqhp/jdybe= ")}; private static string Rsaencrypt (string publickey, string content) {RSACryptoServiceProvider RSA = new RSACRYP            Toserviceprovider (1024x768);//RSACryptoServiceProvider (); Rsa. ImportParameters (RSAP);//import public key byte[] result = RSA.            Encrypt (Encoding.UTF8.GetBytes (content), false);        return convert.tobase64string (Result); } private static string Rsadecrypt (string Privatekey, string content) {RSACryptoServiceProvider RSA = n            EW RSACryptoServiceProvider (1024); Rsa.            ImportParameters (RSAP); var result = RSA.            Decrypt (convert.frombase64string (content), false); RetUrn Encoding.UTF8.GetString (result); }

The public key information that is uploaded to the foreground in the background is:

rSAP. Exponent.hexencode ();

rSAP. Modulus.hexencode ();

Hexencode () is a static extension of the byte array type, and only converts each byte to the corresponding 16 binary, the code is as follows:

public static string Tohexencode (this byte[] bytes) {            return bitconverter.tostring (bytes). Replace ("-", String. Empty);        }

Using the above static object rsap, the public key information passed through the Hexencode to the foreground is:

1.0100012. B01ebee2bb4edac61e219f2424618ceb8ecf226f9d264c2f48f356710d350f67303e3206576738d61dfd16362ece000aceb2058d2bee0691b8cbf3c51 07B
8df8b69c912b7a314eea71bb3b973b9bf15f7b5d9e5f75d3daabf1965e8f66ac830c143316ef3f3c5abba42c64efc24ab248f05e99cdb6ae3b9ad6e4d 1DD
Acda7bb5

JS encryption at the front desk, from the online raid RSA JS implementation, the page should be added three JS files: Bigint.js,barrett.js,rsa.js, the following is attached to the three JS library. For example, the encryption Zjt,js code is as follows:

Setmaxdigits (n); var key = new Rsakeypair (' 010001 ', "", ' b01ebee2bb4edac61e219f2424618ceb8ecf226f9d264c2f48f356710d350f67303e3206576738d61dfd163
62ece000aceb2058d2bee0691b8cbf3c5107b8df8b69c912b7a314eea71bb3b973b9bf15f7b5d9e5f75d3daabf1965e8f66ac830c143316ef3f3c5abb A42c64
Efc24ab248f05e99cdb6ae3b9ad6e4d1ddacda7bb5 ') var res = encryptedstring (key, ' ZJT ')

Get res as the result of encryption, used to upload to the background, the res result is:

9839c4f8cf40e5cd30be61a1e215c571424fca76108aacecc5ca45f9cdb6c207f4477eff32d41799babe915fd8806fce18ad38c4732d96008c5064849 40da7
A9b47cb7bc40f8bb70e079f8ae91e2b46d8d5d9bd1fa6ed637176d67a2a437ac801cca25f7e7c33b482d36f980da52982d65e6e388f2b2c47c7fcdfa7 97802
F2a5

The background receives the second encrypted string, turns into the byte first, here again uses a static extension hexdecode, turns into the byte, uses the RSACryptoServiceProvider object to import the private key to decode:

public static byte[] Hexdecode (This string me) {     string s = Me. Length% 2 = = 1? "0" + me:me;     byte[] data = new BYTE[S.LENGTH/2];     for (int i = 0; i < data. Length; i++)         {            data[i] = byte. Parse (s.substring (i + I, 2), numberstyles.hexnumber);         }         return data;  } public static string GetString (this byte[] content, Encoding Encoding = null) {     Encoding = Encoding?? Encoding.ascii;     return encoding. GetString (content);}

The decryption process is as follows:

var pass = " 9839c4f8cf40e5cd30be61a1e215c571424fca76108aacecc5ca45f9cdb6c207f4477eff32d41799babe915fd8806fce18ad38c4732d96008c50648494
0da7a9b47cb7bc40f8bb70e079f8ae91e2b46d8d5d9bd1fa6ed637176d67a2a437ac801cca25f7e7c33b482d36f980da52982d65e6e388f2b2c47c7fc dfa797802
F2a5 "; byte[] data = pass. Hexdecode (); RSACryptoServiceProvider RSA = new RSACryptoServiceProvider (1024x768); RSA. ImportParameters (RSAP); var res = RSA. Decrypt (data, false); Console.WriteLine (Res. GetString ());//Use the above static extension, after decryption result is ZJT

The implementation of the RSA algorithm JS

RSA Front desk JS encryption, background C # decryption

Related Article

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.