2015-11-07 134 views
0

我需要使用C#使用“AES/ECB/PKCS5Padding”实现加密和解密方法对。原始代码是用Java编写的。这是在Java中的加密方法:将加密方法从Java转换为C#

public static String Encrypt(String plainText, byte[] key2) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException { 
     byte[] encryptedTextBytes=null; 
     byte[] key3 =null; 
     MessageDigest sha = MessageDigest.getInstance("SHA-1"); 
     key3= sha.digest(key2); 
     key3 = copyOf(key3, 16); 
     SecretKeySpec keySpec = new SecretKeySpec(key3, "AES"); 
     // Instantiate the cipher 
     Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); 
     cipher.init(Cipher.ENCRYPT_MODE, keySpec); 
     encryptedTextBytes = cipher.doFinal(plainText.getBytes("UTF-8")); 
     return new Base64().encode(encryptedTextBytes); 
} 

这是我尝试在C#重建它:

public static string Encrypt_AES(string plainText, byte[] key2) 
{ 
    var sha = new System.Security.Cryptography.SHA1CryptoServiceProvider();    
    byte[] key3 = new byte[16]; 
    sha.TransformFinalBlock(key2, 0, key2.Length); 
    var tmpkey = sha.Hash; 
    Array.Copy(tmpkey, key3, 16); 

    var aes = new System.Security.Cryptography.AesCryptoServiceProvider(); 
    aes.Padding = System.Security.Cryptography.PaddingMode.PKCS7; 
    aes.Mode = System.Security.Cryptography.CipherMode.ECB; 
    aes.Key = key3; 
    var plainTextBytes = Encoding.UTF8.GetBytes(plainText); 
    var encryptor = aes.CreateEncryptor(); 
    byte[] encryptedTextBytes = encryptor.TransformFinalBlock(plainTextBytes, 0, plainTextBytes.Length); 
    return Convert.ToBase64String(encryptedTextBytes); 
} 

加密一些内容,并将其发送到远程服务后,服务与回复错误表示无法解密邮件。所以我假设它有问题。

我也有一个Java解密方法的例子。我也实现了这种方法,并试图在本地加密和解密一些文本。当我这样做时,Decrypt_AES方法在TransformFinalBlock()中抛出CryptographicException,说“填充无效并且无法删除。”也许我正在使用CryptoProvider类错误?

这里有解密功能的Java和C#的版本: 的Java

public static String Decrypt(String encryptedText, byte[] key2) throws NoSuchAlgorithmException,NoSuchPaddingException,InvalidKeyException, IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException { 
    byte[] decryptedTextBytes=null; 
    byte[] key3 =null; 
    MessageDigest sha = MessageDigest.getInstance("SHA-1"); 
    key3= sha.digest(key2); 
    key3 = copyOf(key3, 16); 
    SecretKeySpec keySpec = new SecretKeySpec(key3, "AES"); 
    // Instantiate the cipher 
    Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); 
    cipher.init(Cipher.DECRYPT_MODE, keySpec); 
    byte[] encryptedTextBytes = new Base64().decode(encryptedText); 
    decryptedTextBytes = cipher.doFinal(encryptedTextBytes); 
    return new String(decryptedTextBytes); 
} 

C#

public static string Decrypt_AES(byte[] key2, string encryptedText) 
{ 
    var sha = new System.Security.Cryptography.SHA1CryptoServiceProvider(); 
    byte[] key3 = new byte[16]; 
    sha.TransformFinalBlock(key2, 0, key2.Length); 
    var tmpkey = sha.Hash; 
    Array.Copy(tmpkey, key3, 16); 

    var aes = new System.Security.Cryptography.AesCryptoServiceProvider(); 
    aes.Padding = System.Security.Cryptography.PaddingMode.PKCS7; 
    aes.Mode = System.Security.Cryptography.CipherMode.ECB; 
    aes.Key = key3; 

    var encryptedBytes = Encoding.UTF8.GetBytes(encryptedText); 
    var decryptor = aes.CreateDecryptor(); 
    var decryptedBytes = decryptor.TransformFinalBlock(encryptedBytes, 0, encryptedBytes.Length);    

    return System.Text.Encoding.UTF8.GetString(decryptedBytes); 
} 

谢谢你的任何暗示提前!

+0

你需要说什么错误。即你期望什么,你观察到了什么?否则,这个问题不适用于StackOverflow。 –

回答

0

你不是Base64-解密你的解密方法中的密文。

var encryptedBytes = Encoding.UTF8.GetBytes(encryptedText); 

应改为类似

var encryptedBytes = Convert.FromBase64String(encryptedText); 
+0

这使解密方法正常工作,谢谢。但是加密方法仍然有问题。任何关于java和C#加密方法可能不同的想法? – webtopf

+0

我现在用相同的输入运行了java代码和C#代码。它产生完全相同的输出。似乎我的方法毕竟是正确的。我会联系我试图拨打的服务的开发人员。如果没有其他的东西,我会接受这个答案,因为它解决了唯一的错误。再次感谢@JamesKPolk – webtopf