【问题标题】:xamarin Android encryption GetBytes Method difference with C#xamarin Android 加密 GetBytes 方法与 C# 的区别
【发布时间】: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


    【解决方案1】:

    从您可以查看的文档中,您得到不同的字节数组是正常的,GetByte() 总是返回 RANDOM 键字节。 more

    GetBytes(Int32) : Byte[]
    

    返回伪随机密钥字节。

    【讨论】:

      猜你喜欢
      • 2016-09-12
      • 2013-06-04
      • 2011-05-23
      • 1970-01-01
      • 2022-10-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-29
      相关资源
      最近更新 更多