Cryptojs and C#aes plus decrypt each other

Source: Internet
Author: User
Tags pkcs7

CRYPTOJS:

Https://code.google.com/archive/p/crypto-js/downloads

Page JS Reference:

    <type= "Text/javascript"  src= "/content/plugin/cryptojsv3.1.2/ Components/core-min.js "></script>    <  type = "Text/javascript" src = "/content/plugin/cryptojsv3.1.2/rollups/aes.js" ></ Script >

JS-side AES Encryption and decryption:

1Com.str = {2_key: "12345678900000001234567890000000",//32 Guests3_iv: "1234567890000000",//16 Guests4         /**************************************************************5 * String Encryption6 * Str: A string that needs to be encrypted7         ****************************************************************/8Encrypt:function(str) {9             varKey = CryptoJS.enc.Utf8.parse ( This. _key); Ten             varIV = CryptoJS.enc.Utf8.parse ( This. _iv); One  A             varencrypted = ' '; -  -             varSRCs =CryptoJS.enc.Utf8.parse (str); theencrypted =CryptoJS.AES.encrypt (SRCs, key, { - Iv:iv, - Mode:CryptoJS.mode.CBC, - PADDING:CRYPTOJS.PAD.PKCS7 +             }); -  +             returnencrypted.ciphertext.toString (); A         }, at  -         /************************************************************** - * String Decryption - * Str: A string to decrypt -         ****************************************************************/ -Decrypt:function(str) { in             varKey = CryptoJS.enc.Utf8.parse ( This. _key); -             varIV = CryptoJS.enc.Utf8.parse ( This. _iv); to             varEncryptedhexstr =CryptoJS.enc.Hex.parse (str); +             varSRCs =CryptoJS.enc.Base64.stringify (ENCRYPTEDHEXSTR); -             varDecrypt =CryptoJS.AES.decrypt (SRCs, key, { the Iv:iv, * Mode:CryptoJS.mode.CBC, $ PADDING:CRYPTOJS.PAD.PKCS7Panax Notoginseng             }); -             varDecryptedstr =decrypt.tostring (CRYPTOJS.ENC.UTF8); the             returndecryptedstr.tostring (); +         } A}

C # AES Encryption decryption:

Const string AES_IV = "1234567890000000";//16 bit

/// <summary> ///AES Encryption Algorithm/// </summary> /// <param name= "Input" >PlainText String</param> /// <param name= "key" >Key (32-bit)</param> /// <returns>string</returns> Public Static stringEncryptbyaes (stringInputstringkey) { byte[] keybytes = Encoding.UTF8.GetBytes (key. Substring (0, +)); using(Aescryptoserviceprovider Aesalg =NewAescryptoserviceprovider ()) {Aesalg.key=keybytes; Aesalg.iv= Encoding.UTF8.GetBytes (Aes_iv. Substring (0, -)); ICryptoTransform encryptor=Aesalg.createencryptor (Aesalg.key, AESALG.IV); using(MemoryStream msencrypt =NewMemoryStream ()) { using(CryptoStream csencrypt =NewCryptoStream (Msencrypt, Encryptor, CryptoStreamMode.Write)) { using(StreamWriter Swencrypt =NewStreamWriter (Csencrypt)) {swencrypt.write (input); } byte[] bytes =Msencrypt.toarray (); returnbytearraytohexstring (bytes); } } } } /// <summary> ///AES Decryption/// </summary> /// <param name= "Input" >cipher text Section array</param> /// <param name= "key" >Key (32-bit)</param> /// <returns>returns the decrypted string</returns> Public Static stringDecryptbyaes (stringInputstringkey) { byte[] Inputbytes =Hexstringtobytearray (input); byte[] keybytes = Encoding.UTF8.GetBytes (key. Substring (0, +)); using(Aescryptoserviceprovider Aesalg =NewAescryptoserviceprovider ()) {Aesalg.key=keybytes; Aesalg.iv= Encoding.UTF8.GetBytes (Aes_iv. Substring (0, -)); ICryptoTransform decryptor=Aesalg.createdecryptor (Aesalg.key, AESALG.IV); using(MemoryStream msencrypt =NewMemoryStream (inputbytes)) { using(CryptoStream csencrypt =NewCryptoStream (Msencrypt, Decryptor, CryptoStreamMode.Read)) { using(StreamReader Srencrypt =NewStreamReader (Csencrypt)) { returnSrencrypt.readtoend (); } } } } } /// <summary> ///converts the specified 16 binary string to a byte array/// </summary> /// <param name= "S" >16 binary strings (such as: "7F 2C 4 A" or "7f2c4a" can be)</param> /// <returns>byte array corresponding to the 16 binary string</returns> Public Static byte[] Hexstringtobytearray (strings) {s= S.replace (" ",""); byte[] buffer =New byte[S.length/2]; for(inti =0; i < s.length; i + =2) Buffer[i/2] = (byte) Convert.tobyte (s.substring (i,2), -); returnbuffer; } /// <summary> ///converts a byte array to a formatted 16 binary string/// </summary> /// <param name= "Data" >byte array</param> /// <returns>formatted 16 binary string</returns> Public Static stringBytearraytohexstring (byte[] data) {StringBuilder SB=NewStringBuilder (data. Length *3); foreach(byteBinchdata) { //16 binary digitsSb. Append (convert.tostring (b, -). PadLeft (2,'0')); //16 binary digits separated by a space//sb. Append (convert.tostring (b, 16). PadLeft (2, ' 0 '). PadRight (3, ")); } returnsb. ToString (). ToUpper (); }

Usage:

 com.str.Encrypt ("2017-05") //  Result: 68f4a7903a9fe6085d2301ac68cc039c  com.str.decrypt ( " 68f4a7903a9fe6085d2301ac68cc039c ") //  Result: 2017-05 
// Encrypt string str1 = Encrypt.encryptbyaes ("2017-05"" 12345678900000001234567890000000"); // decryption string str2 = Encrypt.decryptbyaes ("68f4a7903a9fe6085d2301ac68cc039c" " 12345678900000001234567890000000 ");

Cryptojs and C#aes plus decrypt each other

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.