将加密方法从Java转换为C#

时间:2015-11-07 05:07:26

标签: java c# encryption aes

我需要使用使用" AES / ECB / PKCS5Padding"的C#实现加密和解密方法对。原始代码是Java。以下是Java中的加密方法:

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#版本: 爪哇

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);
}

感谢您提前提示!

1 个答案:

答案 0 :(得分:1)

您的解密方法中没有Base64解码您的密文。

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

应改为

var encryptedBytes = Convert.FromBase64String(encryptedText);