using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Security;
using System.Security.Cryptography;
using System.Runtime.InteropServices;
namespace des_test
{
class Class1
{
// Call this function to remove the key from memory after use for security
[System.Runtime.InteropServices.DllImport("KERNEL32.DLL", EntryPoint="RtlZeroMemory")]
public static extern bool ZeroMemory(IntPtr Destination, int Length);
static Byte[] EncryptData(Byte[] Srcbyte, SymmetricAlgorithm key)
{
ICryptoTransform desencrypt = key.CreateEncryptor();
Byte[] result1 = desencrypt.TransformFinalBlock(Srcbyte,0,Srcbyte.Length);
return result1;
} static Byte[] DecryptData(Byte[] Desbyte, SymmetricAlgorithm key)
{
ICryptoTransform desencrypt = key.CreateDecryptor();
Byte[] result = desencrypt.TransformFinalBlock(Desbyte, 0, Desbyte.Length);
return result;
} class Program
{
static void Main(string[] args)
{
DESCryptoServiceProvider key = new DESCryptoServiceProvider(); Byte[] Srcbyte = new Byte[16];
Byte[] Desbyte = new Byte[18];
for (int i = 0; i < 16; i++)
Srcbyte[i] = 0x88;
DecryptData((EncryptData(Srcbyte, key)),key);
Console.WriteLine("OK");
}
}
}
}加密解密都正常
但使用过程中发现,当明文是7byte,密文为8byte,当明文为8byte,密文就变成16byte
按照DES原理,应该只要明文是8byte的倍数时,密文应该跟密文一样长啊
达人给指点一下
密文长度应该不会大于明文8byte吧