1

这是我在 Unity3d 中使用的解密方法。当我用 128 字节大小的部分调用它时它工作得很好,但是当它一次说 256 个字节时它会失败。我应该只在块大小块中调用它吗?

经过进一步分析,我认为 PKCS7 填充并没有以所述加速速度被移除。但是,它不是相同的字节,因此排除了填充删除。但是我不知道这 24 个额外的字节是从哪里来的。一方面,您可以看到解密的文本中没有任何遗漏。那么他们到底是从哪里来的呢?当一个大块被解码时,底部的解密数组的调试视图有这个内容:

1--------3895-------/home/max/PycharmProjects/RealityShell/Backend\r\n\xx2\xx2\xx2\xx2\xx2\xx2\xx2\xx2\xx2\xx2\ xx2\xx2\xx2\xx2\xx2\xx2\xx2\"\xx2neL\xx2\xx2\xx2\xx2\xx2{2--------3895-------/home/max /PycharmProjects/ RealityShell/后端\r\n\xx2\xx2\xx2\xx2\xx2\xx2\xx2\xx2\xx2\xx2\xx2\xx2\xx2\xx2tC; \xx2VCY3\xx2Z?r\xx2\xx2\xx2f3-- ----3895-------/home/max/PycharmProjects/RealityShell/Backend\r\n\xx2\xx2\xx2\xx2\xx2\xx2\xx2\xx2\xx2\xx2\xx2\xx2 \xx2\xx2\xx2\xx2neWc\xx2\xx2\xx2\xx2+ \xx2\xx2L;M4------3895-------/home/max/PycharmProjects/RealityShell/Backend\r\n \

所以我的测试内容是“1-----3895------/home/max/PycharmProjects/RealityShell/Backend\r\n\”等行,而\xx2字节是莫名其妙的,构成问题。

public static string DecryptStringFromBytes_Aes(string serversays, string Key)
{

Debug.Log ("Decrypt incoming length:" + serversays.Length.ToString ());
byte[] cipherText = Convert.FromBase64String(serversays);
byte[] keystring = Encoding.UTF8.GetBytes(Key);

using (Aes aesAlg = Aes.Create())
{
    aesAlg.Key = keystring;
    aesAlg.Mode = CipherMode.CBC;
    aesAlg.BlockSize = 128;
    aesAlg.Padding = PaddingMode.PKCS7;

    byte[] cipherText_only = new byte[cipherText.Length - aesAlg.IV.Length];
    byte[] IV = new byte[aesAlg.IV.Length];

    Array.Copy(cipherText, 0, IV, 0, aesAlg.IV.Length);
    Array.Copy(cipherText, aesAlg.IV.Length, cipherText_only, 0, cipherText_only.Length);

    aesAlg.IV = IV;

    using (MemoryStream ms = new MemoryStream())
    {
        using (ICryptoTransform decryptor = aesAlg.CreateDecryptor())
        {
            using (CryptoStream cs = new CryptoStream(ms, decryptor, CryptoStreamMode.Write))
            {
                cs.Write(cipherText_only, 0, cipherText_only.Length);
                //cs.FlushFinalBlock();

                byte[] decrypted = ms.ToArray();
                string decr_serverSays = Encoding.UTF8.GetString (decrypted);
                Debug.Log (decr_serverSays + "..." + decr_serverSays.Length.ToString());
                return decr_serverSays;
            }
        }
    }
}
}

这是 Python 加密方法:

def encrypt(self, key, raw):
    encoder = PKCS7Encoder()
    #mac = HMAC.new(raw, digestmod=SHA256)
    raw_pad = encoder.encode(raw)
    iv = Random.new().read(AES.block_size)
    cipher = AES.new(key.encode('utf-8'), AES.MODE_CBC, iv)
    encry_cipher = iv + cipher.encrypt(raw_pad)
    encry = base64.b64encode(encry_cipher)
    #print(len(encry), len(encry_cipher), len(raw_pad))
    return encry
4

1 回答 1

0

不完全确定如何解释该调试输出,但您的代码存在问题:您在刷新MemoryStream之前阅读了整个内容。CryptoStream您可以做的最好的事情是移出ms.ToArray()内部使用:

using (MemoryStream ms = new MemoryStream())
{
    using (ICryptoTransform decryptor = aesAlg.CreateDecryptor())
    using (CryptoStream cs = new CryptoStream(ms, decryptor, CryptoStreamMode.Write))
    {
        cs.Write(cipherText_only, 0, cipherText_only.Length);
    }

    byte[] decrypted = ms.ToArray();
    string decr_serverSays = Encoding.UTF8.GetString(decrypted);
    Debug.Log (decr_serverSays + "..." + decr_serverSays.Length.ToString());
    return decr_serverSays;
}
于 2017-07-09T21:18:21.637 回答