0% found this document useful (0 votes)
33 views2 pages

AES Encryption and Decryption Example

Uploaded by

jgd.lok
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views2 pages

AES Encryption and Decryption Example

Uploaded by

jgd.lok
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

using System;

using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];

public class AESEncrytDecry


{
public static string key_Val = "k2hLr4X0ozNyZByj5DT66edtCEee1x+6";

public static string DecryptAES(string encryptedValue)


{
if ([Link](encryptedValue))
{
return encryptedValue;
}

if ([Link](0, 2) == "\\x")
{
encryptedValue = [Link](0, 2);
}

byte[] key = [Link](key_Val);


using (AesCryptoServiceProvider aesAlg = new AesCryptoServiceProvider())
{
[Link] = key;
[Link] = [Link];
ICryptoTransform decryptor = [Link]([Link],
[Link]);
MemoryStream msDecrypt = new
MemoryStream(HexToByteArray(encryptedValue));
CryptoStream csDecrypt = new
[Link](msDecrypt, decryptor,
[Link]);

StreamReader srDecrypt = new [Link](csDecrypt);

return [Link]();

}
}

public static string Encrypt(string plainText)


{
if ([Link](plainText))
{
return plainText;
}

byte[] Key = [Link](key_Val);


using (AesCryptoServiceProvider aesAlg = new AesCryptoServiceProvider())
{
[Link] = Key;
[Link] = [Link];

ICryptoTransform encryptor = [Link]([Link],


[Link]);
using (MemoryStream outputStream = new MemoryStream())
{
using (CryptoStream csEncrypt = new CryptoStream(outputStream,
encryptor, [Link]))
{
using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
{
[Link](plainText);
}
}
return "\\x" + ByteArrayToString([Link]());

}
}
}

public static string ByteArrayToString(byte[] ba)


{
StringBuilder hex = new StringBuilder([Link] * 2);
foreach (byte b in ba)
[Link]("{0:x2}", b);
return [Link]();
}

private static byte[] HexToByteArray(string hex)


{
int NumberChars = [Link];
byte[] bytes = new byte[NumberChars / 2];
int iPos = 0;
for (int i = 0; i <= NumberChars - 1; i += 2)
{
bytes[iPos] = [Link]([Link](i, 2), 16);
iPos += 1;
}
return bytes;
}

You might also like