Skip to content

Instantly share code, notes, and snippets.

@gfhuertac
Created March 6, 2015 20:37
Show Gist options
  • Save gfhuertac/3b5c83f4a686adc53d5a to your computer and use it in GitHub Desktop.
Save gfhuertac/3b5c83f4a686adc53d5a to your computer and use it in GitHub Desktop.
AES decryption
using System;
using System.Text;
using System.Security.Cryptography;
/**
USO:
Crypto c = new Crypto(null);
string rawToken = c.DecryptFromBase64(token);
**/
namespace biometrika
{
public class Crypto
{
private ICryptoTransform rijndaelDecryptor;
private static byte[] rawSecretKey = {0x24, 0xed, 0xf3, 0x75,
0x97, 0xa4, 0x6b, 0xed, 0xa1, 0x90,
0x97, 0x5b, 0xb6, 0x2e, 0xcf, 0x70};
public Crypto(string passphrase)
{
if (passphrase == null) {
passphrase = "--__b10m3tr1k4__--";
}
byte[] passwordKey = encodeDigest(passphrase);
RijndaelManaged rijndael = new RijndaelManaged();
rijndaelDecryptor = rijndael.CreateDecryptor(passwordKey, rawSecretKey);
}
public string Decrypt(byte[] encryptedData)
{
byte[] newClearData = rijndaelDecryptor.TransformFinalBlock(encryptedData, 0, encryptedData.Length);
return Encoding.ASCII.GetString(newClearData);
}
public string DecryptFromBase64(string encryptedBase64)
{
return Decrypt(Convert.FromBase64String(encryptedBase64));
}
private byte[] encodeDigest(string text)
{
MD5CryptoServiceProvider x = new System.Security.Cryptography.MD5CryptoServiceProvider();
byte[] data = Encoding.ASCII.GetBytes(text);
return x.ComputeHash(data);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment