Javascript C # real interactive DES encryption and decryption code

Source: Internet
Author: User

As required by the project, use JS encryption and submit it to C # for decryption,
After searching for the Internet for half a day, there are no versions available on the Internet, but no one can be found for mutual use.
Even the JS version cannot communicate with each other.
Because of the time relationship, there is no in-depth Encryption Code To download a JS version,
And rewrite it to the C # version.
In this way, the result of JS encryption can be decrypted by C #.
On the contrary, the C # encrypted result can also be decrypted by Js.
I will share it with you.
Note: Source code Created in vs. net2008, which contains the JS encryption and decryption functions and the C # encryption and decryption functions.

If you find any bugs or have any suggestions, contact me:
BTW: During the rewrite process, JavaScript supports shifts right with a symbol (>>> operator), while C # does not,
You have to convert int to uint, and then shift it to the right (> operator)
Operator does not support >>>
I had to write another function.
Package
The following is the previous Code. For more information, seeCopy codeThe Code is as follows: C # And javascript can be used to teach encryption and decryption functions.
Des encryption and decryption implemented in C:
Protected void page_load (Object sender, eventargs E)
{
String data = "testdes"; // data to be encrypted
Byte [] _ DATA = system. Text. encoding. Unicode. getbytes (data );
String keystr = "1234"; // key
String ivstr = "ABCD"; // initialization Vector
Byte [] Key = system. Text. encoding. Unicode. getbytes (keystr );
Byte [] IV = system. Text. encoding. Unicode. getbytes (ivstr );
String encdata = encrypt (data, key, IV );
String decdata = decrypt (encdata, key, IV );
Response. Write ("original data:" + Data + "<br/> ";
Response. Write ("encrypted data:" + encdata + "<br/> ";
Response. Write ("decrypted data:" + decdata + "<br/> ";

}

// EncryptionAlgorithm
Private Static string encrypt (string data, byte [] key, byte [] IV)
{
// If (null = Key | null = IV)
// Initkeyandiv ();

Byte [] _ DATA = system. Text. encoding. Unicode. getbytes (data );
Memorystream MS = new memorystream ();
Descryptoserviceprovider des = new descryptoserviceprovider ();
Des. Key = key;
Des. IV = IV;
// Generate the encryptor instance
Icryptotransform trans = des. createencryptor ();
Cryptostream cs = new cryptostream (MS, trans, cryptostreammode. Write );
// Encrypt the data and write the data into the stream
CS. Write (_ data, 0, _ data. Length );
CS. flushfinalblock ();
String result = convert. tobase64string (Ms. toarray ());
Ms. Close ();
CS. Close ();
Return result;
}

// decrypt
Private Static string decrypt (string data, byte [] key, byte [] IV)
{< br> byte [] _ DATA = convert. frombase64string (data);
descryptoserviceprovider des = new descryptoserviceprovider ();
memorystream MS = new memorystream ();
cryptostream cs = new cryptostream (MS, Des. createdecryptor (Key, IV), cryptostreammode. write);
CS. write (_ data, 0, _ data. length);
CS. flushfinalblock ();
string result = system. text. encoding. unicode. getstring (Ms. toarray ();
MS. close ();
CS. close ();
return result;
}

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.