Created
April 14, 2020 07:37
-
-
Save CoreProgramm/686446caf1e7388aae978afa29f19960 to your computer and use it in GitHub Desktop.
Encrypt in JavaScript and Decrypt in C# With AES Algorithm in ASP.Net MVC
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class AESEncryption | |
{ | |
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; | |
} | |
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; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment