Skip to content

Instantly share code, notes, and snippets.

@alexandre-tobia
Last active January 25, 2018 15:03
Show Gist options
  • Save alexandre-tobia/2a4599d97066d72a74e9d15cc5622b89 to your computer and use it in GitHub Desktop.
Save alexandre-tobia/2a4599d97066d72a74e9d15cc5622b89 to your computer and use it in GitHub Desktop.
```  
private readonly AesCryptoServiceProvider AES = new AesCryptoServiceProvider();
public CryptClass(string EncryptKey)
{
// Initialize the crypto provider.
AES.Mode = CipherMode.ECB;
AES.Key = TruncateHash(EncryptKey, AES.KeySize / 8);
AES.IV = TruncateHash("", AES.BlockSize / 8);
AES.Padding = PaddingMode.PKCS7;
}
public string Encrypt(string plaintext)
{
plaintext += "." + DateTime.Now.AddMinutes(5.0).Ticks.ToString();
plaintext += "." + GetStringHash(plaintext);
Debug.WriteLine(plaintext);
// Convert the plaintext string to a byte array.
byte[] plaintextBytes = System.Text.Encoding.Unicode.GetBytes(plaintext);
// Create the stream.
System.IO.MemoryStream ms = new System.IO.MemoryStream();
// Create the encoder to write to the stream.
CryptoStream encStream = new CryptoStream(ms, AES.CreateEncryptor(), CryptoStreamMode.Write);
// Use the crypto stream to write the byte array to the stream.
encStream.Write(plaintextBytes, 0, plaintextBytes.Length);
encStream.FlushFinalBlock();
// Convert the encrypted stream to a printable string.
return Convert.ToBase64String(ms.ToArray());
}
public string Decrypt(string encryptedtext)
{
return Decrypt(encryptedtext, false);
}
/// <summary>
/// Decrypts the specified encryptedtext.
/// </summary>
/// <param name="encryptedtext">The encryptedtext.</param>
/// <param name="b24">if set to <c>true</c> add 24 hours of validity for the token.</param>
/// <returns></returns>
/// <exception cref="System.Exception">
/// Invalid hash !!!!
/// or
/// This Token is no longer valid !!!!
/// </exception>
public string Decrypt(string encryptedtext, bool b24 = false)
{
// Convert the encrypted text string to a byte array.
byte[] encryptedBytes = Convert.FromBase64String(encryptedtext);
// Create the stream.
System.IO.MemoryStream ms = new System.IO.MemoryStream();
// Create the decoder to write to the stream.
CryptoStream decStream = new CryptoStream(ms, AES.CreateDecryptor(), CryptoStreamMode.Write);
// Use the crypto stream to write the byte array to the stream.
decStream.Write(encryptedBytes, 0, encryptedBytes.Length);
decStream.FlushFinalBlock();
// Convert the plaintext stream to a string.
string plaintext = System.Text.Encoding.Unicode.GetString(ms.ToArray());
//
int idx = plaintext.LastIndexOf(".");
string hash = plaintext.Substring(idx + 1);
plaintext = plaintext.Substring(0, idx);
if (hash != GetStringHash(plaintext))
{
throw new Exception("Invalid hash !!!!");
}
idx = plaintext.LastIndexOf(".");
string ValidityEndDate = plaintext.Substring(idx + 1);
DateTime dtEnd = new System.DateTime(long.Parse(ValidityEndDate));
if (b24) { dtEnd = dtEnd.AddDays(1.0); }
if (dtEnd < System.DateTime.Now) { throw new Exception("This Token is no longer valid !!!!"); }
plaintext = plaintext.Substring(0, idx);
return plaintext;
}
private byte[] TruncateHash(string key, int length)
{
byte[] hash = GetHash(key);
// Truncate or pad the hash.
Array.Resize(ref hash, length);
return hash;
}
private byte[] GetHash(string key)
{
byte[] keyBytes = System.Text.Encoding.ASCII.GetBytes(key);
return keyBytes;
}
private string GetStringHash(string key)
{
string retHash = Convert.ToBase64String(GetHash(key));
return retHash;
}
       ```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment