java - 解密 C# RIJNDAEL 编码的文本

标签 java c# encryption cryptography rijndael

我正在用 Java 实现与第三方应用程序的通信。 作为登录过程的一部分,第三方应用程序正在发送一个加密字符串,我必须对其进行解码并发回。 我花了将近 2 天的时间在谷歌搜索和阅读帖子,但我找不到正确的方法来实现这一点。

我有一个测试用例,其中加密字符串为“c1W2YO1vYQzu6czteEidrG0U4g5gT4h57vAlP7tdjcY=”,使用密码“GAT”解密必须返回“101714994”。

我的文档说明了这一点: 授权字符串使用以下设置加密:

  • 输入数据的填充:PKCS*7
  • 密码字节数组的长度为 32 个字节。密码字符串被转换为 UTF-16 编码的字节数组,然后用零填充字节数组,最多 32 个字节的长度。较长的密码会被截断。

这是如何解密授权字符串的 C# 示例:

/// <summary> 
/// Decrypts a string. 
/// </summary> 
/// <param name="content">The string to decrypt.</param> 
/// <param name="password">The password to use.</param> 
/// <returns>The decrypted string.</returns> 
private static string DecryptString(string content, string password) { 
    Rijndael aes; 
    byte[] retVal = null; 
    byte[] contentBytes; 
    byte[] passwordBytes; 
    byte[] ivBytes;
    try { 
        //Get the content as byte[] 
        contentBytes = Convert.FromBase64String(content);

        //Create the password and initial vector bytes 
        passwordBytes = new byte[32]; 
        ivBytes = new byte[16]; 
        Array.Copy(Encoding.Unicode.GetBytes(password), passwordBytes, Encoding.Unicode.GetBytes(password).Length); 
        Array.Copy(passwordBytes, ivBytes, 16);

        //Create the cryptograpy 
        object aes = Rijndael.Create(); 
        aes.Key = passwordBytes; 
        aes.IV = ivBytes; 
        aes.Padding = PaddingMode.PKCS7;

        //Decrypt 
        retVal = aes.CreateDecryptor().TransformFinalBlock(contentBytes, 0, contentBytes.Length); 
    } 
    catch { 
    }
    aes = null; 
    contentBytes = null; 
    passwordBytes = null; 
    ivBytes = null;
    return Encoding.Unicode.GetString(retVal) 
}

这是我解密字符串的 Java 程序:

private String decryptAuthorizationString(String authString, String password) {
  try {
    //Force the test string
    authString = "c1W2YO1vYQzu6czteEidrG0U4g5gT4h57vAlP7tdjcY=";
    //Force the test password
    password = "GAT";

    //Create the password and initial vector bytes
    byte[] passwordBytes= new byte[32];      
    byte[] b= password.getBytes("UTF-8");      
    int len= b.length;
    if (len > passwordBytes.length) len = passwordBytes.length;
      System.arraycopy(b, 0, passwordBytes, 0, len);

    byte[] ivBytes= new byte[16];
    System.arraycopy(passwordBytes, 0, ivBytes, 0, 16);

    //Get the authString as byte[]
    byte[] authBytes = new BASE64Decoder().decodeBuffer(authString);

    InputStream inputStream = new ByteArrayInputStream(authBytes);
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

    // If you have Bouncycastle library installed, you can use
    // Rijndael/CBC/PKCS7PADDING directly.
    Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
    Cipher cipher = Cipher.getInstance("Rijndael/CBC/PKCS7PADDING", "BC");

    // convertedSecureString and initVector must be byte[] with correct length
    cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(passwordBytes, "AES"), new IvParameterSpec(ivBytes));

    CipherInputStream cryptoStream = new CipherInputStream(inputStream, cipher);
    byte[] buffer = new byte[1024];
    len = cryptoStream.read(buffer, 0, buffer.length);
    while (len > 0) {
      outputStream.write(buffer, 0, len);
      len = cryptoStream.read(buffer, 0, buffer.length);
    }

    outputStream.flush();
    cryptoStream.close();
    String resStr = outputStream.toString("UTF-8");       
    return resStr; //<<--- resStr must be "101714994"
  } catch (Throwable t) {

  }
  return null;
}

程序运行没有任何错误,但结果不是我想要的。 任何帮助将不胜感激。

最佳答案

您不需要为此使用 BouncyCaSTLeProvider,因为 AES 已包含在 Java 中。但是 PKCS#7 填充是 incorrectly"PKCS5Padding" 指示,因此 "AES/CBC/PKCS7Padding" 不能在没有 Bouncy CaSTLe 的情况下指示。

.NET默认的Unicode编码其实更兼容UTF-16LE。让 Microsoft 不遵守标准名称(尽管它们可能已经在它之前)。

Java JCE 并不像 C# 类那样真正围绕流构建,因此最好完全避免流。

我已经重写了您的示例代码,以展示如何在 Java 中正确编写代码(尽管您需要与 Java 7 兼容)。不要隐藏异常,将它们转化为 AssertErrorRuntimeException

我使用的是 Bouncy CaSTLe Base 64 解码器,因为您和我都可以使用它(但除此之外它与 Bouncy 无关)。 Java 8 有一个 base 64 class包括。


所以事不宜迟:

import static java.nio.charset.StandardCharsets.UTF_16LE;

import java.security.GeneralSecurityException;
import java.util.Arrays;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

import org.bouncycastle.util.encoders.Base64;

public class AuthenticationStringDecrypter {

    private static final String AES_CBC_PKCS5PADDING = "AES/CBC/PKCS5PADDING";
    private static final int KEY_SIZE = 256;

    public static void main(final String[] args) throws Exception {
        System.out.println(decryptAuthorizationString(
                "c1W2YO1vYQzu6czteEidrG0U4g5gT4h57vAlP7tdjcY=", "GAT"));
    }

    private static String decryptAuthorizationString(final String authString,
            final String password) {
        try {
            // --- check if AES-256 is available
            if (Cipher.getMaxAllowedKeyLength(AES_CBC_PKCS5PADDING) < KEY_SIZE) {
                throw new IllegalStateException("Unlimited crypto files not present in this JRE");
            }

            // --- create cipher
            final Cipher cipher = Cipher.getInstance(AES_CBC_PKCS5PADDING);

            // --- create the key and initial vector bytes
            final byte[] passwordEncoded = password.getBytes(UTF_16LE);
            final byte[] keyData = Arrays.copyOf(passwordEncoded, KEY_SIZE
                    / Byte.SIZE);
            final byte[] ivBytes = Arrays.copyOf(keyData, cipher.getBlockSize());

            // --- init cipher
            cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(keyData, "AES"),
                    new IvParameterSpec(ivBytes));

            // --- decode & decrypt authentication string
            final byte[] authBytes = Base64.decode(authString);
            final byte[] decryptedData = cipher.doFinal(authBytes);

            // WARNING: may still decrypt to wrong string if
            // authString or password are incorrect - 
            // BadPaddingException may *not* be thrown
            return new String(decryptedData, UTF_16LE);
        } catch (BadPaddingException | IllegalBlockSizeException e) {
            // failure to authenticate
            return null;
        } catch (final GeneralSecurityException e) {
            throw new IllegalStateException(
                    "Algorithms or unlimited crypto files not available", e);
        }
    }
}

关于java - 解密 C# RIJNDAEL 编码的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25064529/

相关文章:

c - 在c中制作一长串加密子字符串

java - 外键约束失败 - hibernate

java - 如何验证基于 soap 的 Java Web 服务?

c# - webBrowser 控件停止来自网站的 javascript 警报

javascript - "Invalid AES Block Size"SJCL解密

java - 远程创建加密的 MySQL 备份

java - 突然出现此错误 java.lang.IllegalStateException : Request cannot be executed; I/O reactor status: STOPPED

java - 简单字符串拆分

c# - 将图像控件转换为字节数组 Asp.Net

c# - 如何在 C# 中使用 List 的范围?