gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 06:01:43 25 4
gpt4 key购买 nike

我正在加密 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 Net Code(解密代码):

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",

最佳答案

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

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

或者,他们只解密有效负载并忽略 header 。如果不查看他们的代码,就不可能知道。

关于java - 解密后消息开头的特殊字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55971873/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com