简体   繁体   English

从点网到Java的AES 256解密

[英]AES 256 decryption from Dot net to Java

I have below do not code and trying to convert the same to java , but the result of the bot are quite different, In Dot net i get the actual value but in the Java i get the value result with same length but the values differs. 我在下面没有编写代码,并尝试将其转换为java,但是bot的结果是完全不同的,在Dot net中,我得到了实际值,但是在Java中,我得到了具有相同长度的值结果,但是值却不同。 The Decryption technique used is AES 256 , May i know where I am going wrong in the java code. 使用的解密技术是AES 256,我可以知道我在Java代码中出了什么问题。

Dot Net Code: 点网代码:

public static string AESDecryptText(string input, string key)
    {
        // Get the bytes of the string
        byte[] bytesToBeDecrypted = Convert.FromBase64String(input);
        byte[] keyBytes = Encoding.UTF8.GetBytes(key);
        keyBytes = SHA256.Create().ComputeHash(keyBytes);

        byte[] bytesDecrypted = AESDecrypt(bytesToBeDecrypted, keyBytes);

        string result = Encoding.UTF8.GetString(bytesDecrypted);

        return result;
    }

    private static byte[] AESDecrypt(byte[] bytesToBeDecrypted, byte[] keyBytes)
    {
        byte[] decryptedBytes = null;

        // Set your salt here, change it to meet your flavor:
        // The salt bytes must be at least 8 bytes.
        byte[] saltBytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };

        using (MemoryStream ms = new MemoryStream())
        {
            using (RijndaelManaged AES = new RijndaelManaged())
            {
                AES.KeySize = 256;
                AES.BlockSize = 128;

                var key = new Rfc2898DeriveBytes(keyBytes, saltBytes, 1000);
                AES.Key = key.GetBytes(AES.KeySize / 8);
                AES.IV = key.GetBytes(AES.BlockSize / 8);

                AES.Mode = CipherMode.CBC;

                using (var cs = new CryptoStream(ms, AES.CreateDecryptor(), CryptoStreamMode.Write))
                {
                    cs.Write(bytesToBeDecrypted, 0, bytesToBeDecrypted.Length);
                    cs.Close();
                }
                decryptedBytes = ms.ToArray();
            }
        }

        return decryptedBytes;
    }

Below the Java Code : 在Java代码下方:

byte[] salt = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };

        SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");

        MessageDigest md = MessageDigest.getInstance("SHA-256" );

        md.update(KEY_IV.getBytes( StandardCharsets.UTF_8 ) );
        byte[] hashDigest = md.digest();


        String encryptedHashDigest = Base64.encodeBase64String(hashDigest);

        PBEKeySpec pbeKeySpec = new PBEKeySpec(encryptedHashDigest.toCharArray(),
                salt, 1000, 384);

        Key generatedKey = factory.generateSecret(pbeKeySpec);

        byte[] extractedKey = new byte[32];
        byte[] iv = new byte[16];


        System.arraycopy(generatedKey.getEncoded(), 0, extractedKey, 0, 32);
        System.arraycopy(generatedKey.getEncoded(), 32, iv, 0, 16);


        byte[] base64Decoded = Base64.decodeBase64(toBeDecrypted);

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

        c.init(2, new SecretKeySpec(extractedKey, "AES"), new IvParameterSpec(iv));

        byte[] decryptedBytes = c.doFinal(base64Decoded);

        System.out.println("Decrypted " + new String(Base64.encodeBase64(decryptedBytes), StandardCharsets.UTF_8));

Your problem appears to be here: 您的问题似乎在这里:

 byte[] decValue = c.doFinal(decoded);

 System.out.println("Pin "+new String(decValue, StandardCharsets.UTF_8));

You have a byte array: decValue filled with the decrypted bytes. 您有一个字节数组: decValue填充了解密的字节。 Those bytes may, or may not, have been valid UTF-8 text before they were encrypted. 这些字节在加密之前可能是有效的UTF-8文本,也可能不是。 You need to convert those bytes back into the correct format(s) for customerData whatever that is. 无论是什么,您都需要将这些字节转换回customerData的正确格式。 If it comes from a database, then it may well not be UTF-8 but at least partly in binary. 如果它来自数据库,那么它很可能不是UTF-8,而是至少部分是二进制的。 Numbers might be held as raw binary in the database for instance. 例如,数字可能作为原始二进制文件保存在数据库中。

You need to check the precise format of the expected data, and convert your decrypted byte array into that same format. 您需要检查期望数据的精确格式,然后将解密的字节数组转换为相同的格式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM