Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save OMEGAYALFA/cf3847a61091fb6f7c1940b4816bf5b9 to your computer and use it in GitHub Desktop.
Save OMEGAYALFA/cf3847a61091fb6f7c1940b4816bf5b9 to your computer and use it in GitHub Desktop.
Html and C# scripts to decrypt an AES string (used to communicate between a html server and a flash movie)
//based on code sample from this MSDN article: http://msdn.microsoft.com/en-us/library/system.security.cryptography.aes(v=vs.100).aspx)
Func<byte[], byte[],byte[], string> decryptStringFromBytes_AES =
(cipherText, Key, 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");
// TDeclare the streams used
// to decrypt to an in memory
// array of bytes.
MemoryStream msDecrypt = null;
CryptoStream csDecrypt = null;
StreamReader srDecrypt = null;
// Declare the Aes object
// used to decrypt the data.
Aes aesAlg = null;
// Declare the string used to hold
// the decrypted text.
string plaintext = null;
try
{
// Create an Aes object
// with the specified key and IV.
aesAlg = Aes.Create();
aesAlg.Key = Key;
aesAlg.IV = IV;
// Create a decrytor to perform the stream transform.
ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);
// Create the streams used for decryption.
msDecrypt = new MemoryStream(cipherText);
csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read);
srDecrypt = new StreamReader(csDecrypt);
// Read the decrypted bytes from the decrypting stream
// and place them in a string.
plaintext = srDecrypt.ReadToEnd();
}
finally
{
// Clean things up.
// Close the streams.
if (srDecrypt != null)
srDecrypt.Close();
if (csDecrypt != null)
csDecrypt.Close();
if (msDecrypt != null)
msDecrypt.Close();
// Clear the Aes object.
if (aesAlg != null)
aesAlg.Clear();
}
return plaintext;
};
Func<string, byte[]> stringToByteArray =
(hex) => Enumerable.Range(0, hex.Length)
.Where(x => x % 2 == 0)
.Select(x => Convert.ToByte(hex.Substring(x, 2), 16))
.ToArray();
var topPanel = panel.add_Panel(true);
var decrypt_Source = topPanel.title("Decrypt").add_TextArea();
var decrypt_Result = decrypt_Source.insert_Right().add_TextArea();
decrypt_Source.onTextChange(
(text)=>{
try
{
//text2.set_Text(text);
var iv = text.subString(0,32);
var cipher_Text = text.subString(32);
var key = "0c9abc43b129a5a0c598b4752d4bde2e";
var decryptedText = decryptStringFromBytes_AES(stringToByteArray(cipher_Text),
stringToByteArray(key),
stringToByteArray(iv));
decrypt_Result.set_Text(decryptedText);
decrypt_Source.white();
}
catch(Exception ex)
{
ex.log();
decrypt_Source.pink();
}
});
var rawTextToDecrypt = "655f42836f84b716e6e00cc9829276c1b659672fb4b2509539806edec419fcef79c377847b33705a2e653697e76ed850f6433ac03224097e27311feab5ad9a0968bbb35e28de292d6118e9710651415b";
decrypt_Source.set_Text(rawTextToDecrypt);
return decrypt_Result.get_Text();
//using System.Security.Cryptography
//using System.IO;
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
</head>
<body>
<script src="http://crypto-js.googlecode.com/svn/tags/3.1/build/rollups/aes.js"></script>
<script src="http://crypto-js.googlecode.com/svn/tags/3.1/build/components/pad-nopadding.js"></script>
<script>
var key = CryptoJS.enc.Hex.parse('0c9abc43b129a5a0c598b4752d4bde2e');
var iv = CryptoJS.enc.Hex.parse('655f42836f84b716e6e00cc9829276c1');
var data = CryptoJS.enc.Hex.parse('b659672fb4b2509539806edec419fcef79c377847b33705a2e653697e76ed850f6433ac03224097e27311feab5ad9a0968bbb35e28de292d6118e9710651415b');
var encrypted = {};
encrypted.key=key;
encrypted.iv=iv;
encrypted.ciphertext = data;
var decrypted = CryptoJS.AES.decrypt(encrypted, key, { iv: iv, padding: CryptoJS.pad.NoPadding });
document.write("<br /> decrypted " + decrypted.toString(CryptoJS.enc.Utf8) );
document.write("<br /> dec3: " + CryptoJS.enc.Hex.stringify(decrypted));
</script>
</body>
</html>
var key = "0c9abc43b129a5a0c598b4752dAAAAAA";
var iv = "655f42836f84b716e6e00cc982AAAAAA";
var original_Text = "This is some text";
var encrypted_Text = original_Text.encrypt_AES(key, iv);
var decrypted_Text = encrypted_Text.decrypt_AES(key,iv);
return @"
original_Text:{0}
encrypted_Text:{1}
decrypted_Text: {2}".format(original_Text,
encrypted_Text.hexString(),
decrypted_Text)
.trim();
//O2File:_Extra_methods_To_Add_to_Main_CodeBase.cs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment