首页 > 解决方案 > 解密后消息开头的特殊字符

问题描述

我正在加密一条 JSON 消息并将其发送给用户。当他们解密它时,一些消息在消息的开头显示特殊字符。

当我试图从我身边解密时,它工作正常。如果他们正在解密它,它会显示特殊字符,但仅适用于某些消息,因为有些消息可以很好地解密。

下面是我用来加密消息的 Java 代码,还添加了它们用来在 .NET 中解密的代码。请帮助我了解这种情况以及发生这种情况的原因。

JAVA代码(加密):

package com.kcs.mule.encryption;

import javax.crypto.Cipher;
import org.apache.commons.codec.binary.Base64;
import org.mule.api.MuleEventContext;
import org.mule.api.lifecycle.Callable;

import java.security.AlgorithmParameters;
import java.security.SecureRandom;
import java.util.Arrays;

import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.SecretKeySpec;

public class AESEncryption implements Callable {

    private static final String password = "fvtQhQcKZVWMCXRLbqmRgfEBXYWshTEP";
    private static int pswdIterations = 65536;
    private static int keySize = 256;
    private static byte[] ivBytes;

    @Override
    public Object onCall(MuleEventContext eventContext) throws Exception {

        String plainText = eventContext.getMessageAsString();

        byte[] saltBytes = password.getBytes("UTF-8");

        // Derive the key
        SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256");
        PBEKeySpec spec = new PBEKeySpec(password.toCharArray(), saltBytes, pswdIterations, keySize);

        SecretKey secretKey = factory.generateSecret(spec);
        SecretKeySpec secret = new SecretKeySpec(secretKey.getEncoded(), "AES");

        // encrypt the message
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, secret);
        AlgorithmParameters params = cipher.getParameters();
        ivBytes = params.getParameterSpec(IvParameterSpec.class).getIV();
        byte[] encryptedTextBytes = cipher.doFinal(plainText.getBytes("UTF-8"));

        byte[] encryptedOutput = new byte[ivBytes.length+encryptedTextBytes.length];
        System.arraycopy(ivBytes, 0, encryptedOutput, 0, ivBytes.length);
System.arraycopy(encryptedTextBytes,0,encryptedOutput,ivBytes.length,encryptedTextBytes.length);
        return encryptedOutput;
    }

}

DOT网码(解密码):

byte[] saltBytes = key;
                PBEKeySpec keySpec = new PBEKeySpec(key, saltBytes, 65536);
                var keyHash = keySpec.GetBytes(32);

                using (Aes aesCrypto = Aes.Create())
                {
                    //set the BlockSize and the KeySize before you set the Key and the IV
                    //to avoid padding exceptions.
                    aesCrypto.BlockSize = 128;
                    aesCrypto.KeySize = 256; // AES256
                    aesCrypto.Key = keyHash;
                    byte[] cipherTextCombined = request.MessageBytes;
                    byte[] IV = new byte[aesCrypto.BlockSize / 8];
                    byte[] cipherText = new byte[cipherTextCombined.Length - IV.Length];

                    Array.Copy(cipherTextCombined, IV, IV.Length);
                    Array.Copy(cipherTextCombined, IV.Length, cipherText, 0, cipherText.Length);

                    aesCrypto.IV = IV; //Initialization vector
                    aesCrypto.Mode = CipherMode.CBC; //Cipher Block Chaining mode
                    aesCrypto.Padding = PaddingMode.PKCS7;

                    // Create a decryptor to perform the stream transform.
                    ICryptoTransform decryptor = aesCrypto.CreateDecryptor(aesCrypto.Key, aesCrypto.IV);

                    // Create the streams used for decryption. 
                    using (var msDecrypt = new MemoryStream(cipherText))
                    {
                        using (var csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
                        {
                            using (var srDecrypt = new StreamReader(csDecrypt, new UTF8Encoding(false)))
                            {
                                // Read the decrypted bytes from stream to string.
                                response.MessageText = srDecrypt.ReadToEnd();
                            }
                        }
                    }
                }
            }

实际结果:

����w?*�/�r5le": { "System": "KCS", "Train": { "TrainNumber": "36181542", "TrainID": "G-CDMY -26",

预期结果:

TrainSchedule: { "System": "KCS", "Train": { "TrainNumber": "36181542", "TrainID": "G-CDMY -26",

标签: javaencryption

解决方案


有可能您没有加密字符串“TrainSchedule”,但他们正在解密 TrainSchedule 以及有效负载。

要么加密标头,要么告诉他们在解密之前将有效负载与标头分开,然后将其连接起来。

或者,他们只是解密有效载荷并忽略标头。不看他们的代码是不可能知道的。


推荐阅读