.Net应用程序和Java应用程序之间的对称加密问题

时间:2015-05-17 06:17:59

标签: java .net encryption aes encryption-symmetric

我正在使用使用Symmetric加密的.Net应用程序进行集成。我的应用程序是Java。以下是使用对称加密的团队共享的.Net代码;

public static string SymmetricEncrypt<T>(string value, string PrivateKey, string SALT_STRING) where T : SymmetricAlgorithm, new()
    {

 PasswordDeriveBytes rgb = 
 new PasswordDeriveBytes(PrivateKey ,      
Encoding.Unicode.GetBytes(SALT_STRING));

        SymmetricAlgorithm algorithm = new T();

        byte[] rgbKey = rgb.GetBytes(algorithm.KeySize >> 3);
        byte[] rgbIV = rgb.GetBytes(algorithm.BlockSize >> 3);

        ICryptoTransform transform = algorithm.CreateEncryptor(rgbKey, rgbIV);

        using (MemoryStream buffer = new MemoryStream())
        {
  using (CryptoStream stream = 
                new CryptoStream(buffer, transform, CryptoStreamMode.Write))
            {
using (StreamWriter writer = new StreamWriter(stream, Encoding.Unicode))
                {
                    writer.Write(value);
                }
            }

            return Convert.ToBase64String(buffer.ToArray());
        }
    }

查看.Net文档,我可以看到SymmetricEncrypt类默认使用Rijndael加密。而且我在弹性城堡中找到了类似于“PasswordDeriveBytes”的类,它实现了使用盐的密钥生成的“PKCS5S1”算法,迭代次数为100.但是我仍然无法生成所需的精确加密通过.Net应用程序。以下代码是我到目前为止所尝试的;

 import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

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

import mtnsa.sorb.handler.PKCS5Test.PasswordDeriveBytes;

import org.bouncycastle.crypto.digests.SHA1Digest;
import org.bouncycastle.crypto.generators.PKCS5S1ParametersGenerator;
import org.bouncycastle.crypto.params.KeyParameter;
import org.bouncycastle.util.encoders.Base64;

public class EncryptionHelper {



    public static void main(String[] args)throws Exception {
        encrypt("FTTH","HhN01vcEEtMmwdNFliM8QYg0Y89xzBOJJG7BHARC7g", "002400000480000094000000060200000024000052534131000400000100010085525e9438e9fae122f71ec7124" +
                "443bf2f9f57f5f3760b3704df168493004b9ef68413f500d54fa9fa3869b42b1e2365204826e54b618d56e7e575f2" +
                "7f675f0eae3ea8458a8ee1e92dc3f4bfc34fbe23851afa9d2c28fc8cd5b124f60a03a06bfb598bc3acbd8c4380ae" +
                "f02cc58bdf955d140390f740a7e115c59e3b3b5758ca");
    }

    public static String encrypt(final String valueToEncrpt, final String saltString, final String privateKey) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException{
        String encrptedValue = null;

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

        byte[] password = privateKey.getBytes();
        byte[] salt = saltString.getBytes();

        PKCS5S1ParametersGenerator generator = new PasswordDeriveBytes(new SHA1Digest());
        generator.init(password, salt, 100);

        byte[] keyArr = ((KeyParameter)generator.generateDerivedParameters(128)).getKey();
        byte[] IvArr = ((KeyParameter)generator.generateDerivedParameters(128)).getKey();



        cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(keyArr,"AES"),new IvParameterSpec(IvArr));
        byte[]test  = cipher.doFinal(valueToEncrpt.getBytes());

        System.out.println(new String(Base64.encode(test)));

        return encrptedValue;
    }
}

在Java代码中,我给出了样本SALT密钥和用于测试目的的PRIVATE密钥。但是我仍然无法获得.Net应用程序生成的确切值。下面是一个示例纯文本和我从.Net应用程序获得的加密值,我无法在我的Java代码中复制。

纯文本值:FTTH

加密值:MjgmbdT3Vg6RW / 7K1BjQ / Q ==

对此我提出的任何帮助都非常感谢,因为我目前没有想法。

1 个答案:

答案 0 :(得分:1)

感谢大家的宝贵意见。我已请求.Net模块作者将其实现更改为使用RFC2898DeriveBytes