In C # with des symmetric KEY,IV encryption, front-end crypto.js decryption

Source: Internet
Author: User
Tags decrypt

1. Server-side code

#region ======== Encryption ========
<summary>
Encryption
</summary>
<param name= "Text" ></param>
<returns></returns>
public static string Encrypt (String Text)
{
Return Encrypt (Text, Deskey);
}
<summary>
Encrypt data
</summary>
<param name= "Text" ></param>
<param name= "SKey" ></param>
<returns></returns>
public static string Encrypt (String Text, String SKey)
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider ();
Byte[] Inputbytearray;
Inputbytearray = Encoding.Default.GetBytes (Text);
Des. Key = ASCIIEncoding.ASCII.GetBytes (System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile ( SKey, "MD5"). Substring (0, 8));
DES.IV = ASCIIEncoding.ASCII.GetBytes (System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile (SKey, "MD5"). Substring (0, 8));
System.IO.MemoryStream ms = new System.IO.MemoryStream ();
CryptoStream cs = new CryptoStream (MS, Des. CreateEncryptor (), cryptostreammode.write);
Cs. Write (Inputbytearray, 0, inputbytearray.length);
Cs. FlushFinalBlock ();
StringBuilder ret = new StringBuilder ();
foreach (Byte b in Ms. ToArray ())
{
Ret. AppendFormat ("{0:x2}", b); {0:X2} uppercase hexadecimal
}
return ret. ToString ();
}

#endregion

#region ======== Decryption ========
<summary>
Decrypt
</summary>
<param name= "Text" ></param>
<returns></returns>
public static string Decrypt (String Text)
{
if (!string. IsNullOrEmpty (Text))
{
Return Decrypt (Text, Deskey);
}
Else
{
Return "";
}
}
<summary>
Decrypt data
</summary>
<param name= "Text" ></param>
<param name= "SKey" ></param>
<returns></returns>
public static string Decrypt (String Text, String SKey)
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider ();
int Len;
len = TEXT.LENGTH/2;
byte[] Inputbytearray = new Byte[len];
int x, I;
for (x = 0; x < len; + x)
{
i = Convert.ToInt32 (text.substring (x * 2, 2), 16);
INPUTBYTEARRAY[X] = (byte) i;
}
Des. Key = ASCIIEncoding.ASCII.GetBytes (System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile ( SKey, "MD5"). Substring (0, 8));
DES.IV = ASCIIEncoding.ASCII.GetBytes (System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile (SKey, "MD5"). Substring (0, 8));
System.IO.MemoryStream ms = new System.IO.MemoryStream ();
CryptoStream cs = new CryptoStream (MS, Des. CreateDecryptor (), cryptostreammode.write);
Cs. Write (Inputbytearray, 0, inputbytearray.length);
Cs. FlushFinalBlock ();
Return Encoding.Default.GetString (Ms. ToArray ());
}

#endregion

2. Front End

Encryptdes:function (INPUT,KEY,IV) {
var cipher = Crypto.createcipheriv (' des ', New buffer (key), new buffer (iv));
var buf1 = cipher.update (input, ' UTF8 ');
var buf2 = cipher.final ();
var result = new Buffer (buf1.length + buf2.length);
Buf1.copy (result);
Buf2.copy (result, buf1.length);

return result.tostring (' hex '). toUpperCase ();
},
Decryptdes:function (Encrypt_text,iv,key) {
var key = new Buffer (key);
var IV = new Buffer (Iv. iv:0);
var decipher = crypto.createdecipheriv (' Des ', key, iv);
Decipher.setautopadding (TRUE);
var txt = decipher.update (encrypt_text, ' hex ', ' UTF8 ');
TXT + = decipher.final (' UTF8 ');
return txt;
},

Str2bytes:function (str) {
var bytes = new Array ();
for (var i = 0; i < str.length; i++) {
var s = str.substr (i, 1);
var v = s.tostring (). charCodeAt ();
Bytes.push (v);
}
return bytes;
}
var crypto = require (' crypto ');
var temp=md5 (key). toString (). substring (0, 8). toUpperCase ();
var KEY = this. Str2bytes (temp); [1, 2, 3, 4, 5, 6, 7, 8];
var IV =key; [1, 2, 3, 4, 5, 6, 7, 8];

var encryted_content=this.encryptdes (content, KEY,IV);

var decryted_content=this.decryptdes (Encryted_content,iv,key);
var user_json=json.parse (decryted_content);



In C # with des symmetric KEY,IV encryption, front-end crypto.js decryption

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.