2012-06-29 107 views
0

我需要使用盐和密钥对字符串进行加密以匹配java加密,以便第三方提供者可以解密另一端的值。Rijndael或AES匹配java加密 - 使用salt和密钥

我已经尝试了几个StackOverflow文章,因为我不是加密专家,只是无法使用SALT和KEY作为第三方提供者获得相同的加密字符串。

我需要知道要使用的加密类型和模式在C#来匹配Java的AES加密这里使用

https://gist.github.com/ca958d5921d47c4c0a0f

回答

3

行 - 我想通了,即使它欺骗到一个程度。因为我找不到任何与第三方提供的普通AES加密相匹配的加密技术,所以我要求他们将其更改为

Cipher cipher = Cipher.getInstance(“AES/CBC/PKCS5Padding”);

有了这个,我修改了我的C#代码,并最终得到了整合工作:

public static string Encrypt2(string plainText) 
    { 
     string PassPhrase = "somepassphrase"; 
     string SaltValue = "somesalt"; 
     int PasswordIterations = 0; //amend to match java encryption iteration 
     string InitVector = "someiv"; 
     int KeySize = 0; //amend to match java encryption key size 

     byte[] initVectorBytes = Encoding.ASCII.GetBytes(InitVector); 
     byte[] saltValueBytes = Encoding.ASCII.GetBytes(SaltValue); 

     byte[] plainTextBytes = Encoding.UTF8.GetBytes(plainText); 

     Rfc2898DeriveBytes password = new Rfc2898DeriveBytes(
                 PassPhrase, 
                 saltValueBytes, 
                 PasswordIterations); 

     byte[] keyBytes = password.GetBytes(KeySize/8); 
     RijndaelManaged symmetricKey = new RijndaelManaged(); 
     symmetricKey.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; 
    }