解密后消息开头的特殊字符

时间:2019-05-03 14:28:02

标签: java encryption

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

当我尝试从我的侧面解密时,它工作正常。如果他们正在解密,则它会显示特殊字符,但仅对某些消息有效,因为某些消息正在解密。

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

JAVA CODE(加密):

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“:{       “系统”:“ KCS”,       “火车”:{         “ TrainNumber”:“ 36181542”,         “ TrainID”:“ G-CDMY -26”,

预期结果:

  

TrainSchedule:{       “系统”:“ KCS”,       “火车”:{         “ TrainNumber”:“ 36181542”,         “ TrainID”:“ G-CDMY -26”,

1 个答案:

答案 0 :(得分:0)

可能您没有对字符串“ TrainSchedule”进行加密,但是它们正在对TrainSchedule和有效负载进行解密。

要么加密标头,要么告诉他们将有效载荷与标头分开,然后再解密,然后再进行连接。

或者,它们只是解密有效负载而忽略报头。如果不看他们的代码,就不可能知道。