Azure 功能应用 RabbitMQTrigger AES 加密消息未正确解密。有效负载似乎不是 Base64 字符串

Azure Fuction App RabbitMQTrigger AES Encrypted message not being decrypted correctly. Payload doesn't seem to be a Base64 String

提问人:OutlawsGalaxy 提问时间:11/16/2023 更新时间:11/16/2023 访问量:38

问:

很抱歉,但我对解密 aes 消息和 RabbitMQ 很陌生。

我有一个带有以下 RabbitMQ 触发器的 azure 功能应用程序:

法典:

    [FunctionName("RabbitMQTriggerTest")]       
public async Task Run([RabbitMQTrigger("event-vendor-create", ConnectionStringSetting = "RabbitMQConnection")] string item
  ,[DurableClient] IDurableOrchestrationClient starter,ILogger log)
{           
    string decryptedText = AesDecryptHelper.DecryptAes(item, _options.RabbitMQAesKey);
    log.LogInformation($"Decrypted Text: {decryptedText}");
}

rabbitMQ 服务器和队列是我们外部的东西,我们只有监听权限,我使用以下格式连接到它:

RabbitMQ连接:amqps://user:pass@hostName/vhost”

功能应用程序中的触发器正在触发,并向我提供以下类型的消息,一旦我收到它,它首先似乎不是 base64 或编码不正确:

�5H�����c�5
�5By��S�(��gq�� �9���>���&���f�H��7��%���n���)ח���c{������8��

我有一个名为 AesDecryptHelper 的静态帮助程序类,其中包含我在名为 DecryptAes 的触发器功能中调用的方法。

法典:

public static string DecryptAes(string encryptedText, string aesKey)
{
    string decryptedJson = null;

    // Convert the AES key to bytes
    byte[] keyBytes = Convert.FromBase64String(aesKey);

    // Convert the input Base64 encoded string to bytes
    byte[] encryptedBytes = Convert.FromBase64String(encryptedText);

    // Create an AES decryptor
    using (AesManaged aesAlg = new AesManaged())
    {
        aesAlg.Key = keyBytes;
        aesAlg.Mode = CipherMode.CBC;
        aesAlg.Padding = PaddingMode.PKCS7;

        // Assume the IV (Initialization Vector) is prepended to the encrypted data
        int ivSize = aesAlg.BlockSize / 8;
        byte[] iv = new byte[ivSize];
        Array.Copy(encryptedBytes, 0, iv, 0, ivSize);
        aesAlg.IV = iv;

        // Create a decryptor to perform the stream transform
        using (ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV))
        {
            // Create the streams used for decryption
            using (MemoryStream msDecrypt = new MemoryStream(encryptedBytes, ivSize, encryptedBytes.Length - ivSize))
            {
                using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
                {
                    using (StreamReader srDecrypt = new StreamReader(csDecrypt))
                    {
                        // Read the decrypted bytes from the decrypting stream and deserialize the JSON
                        decryptedJson = srDecrypt.ReadToEnd();
                    }
                }
            }
        }
    }

    return decryptedJson;
}

但是一旦它到达以下行,它就会出错:

byte[] encryptedBytes = Convert.FromBase64String(encryptedText);
 System.Private.CoreLib: Exception while executing function: RabbitMQTriggerTest. System.Private.CoreLib: The input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding characters, or an illegal character among the padding characters.

我尝试将其转换为 UTF8,然后转换为 base 64 sting,这似乎有效,但是:

var plainTextBytes = System.Text.Encoding.UTF8.GetBytes(encryptedText);
encryptedText =  System.Convert.ToBase64String(plainTextBytes);
byte[] encryptedBytes = Convert.FromBase64String(encryptedText);

但是一旦我尝试在 StreamReader 中对其进行解码:

decryptedJson = srDecrypt.ReadToEnd();

我得到以下内容:

 System.Private.CoreLib: Exception while executing function: RabbitMQTriggerTest. System.Security.Cryptography.Algorithms: The input data is not a complete block.

同样,我对此很陌生,我不确定我是否遇到了问题,因为从队列收到的初始项格式不正确,或者我是否需要将其转换为某种编码类型,试图解密它。

任何帮助将不胜感激。

C# 加密 azure-functions rabbitMQ AES

评论


答:

0赞 Sampath 11/16/2023 #1

下面的代码使用 RabbitMQ 触发器,该触发器侦听消息,然后使用 AES 加密对其进行解密。

  • 它从 RabbitMQ 队列中检索加密的消息,使用 AES 加密解密消息并记录它们。

  • 我遵循了 Azure Functions 的 RabbitMQ 触发器的 MSDOC

public static class Function
{
    [FunctionName("RabbitMQTriggerTest")]
    public static void Run(
        [RabbitMQTrigger("azures", ConnectionStringSetting = "sampath")] string encryptedMessage,
        [DurableClient] IDurableOrchestrationClient starter,
        ILogger log)
    {
        log.LogInformation($"C# RabbitMQ trigger function processed message: {encryptedMessage}");

       

        // Decrypt the message
        string decryptedMessage = DecryptAes(encryptedMessage, encryptionKey);
        log.LogInformation($"Decrypted Text: {decryptedMessage}");
    }

 public static string DecryptAes(string encryptedText, string aesKey)
    {
        string decryptedJson = null;

        try
        {
            
            byte[] keyBytes = Convert.FromBase64String(aesKey);

      
            byte[] encryptedBytes = Convert.FromBase64String(encryptedText);

            byte[] iv = new byte[16]; 
            Array.Copy(encryptedBytes, 0, iv, 0, iv.Length);

          
            using (AesManaged aesAlg = new AesManaged())
            {
                aesAlg.Key = keyBytes;
                aesAlg.IV = iv;
                aesAlg.Mode = CipherMode.CBC;
                aesAlg.Padding = PaddingMode.PKCS7;

             
                using (ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV))
                {
           
                    using (MemoryStream msDecrypt = new MemoryStream(encryptedBytes, iv.Length, encryptedBytes.Length - iv.Length))
                    {
                        using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
                        {
                            using (StreamReader srDecrypt = new StreamReader(csDecrypt))
                            {
                                // Read the decrypted bytes from the decrypting stream and deserialize the JSON
                                decryptedJson = srDecrypt.ReadToEnd();
                            }
                        }
                    }
                }
            }
        }

输出: enter image description here