My structure for the c# encrypt/decrypt is as follows:
- Cipher: Rijndael (AES)
- Block Size: 128 bits (16 bytes)
- Mode: CBC (Cipher Block Chaining)
- Key: MD5 hash passphrase
- IV: Same as the key
- Data Encoding: Base64 Character
- UTF-8 Encoding
I'm using input Player
for both inputs as a test, however it is not returning the correct MD5 hash output, and also there's a small issue with my Decrypt
function for byte[] toEncryptArray = Convert.FromBase64String (toDecrypt);
.
Incorrect hash output and Error
playerID is: Player encrypted is: ZCKgr4veKtCDrD6mL+P6Yg==
FormatException: Invalid length. System.Convert.FromBase64String (System.String s) (at /Users/builduser/buildslave/monoAndRuntimeClassLibs/build/mcs/class/corlib/System/Convert.cs:146) APIConnector.Decrypt (System.String toDecrypt) (at Assets/APIConnector.cs:122)
Any ideas on what I can do to 1) fix this error and 2) get my hash output correct based on ym structure above? Thanks!
void submit(){
Debug.Log ("playerID is: " + firstName + " encrypted is: " + Encrypt(firstName));
Debug.Log ("password is: " + password + " decrypted is: " + Decrypt(password));
}
public static string Encrypt (string toEncrypt)
{
byte[] keyArray = UTF8Encoding.UTF8.GetBytes ("SecretPassphrase");
// 256-AES key
byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes (toEncrypt);
RijndaelManaged rDel = new RijndaelManaged ();
rDel.Key = keyArray;
rDel.IV = keyArray;
rDel.Mode = CipherMode.CBC;
rDel.BlockSize = 128;
// http://msdn.microsoft.com/en-us/library/system.security.cryptography.ciphermode.aspx
rDel.Padding = PaddingMode.PKCS7;
// better lang support
ICryptoTransform cTransform = rDel.CreateEncryptor ();
byte[] resultArray = cTransform.TransformFinalBlock (toEncryptArray, 0, toEncryptArray.Length);
return Convert.ToBase64String (resultArray, 0, resultArray.Length);
}
//
public static string Decrypt (string toDecrypt)
{
byte[] keyArray = UTF8Encoding.UTF8.GetBytes ("SecretPassphrase");
// AES-256 key
byte[] toEncryptArray = Convert.FromBase64String (toDecrypt);
RijndaelManaged rDel = new RijndaelManaged ();
rDel.Key = keyArray;
rDel.IV = keyArray;
rDel.Mode = CipherMode.CBC;
rDel.BlockSize = 128;
// http://msdn.microsoft.com/en-us/library/system.security.cryptography.ciphermode.aspx
rDel.Padding = PaddingMode.PKCS7;
// better lang support
ICryptoTransform cTransform = rDel.CreateDecryptor ();
byte[] resultArray = cTransform.TransformFinalBlock (toEncryptArray, 0, toEncryptArray.Length);
return UTF8Encoding.UTF8.GetString (resultArray);
}