You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C# AES加密输出为何截断?旧代码兼容场景排查与解决

长API密钥解密截断问题修复

问题背景

某老旧系统的API密钥加密代码,短密钥加密解密正常,但处理长API密钥时,解密输出会出现截断情况。

原加密解密代码

internal class TestCrypto
{
    private static int mIterations = 2;
    private static int mKeySize = 256;

    private static string mHash = "SHA1";
    private static string mSalt = "This is test....";
    private static string mVector = "Test Vector only";

    public static string Encrypt(string value, string password)
    {
        if (string.IsNullOrEmpty(value)) return null;

        byte[] vectorBytes = Encoding.ASCII.GetBytes(mVector);
        byte[] saltBytes = Encoding.ASCII.GetBytes(mSalt);
        byte[] valueBytes = Encoding.UTF8.GetBytes(value);

        byte[] encrypted;
        using (var cipher = new AesManaged())
        using (var _passwordBytes =
            new PasswordDeriveBytes(password, saltBytes, mHash, mIterations))
        {
            byte[] keyBytes = _passwordBytes.GetBytes(mKeySize / 8);

            cipher.Mode = CipherMode.CBC;

            using (ICryptoTransform encryptor = cipher.CreateEncryptor(keyBytes, vectorBytes))
            using (MemoryStream to = new MemoryStream())
            using (CryptoStream writer = new CryptoStream(to, encryptor, CryptoStreamMode.Write))
            {
                writer.Write(valueBytes, 0, valueBytes.Length);
                writer.FlushFinalBlock();
                encrypted = to.ToArray();
            }
            cipher.Clear();
        }
        return Convert.ToBase64String(encrypted);
    }

    public static string Decrypt(string value, string password)
    {
        if (string.IsNullOrEmpty(value)) return null;

        byte[] vectorBytes = Encoding.ASCII.GetBytes(mVector);
        byte[] saltBytes = Encoding.ASCII.GetBytes(mSalt);
        byte[] valueBytes = Convert.FromBase64String(value);

        byte[] decrypted;
        int decryptedByteCount = 0;

        using (var cipher = new AesManaged())
        using (var _passwordBytes =
            new PasswordDeriveBytes(password, saltBytes, mHash, mIterations))
        {
            byte[] keyBytes = _passwordBytes.GetBytes(mKeySize / 8);

            cipher.Mode = CipherMode.CBC;

            try
            {
                using (ICryptoTransform decryptor = cipher.CreateDecryptor(keyBytes, vectorBytes))
                using (MemoryStream from = new MemoryStream(valueBytes))
                using (CryptoStream reader = new CryptoStream(from, decryptor, CryptoStreamMode.Read))
                {
                    decrypted = new byte[valueBytes.Length];
                    decryptedByteCount = reader.Read(decrypted, 0, decrypted.Length);
                }
            }
            catch (Exception)
            {
                return null;
            }

            cipher.Clear();
        }
        return Encoding.UTF8.GetString(decrypted, 0, decryptedByteCount);
    }
}

测试代码及问题表现

测试代码:

var e = TestCrypto.Encrypt("This is just a test with a string that is very long", "abcd1234");
Console.WriteLine(TestCrypto.Decrypt(e, "abcd1234"));

输出结果被截断:

This is just a test with a string that is very l

修复方案

修改Decrypt方法中读取解密数据的代码:
将

decryptedByteCount = reader.Read(decrypted, 0, decrypted.Length);

替换为

decryptedByteCount = reader.ReadAtLeast(decrypted, decrypted.Length, false);

替换后即可解决长API密钥解密输出截断的问题。


内容的提问来源于stack exchange,提问作者s k

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.09 16:07:51