关于 C# 中 Rijndael 类的使用?
3di 2005-12-22 06:08:37 这个算法的明密文长度应该是什么关系?
我用一下方法加密后,长度变的很长(20位的明文成了64位的密文):
public static string Encrypt(string pToEncrypt, string sKey, int keySize)
{
sKey = ProcessKey(sKey, keySize);
RijndaelManaged crypt = new RijndaelManaged();
//把字符串放到byte数组中
//原来使用的UTF8编码,我改成Unicode编码了,不行
byte[] inputByteArray = Encoding.Default.GetBytes(pToEncrypt);
//byte[] inputByteArray=Encoding.Unicode.GetBytes(pToEncrypt);
//建立加密对象的密钥和偏移量
//原文使用ASCIIEncoding.ASCII方法的GetBytes方法
//使得输入密码必须输入英文文本
crypt.KeySize = keySize * 8;
crypt.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
crypt.IV = ASCIIEncoding.ASCII.GetBytes(sKey.Substring(0, 16));
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, crypt.CreateEncryptor(), CryptoStreamMode.Write);
//Write the byte array into the crypto stream
//(It will end up in the memory stream)
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
//Get the data back from the memory stream, and into a string
StringBuilder ret = new StringBuilder();
foreach(byte b in ms.ToArray())
{
//Format as hex
ret.AppendFormat("{0:X2}", b);
}
return ret.ToString();
}
应该怎么减小密文长度?