AES Rijndael解密C#

问题描述 投票:1回答:1

我必须解密使用AES256 Rijndael加密发送的某些数据。我有我的合作伙伴使用JAVA开发的加密/解密机制,虽然我不太熟悉,但是无法在C#中进行转置。

已提供给我的密钥长10个字符。

我认为下面的代码还可以,除了IV计算。您将首先找到Java代码,然后是C#:

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.Charset;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class UtilsCrypto {

    /* Rijndael/CFB8/NoPadding is default cipher */
    final static String CHIPHER = "Rijndael/CFB8/NoPadding";

    public static final String  MESSAGE_DIGEST_ALGORITHM    = "MD5";
    public static final String  AES                         = "AES";
    public static final String  AES_ECB_NO_PADDING          = "AES/ECB/NoPadding";

    private static byte[] md5(final String input) throws NoSuchAlgorithmException {
        final MessageDigest md = MessageDigest.getInstance(MESSAGE_DIGEST_ALGORITHM);
        return md.digest(input.getBytes());
    }

    private Cipher initCipher(final int mode, final String secretKey) throws Exception {
        final byte[] key = md5(secretKey);
        final byte[] iv = md5(secretKey);

        final SecretKeySpec skeySpec = new SecretKeySpec(key, AES);

        /* This valid with other ciphers than Rijndael/CFB8/NoPadding */
        // final IvParameterSpec initialVector = new IvParameterSpec(iv);

        /* Use this with Rijndael/CFB8/NoPadding */
        final IvParameterSpec initialVector = new IvParameterSpec(getIvBytes(iv));

        final Cipher cipher = Cipher.getInstance(CHIPHER);
        cipher.init(mode, skeySpec, initialVector);

        return cipher;
    }

    public String encrypt(final String dataToEncrypt, final String secretKey) {
        if (Utils.isEmpty(secretKey))
            return dataToEncrypt;

        String encryptedData = null;
        try {
            final Cipher cipher = initCipher(Cipher.ENCRYPT_MODE, secretKey);
            final byte[] encryptedByteArray = cipher.doFinal(dataToEncrypt.getBytes(Charset.forName("UTF8")));
            final BASE64Encoder enc = new BASE64Encoder();
            encryptedData = enc.encode(encryptedByteArray);
            encryptedData = encryptedData.replace("+", "-");
            encryptedData = encryptedData.replace("/", "_");
        } catch (Exception e) {
            System.err.println("Problem encrypting the data");
            e.printStackTrace();
        }

        return encryptedData;
    }

    public String decrypt(final String encryptedData, final String secretKey) {
        String decryptedData = null;
        String inData = encryptedData;
        try {
            final Cipher cipher = initCipher(Cipher.DECRYPT_MODE, secretKey);
            final BASE64Decoder dec = new BASE64Decoder();
            inData = inData.replace("-", "+");
            inData = inData.replace("_", "/");
            final byte[] encryptedByteArray = dec.decodeBuffer(inData); // ok
            final byte[] decryptedByteArray = cipher.doFinal(encryptedByteArray);
            decryptedData = new String(decryptedByteArray, "UTF8");
        } catch (Exception e) {
            System.err.println("Problem decrypting the data");
            e.printStackTrace();
        }
        return decryptedData;
    }

    /**
     * This method is only for Rijndael/CFB8/NoPadding
     * 
     * @param hashedKey
     *            md5
     * @return byte array
     * @throws Exception
     *             If any exceptions.
     */
     // on passe en arg le hash de la clé
    protected byte[] getIvBytes(byte[] hashedKey) throws Exception {
        byte[] inputBytes = new byte[16]; // init son tableau a 16 bytes
        final SecretKey key = new SecretKeySpec(hashedKey, AES); // secretKey
        final Cipher cipher = Cipher.getInstance(AES_ECB_NO_PADDING); 
        cipher.init(Cipher.ENCRYPT_MODE, key); // chiffre sa clé en AES avec un IV
        return cipher.doFinal(inputBytes);
    }
}

现在这是我到目前为止尝试过的:

using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Xml;
using System.Net;
using System.Web;
using System.Web.Services;
using Newtonsoft.Json;
using System.Security.Cryptography;
using System.Text;

namespace test
{
        public byte[] getIVBytes(byte[] hashedKey)
        {
            byte[] inputBytes = new byte[16];
            AesManaged tdes = new AesManaged();
            tdes.Key = hashedKey;
            tdes.Mode = CipherMode.ECB;
            tdes.BlockSize = 128;
            tdes.Padding = PaddingMode.None;
            ICryptoTransform crypt = tdes.CreateEncryptor();
            byte[] bla =  crypt.TransformFinalBlock(hashedKey, 0, inputBytes.Length);
            return bla;
        }

        [WebMethod]
        public string decrypt(String input, String key)
        {
            byte[] md5KeyHash;
            using (MD5 md5 = MD5.Create())
            {
                md5KeyHash = md5.ComputeHash(Encoding.UTF8.GetBytes(key));
            }
            input = input.Replace("-", "+");
            input = input.Replace("_", "/");
            input = input.Replace(" ", "");
            byte[] data = Convert.FromBase64String(input); // récupérer l'array de bytes du message chiffré encodé en b64
            String decrypted;
            using (RijndaelManaged rijAlg = new RijndaelManaged())
            {
                rijAlg.Mode = CipherMode.CFB;
                rijAlg.BlockSize = 128;
                rijAlg.Padding = PaddingMode.None;
                rijAlg.Key = md5KeyHash;
                rijAlg.IV = getIVBytes(md5KeyHash);

                ICryptoTransform decryptor = rijAlg.CreateDecryptor(rijAlg.Key, null);
                using (MemoryStream msDecrypt = new MemoryStream(data))
                {
                    using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
                    {
                        using (StreamReader srDecrypt = new StreamReader(csDecrypt))
                        {
                            decrypted = srDecrypt.ReadToEnd();
                        }
                    }
                }
            }
            return decrypted;
}

代码似乎是“正确的”,因为除此以外没有其他错误:XML错误分析:在文本内容中发现一个无效字符。于:http://localhost:55175/WebService1.asmx/decrypt第2行,col44:>Me����>m.H.ZԤaf2ɾ`A�ٖ H $。&/

我想念什么?

java c# encryption aes rijndael
1个回答
0
投票

C#代码中存在一些错误:

  • getIVBytes方法中,替换行

    byte[] bla = crypt.TransformFinalBlock(hashedKey, 0, inputBytes.Length);
    

    作者

    byte[] bla = crypt.TransformFinalBlock(inputBytes, 0, inputBytes.Length); // encrypt inputBytes
    
  • decrypt方法中,在CreateDecryptor调用之前添加

    rijAlg.FeedbackSize = 8; // Use CFB8
    

    并替换行

    ICryptoTransform decryptor = rijAlg.CreateDecryptor(rijAlg.Key, null);
    

    作者

    ICryptoTransform decryptor = rijAlg.CreateDecryptor(rijAlg.Key, rijAlg.IV); // Consider the IV
    

然后可以使用C#代码将发布的密文解密为发布的纯文本。

© www.soinside.com 2019 - 2024. All rights reserved.