【发布时间】:2017-05-12 20:13:37
【问题描述】:
我在 Visual Studio 2013 中有一个应用程序,带有密码加密方法,我需要将其迁移到 Xamarin Android。
我遇到的问题是 Xamarin 中的加密方法提供了不同的加密字符串。
区别在于GetBytes方法。
这是我的代码的一部分。
public static string Encript(string ptexto, string pClave)
{
return Encript2(ptexto, pClave + "pass75dc@avz10", "s@lAvz", "MD5", 1, "@1B2c3D4e5F6g7H8", 128);
}
private static string Encript2(string textoQueEncriptaremos, string passBase, string saltValue, string hashAlgorithm, int passwordIterations, string initVector, int keySize)
{
byte[] initVectorBytes = Encoding.ASCII.GetBytes(initVector);
byte[] saltValueBytes = Encoding.ASCII.GetBytes(saltValue);
byte[] plainTextBytes = Encoding.UTF8.GetBytes(textoQueEncriptaremos);
PasswordDeriveBytes password = new PasswordDeriveBytes(passBase,saltValueBytes, hashAlgorithm, passwordIterations);
byte[] keyBytes = password.GetBytes(keySize / 8);
RijndaelManaged symmetricKey = new RijndaelManaged()
{
Mode = CipherMode.CBC
};
ICryptoTransform encryptor = symmetricKey.CreateEncryptor(keyBytes,initVectorBytes);
MemoryStream memoryStream = new MemoryStream();
CryptoStream cryptoStream = new CryptoStream(memoryStream, encryptor,CryptoStreamMode.Write);
cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length);
cryptoStream.FlushFinalBlock();
byte[] cipherTextBytes = memoryStream.ToArray();
memoryStream.Close();
cryptoStream.Close();
string cipherText = Convert.ToBase64String(cipherTextBytes);
return cipherText;
}
与众不同的地方是
byte[] keyBytes = password.GetBytes(keySize / 8);
我无法在我的 c# 应用程序中更改我的加密,有没有办法在 Xamarin 中获得相同的结果?
【问题讨论】:
标签: c# android encryption xamarin