Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/369.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Net应用程序和Java应用程序之间的对称加密问题_Java_.net_Encryption_Aes_Encryption Symmetric - Fatal编程技术网

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

Net应用程序和Java应用程序之间的对称加密问题,java,.net,encryption,aes,encryption-symmetric,Java,.net,Encryption,Aes,Encryption Symmetric,我正在与一个使用对称加密的.Net应用程序集成。我的应用程序是基于Java的。下面是使用对称加密的团队共享的.Net代码 public static string SymmetricEncrypt<T>(string value, string PrivateKey, string SALT_STRING) where T : SymmetricAlgorithm, new() { PasswordDeriveBytes rgb = new PasswordDeriv

我正在与一个使用对称加密的.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());
        }
    }
在Java代码中,我给出了用于测试目的的示例SALT密钥和私钥。但我仍然无法获得.Net应用程序生成的确切值。下面是一个示例纯文本和加密值,我从.Net应用程序中获得了该值,但我在Java代码中未能复制该值

纯文本值:FTTH

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


非常感谢您在这方面提供的任何帮助,因为我目前没有任何想法。

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

您如何调用.Net加密函数?什么是算法和密钥大小?问题是.NET中的加密函数没有指定密钥长度。假设我这样称呼:SymmetricEncrypt。我很想尝试和帮助,但没有具体的电话,我不能。然后,它肯定不会以传统的方式工作。你可以在这里找到答案:然而,从长远来看,.NET团队最好还是换成新的,从光明的角度来看:只要你不发送静脉注射,你就应该仍然安全。如果调用
GetBytes
两次且超过20个字节,则
PasswordDeriveBytes
的MS实现可能会重复先前生成的字节。对于
x+y
bytes,调用
GetBytes
一次,然后在
GetBytes(x)
GetBytes(y)
中调用两次,这也有区别。不错吧?现在切换到
RFC2898DeriveBytes
,如果希望与Java兼容,不要忘记只使用代码点<128的字符最好不要这样做,您需要指定字符串中使用的字节编码:
byte[]password=privateKey.getBytes(“utf-16”)或其他什么。IIRC C#使用UTF-16。类似地,对于
byte[]salt=saltString.getBytes()。编译器应该将
String.getBytes()
标记为已弃用。
 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;
    }
}