我的c#encrypt / decrypt结构如下:
我将两个输入的输入Player
用作测试,但它没有返回正确的MD5哈希输出,而且我的Decrypt
函数也存在小问题byte[] toEncryptArray = Convert.FromBase64String (toDecrypt);
。
哈希输出错误和错误
playerID是:加密的玩家是:ZCKgr4veKtCDrD6mL + P6Yg ==
FormatException:长度无效。 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 资产/ APIConnector.cs:122)
关于我能做些什么的任何想法1)修复此错误和2)根据上面的ym结构得到正确的哈希输出?谢谢!
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);
}
答案 0 :(得分:0)
您应该将加密值传递给Decrypt,而不是名字。 Base64转换失败,因为firstname是明确的。
Debug.Log ("playerID is: " + firstName + " encrypted is: " + Encrypt(firstName));
Debug.Log ("password is: " + password + " decrypted is: " + Decrypt(firstName));
应该是
string enc = Encrypt(firstName);
Debug.Log ("playerID is: " + firstName + " encrypted is: " + enc);
Debug.Log ("password is: " + password + " decrypted is: " + Decrypt(enc));
答案 1 :(得分:0)
您的submit
方法没有解密您的加密字符串,它正在尝试解密您未加密的原始字符串。将submit
方法更改为以下方法可使程序顺利执行;
static void submit(){
string password = Encrypt(firstName);
Console.WriteLine ("playerID is: " + firstName +
" encrypted is: " + password);
Console.WriteLine ("password is: " + password +
" decrypted is: " + Decrypt(password));
}
另外,由于你实际上并不是MD5的密钥,你很幸运“SecretPassphrase”实际上是16个字符,这是密钥MD5产生的确切长度。更改为更短或更长的字符串将破坏程序。