Created
March 6, 2015 20:37
-
-
Save gfhuertac/3b5c83f4a686adc53d5a to your computer and use it in GitHub Desktop.
AES decryption
This file contains hidden or 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
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