javascript <script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/rollups/aes.js "></script> <script type="text/javascript"> function SubmitsEncry() { debugger; var txtUserName = "Please enter UserName"; var key = CryptoJS.enc.Utf8.parse('8080808080808080'); var iv = CryptoJS.enc.Utf8.parse('8080808080808080'); var encrypted = CryptoJS.AES.encrypt(CryptoJS.enc.Utf8.parse(txtUserName), key, { keySize: 128 / 8, iv: iv, mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7 }); var decrypted = CryptoJS.AES.decrypt(encrypted, key, { keySize: 128 / 8, iv: iv, mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7 }); alert(decrypted.toString(CryptoJS.enc.Utf8)); } SubmitsEncry(); </script> c# public ActionResult Index() { string s = "The brown fox jumped over the green frog"; //string k = "urieurut"; var encry = EncryptStringAES(s); var sss = DecryptStringAES(encry); return View(); } public static string DecryptStringAES(string cipherText) { var keybytes = Encoding.UTF8.GetBytes("8080808080808080"); var iv = Encoding.UTF8.GetBytes("8080808080808080"); var encrypted = Convert.FromBase64String(cipherText); var decriptedFromJavascript = DecryptStringFromBytes(encrypted, keybytes, iv); return string.Format(decriptedFromJavascript); } private static string DecryptStringFromBytes(byte[] cipherText, byte[] key, byte[] iv) { // Check arguments. if (cipherText == null || cipherText.Length <= 0) { throw new ArgumentNullException("cipherText"); } if (key == null || key.Length <= 0) { throw new ArgumentNullException("key"); } if (iv == null || iv.Length <= 0) { throw new ArgumentNullException("key"); } // Declare the string used to hold // the decrypted text. string plaintext = null; // Create an RijndaelManaged object // with the specified key and IV. using (var rijAlg = new RijndaelManaged()) { //Settings rijAlg.Mode = CipherMode.CBC; rijAlg.Padding = PaddingMode.PKCS7; rijAlg.FeedbackSize = 128; rijAlg.Key = key; rijAlg.IV = iv; // Create a decrytor to perform the stream transform. var decryptor = rijAlg.CreateDecryptor(rijAlg.Key, rijAlg.IV); try { // Create the streams used for decryption. using (var msDecrypt = new MemoryStream(cipherText)) { using (var csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read)) { using (var srDecrypt = new StreamReader(csDecrypt)) { // Read the decrypted bytes from the decrypting stream // and place them in a string. plaintext = srDecrypt.ReadToEnd(); } } } } catch { plaintext = "keyError"; } } return plaintext; } public static string EncryptStringAES(string plainText) { var keybytes = Encoding.UTF8.GetBytes("8080808080808080"); var iv = Encoding.UTF8.GetBytes("8080808080808080"); var encryoFromJavascript = EncryptStringToBytes(plainText, keybytes, iv); return Convert.ToBase64String(encryoFromJavascript); } private static byte[] EncryptStringToBytes(string plainText, byte[] key, byte[] iv) { // Check arguments. if (plainText == null || plainText.Length <= 0) { throw new ArgumentNullException("plainText"); } if (key == null || key.Length <= 0) { throw new ArgumentNullException("key"); } if (iv == null || iv.Length <= 0) { throw new ArgumentNullException("key"); } byte[] encrypted; // Create a RijndaelManaged object // with the specified key and IV. using (var rijAlg = new RijndaelManaged()) { rijAlg.Mode = CipherMode.CBC; rijAlg.Padding = PaddingMode.PKCS7; rijAlg.FeedbackSize = 128; rijAlg.Key = key; rijAlg.IV = iv; // Create a decrytor to perform the stream transform. var encryptor = rijAlg.CreateEncryptor(rijAlg.Key, rijAlg.IV); // Create the streams used for encryption. using (var msEncrypt = new MemoryStream()) { using (var csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write)) { using (var swEncrypt = new StreamWriter(csEncrypt)) { //Write all data to the stream. swEncrypt.Write(plainText); } encrypted = msEncrypt.ToArray(); } } } // Return the encrypted bytes from the memory stream. return encrypted; }
Saturday, March 28, 2015
Encryption and Decryption in javascript and c#
Subscribe to:
Post Comments (Atom)
Muchas gracias por la ayuda, me sirvió al 100%
ReplyDelete