C# 当输入大小与块大小精确对齐时,CryptoAPI密码数据生成为大数据

C# 当输入大小与块大小精确对齐时,CryptoAPI密码数据生成为大数据,c#,windows,encryption,ios6,cryptography,C#,Windows,Encryption,Ios6,Cryptography,有人能解释一下吗?在附加代码中,当要加密的输入文本与 ICryptoTransform的InputBlockSize,结果比输入大1个块。 这是为什么?如何避免。我需要在另一个系统(iOS6)上解密数据,在该系统中,没有添加或预期会添加此附加块 代码的输出正好在代码段之后 using System; using System.Collections.Generic; using System.Linq; using System.Security.Crypto

有人能解释一下吗?在附加代码中,当要加密的输入文本与 ICryptoTransform的InputBlockSize,结果比输入大1个块。 这是为什么?如何避免。我需要在另一个系统(iOS6)上解密数据,在该系统中,没有添加或预期会添加此附加块

代码的输出正好在代码段之后



    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Security.Cryptography;
    using System.Text;
    using System.Threading.Tasks;

    namespace TestCryptoAPI
    {
             class Program
             {
                     private static AesCryptoServiceProvider _cryptoServiceProvider;

                     static void Main(string[] args)
                     {
                              var encryptionKey = new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xae };
                              var initializationVector = new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xd5 };
                              _cryptoServiceProvider = new AesCryptoServiceProvider();

                              _cryptoServiceProvider.Key = encryptionKey;
                              _cryptoServiceProvider.IV = initializationVector;
                              _cryptoServiceProvider.Mode = CipherMode.CFB;
                              _cryptoServiceProvider.Padding = PaddingMode.PKCS7;

                              Console.WriteLine("_cryptoServiceProvider.BlockSize={0}", _cryptoServiceProvider.BlockSize);
                              ICryptoTransform encryptor = _cryptoServiceProvider.CreateEncryptor();
                              Console.WriteLine("encryptor.InputBlockSize={0}, encryptor.OutputBlockSize={1}", encryptor.InputBlockSize, encryptor.OutputBlockSize);

                              const string clearText15Chars = "123456789012345";
                              const string clearText16Chars = "1234567890123456";
                              const string clearText31Chars = "1234567890123456789012345678901";
                              const string clearText32Chars = "12345678901234567890123456789012";
                              const string clearText47Chars = "12345678901234567890123456789012345678901234567";
                              const string clearText48Chars = "123456789012345678901234567890123456789012345678";
                              const string clearText63Chars = "123456789012345678901234567890123456789012345678901234567890123";
                              const string clearText64Chars = "1234567890123456789012345678901234567890123456789012345678901234";

                              EncryptAndPrint(clearText15Chars);
                              EncryptAndPrint(clearText16Chars);
                              EncryptAndPrint(clearText31Chars);
                              EncryptAndPrint(clearText32Chars);
                              EncryptAndPrint(clearText47Chars);
                              EncryptAndPrint(clearText48Chars);
                              EncryptAndPrint(clearText63Chars);
                              EncryptAndPrint(clearText64Chars);
                     }

                     private static void EncryptAndPrint(string clearTextChars)
                     {
                              var cypherData = Encrypt(clearTextChars);
                              Console.WriteLine("ClearTextChars.Length={0}, CypherData.Length={1}, {2}", clearTextChars.Length, cypherData.Length, Convert.ToBase64String(cypherData));
                     }

                     private static byte[] Encrypt(string clearText15Chars)
                     {
                              var dataToEncrypt = Encoding.Default.GetBytes(clearText15Chars);
                              ICryptoTransform encryptor = _cryptoServiceProvider.CreateEncryptor();

                              var cypherData = encryptor.TransformFinalBlock(dataToEncrypt, 0, dataToEncrypt.Length);
                              return cypherData;
                     }
             }
    }

输出:

_cryptoServiceProvider.BlockSize=128 encryptor.InputBlockSize=16, encryptor.OutputBlockSize=16 ClearTextChars.Length=15, CypherData.Length=16, u8UVUNITsRswDu+usR3/gA== ClearTextChars.Length=16, CypherData.Length=32, u8UVUNITsRswDu+usR3/t7WnO82YSBITPehPTgwYTcg= ClearTextChars.Length=31, CypherData.Length=32, u8UVUNITsRswDu+usR3/t5LDd7hM0jYQr5mv9BU/6rE= ClearTextChars.Length=32, CypherData.Length=48, u8UVUNITsRswDu+usR3/t5LDd7hM0jYQr5mv9BU/6oK3EUi+F5lNkLmPFLjyIRCz ClearTextChars.Length=47, CypherData.Length=48, u8UVUNITsRswDu+usR3/t5LDd7hM0jYQr5mv9BU/6oKUWgdedM8a5BDQMQtWF5eA ClearTextChars.Length=48, CypherData.Length=64, u8UVUNITsRswDu+usR3/t5LDd7hM0jYQr5mv9BU/6oKUWgdedM8a5BDQMQtWF5e5HTHV4UCSuS5YsTlhNZwt+g== ClearTextChars.Length=63, CypherData.Length=64, u8UVUNITsRswDu+usR3/t5LDd7hM0jYQr5mv9BU/6oKUWgdedM8a5BDQMQtWF5e5NMSh/6GlzSMfscnk1Sc4bg== ClearTextChars.Length=64, CypherData.Length=80, u8UVUNITsRswDu+usR3/t5LDd7hM0jYQr5mv9BU/6oKUWgdedM8a5BDQMQtWF5e5NMSh/6GlzSMfscnk1Sc4W+Pb0ijEWKmgNJfGsQigc6A= Press any key to continue . . . _cryptoServiceProvider.BlockSize=128 encryptor.InputBlockSize=16,encryptor.OutputBlockSize=16 ClearTextChars.Length=15,CypherData.Length=16,u8UVUNITsRswDu+usR3/gA== ClearTextChars.Length=16,CypherData.Length=32,u8UVUNITsRswDu+usR3/t7WnO82YSBITPehPTgwYTcg= ClearTextChars.Length=31,CypherData.Length=32,u8UVUNITsRswDu+usR3/t5LDd7hM0jYQr5mv9BU/6rE= ClearTextChars.Length=32,CypherData.Length=48,u8UVUNITsRswDu+usR3/t5LDd7hM0jYQr5mv9BU/6oK3EUi+F5lNkLmPFLjyIRCz ClearTextChars.Length=47,CypherData.Length=48,u8UVUNITsRswDu+usR3/t5LDd7hM0jYQr5mv9BU/6oKUWgdedM8a5BDQMQtWF5eA ClearTextChars.Length=48,CypherData.Length=64,u8UVUNITsRswDu+usR3/t5LDd7hM0jYQr5mv9BU/6oKUWgdedM8a5BDQMQtWF5e5HTHV4UCSuS5YsTlhNZwt+g== ClearTextChars.Length=63,CypherData.Length=64,u8UVUNITsRswDu+usR3/t5LDd7hM0jYQr5mv9BU/6oKUWgdedM8a5BDQMQtWF5e5NMSh/6GlzSMfscnk1Sc4bg== ClearTextChars.Length=64,CypherData.Length=80,u8UVUNITsRswDu+usR3/t5LDd7hM0jYQr5mv9BU/6oKUWgdedM8a5BDQMQtWF5e5NMSh/6GlzSMfscnk1Sc4W+Pb0ijEWKmgNJfGsQigc6A= 按任意键继续。 您正在使用PKCS#7填充。此方案将始终向纯文本添加字节,即使它已经是块对齐的

在您的例子中,明文被填充为16个字节,值为“16”。因此,在密文中会有一个额外的块

如果您的其他代码没有预料到这一点,那么它就没有使用PKCS#7填充。或者实现被破坏。

您正在使用PKCS#7填充。此方案将始终向纯文本添加字节,即使它已经是块对齐的

在您的例子中,明文被填充为16个字节,值为“16”。因此,在密文中会有一个额外的块


如果您的其他代码没有预料到这一点,那么它就没有使用PKCS#7填充。或者执行失败。

Duncan,感谢您的快速响应。根据您的回答,我可以找到以下链接:[和[Duncan,感谢您的快速回复。根据您的回答,我可以找到以下链接:[和][