2016-12-28 20 views
0

我有一个应用程序在Visual Studio 2013中,密码加密方法,我需要将它迁移到Xamarin Android。xamarin Android加密GetBytes与C#的方法区别

我遇到的问题是Xamarin中的加密方法给出了不同的加密字符串。

区别在于GetBytes方法。

这是我的代码的一部分。

public static string Encript(string ptexto, string pClave) 
     { 
    return Encript2(ptexto, pClave + "[email protected]", "[email protected]", "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); 

我不能改变我的Encryptation在我的C#Applicaction线,是否有办法获得Xamarin同样的结果?

回答

0

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

GetBytes(Int32) : Byte[] 

返回伪随机密钥字节。

相关问题