因为演示需要,要比较一下AES和RSA加密解密的效率差异。
我们知道RSA算法作为非对称加密的代表,有优秀的安全性,但是加密效率比较低,
查了一些资料说一般是对称算法的1/10左右。
但是我在测试的时候遇到一个奇怪的问题:
在通过vs2005debug运行的时候,解密得到数据如下图:点这个链接看吧
但是通过直接点击生成的应用程序运行, 得到如下数据: :点这个链接看吧加密过程雷同。我贴一下解密的程序://Decrypt
private void btnDecrypt_Click(object sender, EventArgs e)
{
this.dataGridView1.Rows.Clear();
string temp = "ASDFGHK:";
string AESKey = "1234567890123456";
ASCIIEncoding asc = new ASCIIEncoding(); byte[] bAESKey = asc.GetBytes(AESKey);
string pubKey = "<RSAKeyValue><Modulus>4nEGyIsFJWS0qEgGPFFX3jakJwm/EsyPFW5gAX“+
”OJpw/DgPsWXkMUxVs3X8XDRuzaT+RarF2Q1YJ/CfAzVZGBLl/Xu/n9a7QTUE7“+
“nPn3afvXAydQNwcL+8qdCS35CMpR/cpju/tGFksEY+vndxdVP63PsE71ITtJx“+
”wC1VNm3NWBU=</Modulus><Exponent>AQAB</Exponent></RSAKeyValue>"; int count = Convert.ToInt32(this.txtLoopTimes.Text);
for (int i = 0; i < count; i++)
{
this.dataGridView1.Rows.Add(1);
this.dataGridView1.Rows[i].Cells[0].Value = (i + 1).ToString();
this.dataGridView1.Rows[i].Cells[1].Value = (asc.GetByteCount(temp) * 8);
int j = 0;
//AES encrypt
byte[] tempB_AES = AES.AESEncrypt(asc.GetBytes(temp), bAESKey, bAESKey); //RSA encrypt
RsaOperation rsaOper = new RsaOperation();
byte[] tempB_RSA = rsaOper.Encrypt(temp, pubKey);
rsaOper = null; DateTime sTime = DateTime.Now;
while (j < 1000)
{
byte[] tempB = AES.AESDecrypt(tempB_AES, bAESKey, bAESKey);
tempB = null;
j++;
}
DateTime eTime = DateTime.Now;
TimeSpan span = eTime - sTime;
string tmpStr = span.ToString();
Decimal d;
if (tmpStr.IndexOf('.') > 0)
{
d = Convert.ToDecimal(tmpStr.Substring(tmpStr.IndexOf('.'), tmpStr.Length - tmpStr.IndexOf('.') - 1)) * 1000;
}
else
d = 0;
this.dataGridView1.Rows[i].Cells[2].Value = (span.Minutes * 60000) + (span.Seconds * 1000) + d; //span = null;
j = 0; byte[] C_pri = asc.GetBytes("<RSAKeyValue><Modulus>4nEGyIsFJWS0qEgGPFFX3jakJwm/EsyP”+
“FW5gAXOJpw/DgPsWXkMUxVs3X8XDRuzaT+RarF2Q1YJ/CfAzVZGBLl/Xu/n9a7”+
“QTUE7nPn3afvXAydQNwcL+8qdCS35CMpR/cpju/tGFksEY+vndxdVP63PsE71IT”+
“tJxwC1VNm3NWBU=</Modulus><Exponent>AQAB</Exponent><P>8flE4Y6Y2U0M34”+
“6WQse4kb0nmg9JoBVpUA1yCAG+O+p5JUByxi0v1CpKVZSh9Hq9rDBe1iUunoo65blcWm4pW”+
“Q==</P><Q>75FFEsss1ekkqWIAAiMqLl4SF/Nr9RZnitz2A0UvXw0bCsbDbEGVCHdz20NnFKq”+
"p27tOJdkmx1dsHr6tSPnRHQ==</Q><DP>rJzULU9K7CDR4RxXRnYwzQ4xlO346JC9mpSs1tdffGBs"+
"YU2TUpnjd6dfdaNc0QlKCu5KiNrnYcNb9op/Pic9qQ==</DP><DQ>c4UHW8KhQh0EUrEw89blFirCas4"+
"0dqBxGWLnNjh8WUdGjPG/dkpBhS2krkxYPnz1H0G4s0N9EY60Ujb79D6SeQ==</DQ><InverseQ>Qrohxtn"+
"P0ixij7mMcJ5/algtOv69Sf7D4CtHxykXgUdUOYJBtgcrgiViCkKPz78XB92MUZDvCr4sk01Mr1j/Bw==</Inver"+
"seQ><D>jcB3oJ72b7wlI5tC3AuH2AGYrR96z4fiomXgD8iGCWDi4CFOlV+k5w79ESU543JuD8lBwdkoy5/PaydE23Ikz"+
Peb146ULV+qFJOUnyZVSRKEPEAzbkJZgzTqZ+YYycrzdRVy07JTkjxNMG4e+1tL2h2o+qpRZqTBaB6epmVlI0E=</D></RSAKeyValue>"); sTime = DateTime.Now;
while (j < 1000)
{
RsaOperation rsaOper1 = new RsaOperation();
string tempB = rsaOper1.Decrypt(tempB_RSA,C_pri);
tempB = null;
rsaOper1 = null;
j++;
}
eTime = DateTime.Now;
span = eTime - sTime;
//this.dataGridView1.Rows[i].Cells[3].Value = span.ToString();
tmpStr = span.ToString();
//Decimal d;
if (tmpStr.IndexOf('.') > 0)
{
d = Convert.ToDecimal(tmpStr.Substring(tmpStr.IndexOf('.'), tmpStr.Length - tmpStr.IndexOf('.') - 1)) * 1000;
}
else
d = 0;
this.dataGridView1.Rows[i].Cells[3].Value = (span.Minutes * 60000) + (span.Seconds * 1000) + d;
//span = null;
temp = temp + temp;
tempB_AES = null;
tempB_RSA = null; }
} //AESDecrypt Method
public static Byte[] AESDecrypt(Byte[] Data, Byte[] bKey, Byte[] bVector)
{
//Byte[] bKey = new Byte[32];
//Array.Copy(Encoding.UTF8.GetBytes(Key.PadRight(bKey.Length)), bKey, bKey.Length);
//Byte[] bVector = new Byte[16];
//Array.Copy(Encoding.UTF8.GetBytes(Vector.PadRight(bVector.Length)), bVector, bVector.Length); Byte[] original = null; Rijndael Aes = Rijndael.Create();
try
{
using (MemoryStream Memory = new MemoryStream(Data))
{
using (CryptoStream Decryptor = new CryptoStream(Memory,
Aes.CreateDecryptor(bKey, bVector),
CryptoStreamMode.Read))
{
using (MemoryStream originalMemory = new MemoryStream())
{
Byte[] Buffer = new Byte[1024];
Int32 readBytes = 0;
while ((readBytes = Decryptor.Read(Buffer, 0, Buffer.Length)) > 0)
{
originalMemory.Write(Buffer, 0, readBytes);
} original = originalMemory.ToArray();
}
}
}
}
catch
{
original = null;
} return original;
}//RSA Decrypt Method
public string Decrypt(byte[] EnContentsByte, byte[] prvKey)
{
try
{
byte[] prvkeyBYtes = prvKey;
string PrvKeyStr = UnicodeEncoding.Unicode.GetString(prvkeyBYtes, 0, prvkeyBYtes.Length);
RSACryptoServiceProvider dec = new RSACryptoServiceProvider();
dec.FromXmlString(PrvKeyStr);
byte[] tempEnContentsByte = new byte[128];
byte[] tempDecontentsByte = new byte[117];
byte[] DeContentsByte = new byte[117 * EnContentsByte.Length / 128];
for (int i = 0; i < EnContentsByte.Length / 128; i++)
{
Array.Copy(EnContentsByte, 128 * i, tempEnContentsByte, 0, 128);
tempDecontentsByte = dec.Decrypt(tempEnContentsByte, false);
Array.Copy(tempDecontentsByte, 0, DeContentsByte, 117 * i, 117);
Array.Clear(tempDecontentsByte, 0, 117);
Array.Clear(tempEnContentsByte, 0, 128);
} UTF8Encoding enstr = new UTF8Encoding();
return enstr.GetString(DeContentsByte);
}
catch (Exception ex)
{
return null;
}
}程序有点乱。请了解的人指点一下迷津。
万分感谢。
我们知道RSA算法作为非对称加密的代表,有优秀的安全性,但是加密效率比较低,
查了一些资料说一般是对称算法的1/10左右。
但是我在测试的时候遇到一个奇怪的问题:
在通过vs2005debug运行的时候,解密得到数据如下图:点这个链接看吧
但是通过直接点击生成的应用程序运行, 得到如下数据: :点这个链接看吧加密过程雷同。我贴一下解密的程序://Decrypt
private void btnDecrypt_Click(object sender, EventArgs e)
{
this.dataGridView1.Rows.Clear();
string temp = "ASDFGHK:";
string AESKey = "1234567890123456";
ASCIIEncoding asc = new ASCIIEncoding(); byte[] bAESKey = asc.GetBytes(AESKey);
string pubKey = "<RSAKeyValue><Modulus>4nEGyIsFJWS0qEgGPFFX3jakJwm/EsyPFW5gAX“+
”OJpw/DgPsWXkMUxVs3X8XDRuzaT+RarF2Q1YJ/CfAzVZGBLl/Xu/n9a7QTUE7“+
“nPn3afvXAydQNwcL+8qdCS35CMpR/cpju/tGFksEY+vndxdVP63PsE71ITtJx“+
”wC1VNm3NWBU=</Modulus><Exponent>AQAB</Exponent></RSAKeyValue>"; int count = Convert.ToInt32(this.txtLoopTimes.Text);
for (int i = 0; i < count; i++)
{
this.dataGridView1.Rows.Add(1);
this.dataGridView1.Rows[i].Cells[0].Value = (i + 1).ToString();
this.dataGridView1.Rows[i].Cells[1].Value = (asc.GetByteCount(temp) * 8);
int j = 0;
//AES encrypt
byte[] tempB_AES = AES.AESEncrypt(asc.GetBytes(temp), bAESKey, bAESKey); //RSA encrypt
RsaOperation rsaOper = new RsaOperation();
byte[] tempB_RSA = rsaOper.Encrypt(temp, pubKey);
rsaOper = null; DateTime sTime = DateTime.Now;
while (j < 1000)
{
byte[] tempB = AES.AESDecrypt(tempB_AES, bAESKey, bAESKey);
tempB = null;
j++;
}
DateTime eTime = DateTime.Now;
TimeSpan span = eTime - sTime;
string tmpStr = span.ToString();
Decimal d;
if (tmpStr.IndexOf('.') > 0)
{
d = Convert.ToDecimal(tmpStr.Substring(tmpStr.IndexOf('.'), tmpStr.Length - tmpStr.IndexOf('.') - 1)) * 1000;
}
else
d = 0;
this.dataGridView1.Rows[i].Cells[2].Value = (span.Minutes * 60000) + (span.Seconds * 1000) + d; //span = null;
j = 0; byte[] C_pri = asc.GetBytes("<RSAKeyValue><Modulus>4nEGyIsFJWS0qEgGPFFX3jakJwm/EsyP”+
“FW5gAXOJpw/DgPsWXkMUxVs3X8XDRuzaT+RarF2Q1YJ/CfAzVZGBLl/Xu/n9a7”+
“QTUE7nPn3afvXAydQNwcL+8qdCS35CMpR/cpju/tGFksEY+vndxdVP63PsE71IT”+
“tJxwC1VNm3NWBU=</Modulus><Exponent>AQAB</Exponent><P>8flE4Y6Y2U0M34”+
“6WQse4kb0nmg9JoBVpUA1yCAG+O+p5JUByxi0v1CpKVZSh9Hq9rDBe1iUunoo65blcWm4pW”+
“Q==</P><Q>75FFEsss1ekkqWIAAiMqLl4SF/Nr9RZnitz2A0UvXw0bCsbDbEGVCHdz20NnFKq”+
"p27tOJdkmx1dsHr6tSPnRHQ==</Q><DP>rJzULU9K7CDR4RxXRnYwzQ4xlO346JC9mpSs1tdffGBs"+
"YU2TUpnjd6dfdaNc0QlKCu5KiNrnYcNb9op/Pic9qQ==</DP><DQ>c4UHW8KhQh0EUrEw89blFirCas4"+
"0dqBxGWLnNjh8WUdGjPG/dkpBhS2krkxYPnz1H0G4s0N9EY60Ujb79D6SeQ==</DQ><InverseQ>Qrohxtn"+
"P0ixij7mMcJ5/algtOv69Sf7D4CtHxykXgUdUOYJBtgcrgiViCkKPz78XB92MUZDvCr4sk01Mr1j/Bw==</Inver"+
"seQ><D>jcB3oJ72b7wlI5tC3AuH2AGYrR96z4fiomXgD8iGCWDi4CFOlV+k5w79ESU543JuD8lBwdkoy5/PaydE23Ikz"+
Peb146ULV+qFJOUnyZVSRKEPEAzbkJZgzTqZ+YYycrzdRVy07JTkjxNMG4e+1tL2h2o+qpRZqTBaB6epmVlI0E=</D></RSAKeyValue>"); sTime = DateTime.Now;
while (j < 1000)
{
RsaOperation rsaOper1 = new RsaOperation();
string tempB = rsaOper1.Decrypt(tempB_RSA,C_pri);
tempB = null;
rsaOper1 = null;
j++;
}
eTime = DateTime.Now;
span = eTime - sTime;
//this.dataGridView1.Rows[i].Cells[3].Value = span.ToString();
tmpStr = span.ToString();
//Decimal d;
if (tmpStr.IndexOf('.') > 0)
{
d = Convert.ToDecimal(tmpStr.Substring(tmpStr.IndexOf('.'), tmpStr.Length - tmpStr.IndexOf('.') - 1)) * 1000;
}
else
d = 0;
this.dataGridView1.Rows[i].Cells[3].Value = (span.Minutes * 60000) + (span.Seconds * 1000) + d;
//span = null;
temp = temp + temp;
tempB_AES = null;
tempB_RSA = null; }
} //AESDecrypt Method
public static Byte[] AESDecrypt(Byte[] Data, Byte[] bKey, Byte[] bVector)
{
//Byte[] bKey = new Byte[32];
//Array.Copy(Encoding.UTF8.GetBytes(Key.PadRight(bKey.Length)), bKey, bKey.Length);
//Byte[] bVector = new Byte[16];
//Array.Copy(Encoding.UTF8.GetBytes(Vector.PadRight(bVector.Length)), bVector, bVector.Length); Byte[] original = null; Rijndael Aes = Rijndael.Create();
try
{
using (MemoryStream Memory = new MemoryStream(Data))
{
using (CryptoStream Decryptor = new CryptoStream(Memory,
Aes.CreateDecryptor(bKey, bVector),
CryptoStreamMode.Read))
{
using (MemoryStream originalMemory = new MemoryStream())
{
Byte[] Buffer = new Byte[1024];
Int32 readBytes = 0;
while ((readBytes = Decryptor.Read(Buffer, 0, Buffer.Length)) > 0)
{
originalMemory.Write(Buffer, 0, readBytes);
} original = originalMemory.ToArray();
}
}
}
}
catch
{
original = null;
} return original;
}//RSA Decrypt Method
public string Decrypt(byte[] EnContentsByte, byte[] prvKey)
{
try
{
byte[] prvkeyBYtes = prvKey;
string PrvKeyStr = UnicodeEncoding.Unicode.GetString(prvkeyBYtes, 0, prvkeyBYtes.Length);
RSACryptoServiceProvider dec = new RSACryptoServiceProvider();
dec.FromXmlString(PrvKeyStr);
byte[] tempEnContentsByte = new byte[128];
byte[] tempDecontentsByte = new byte[117];
byte[] DeContentsByte = new byte[117 * EnContentsByte.Length / 128];
for (int i = 0; i < EnContentsByte.Length / 128; i++)
{
Array.Copy(EnContentsByte, 128 * i, tempEnContentsByte, 0, 128);
tempDecontentsByte = dec.Decrypt(tempEnContentsByte, false);
Array.Copy(tempDecontentsByte, 0, DeContentsByte, 117 * i, 117);
Array.Clear(tempDecontentsByte, 0, 117);
Array.Clear(tempEnContentsByte, 0, 128);
} UTF8Encoding enstr = new UTF8Encoding();
return enstr.GetString(DeContentsByte);
}
catch (Exception ex)
{
return null;
}
}程序有点乱。请了解的人指点一下迷津。
万分感谢。
尤其是在可执行程序的运行中,RSA算法在对大数据操作的解密效率竟然比AES高。
是我算法的问题,还是平台的问题?
具体原因说不出来,遇到过这种情况
因为提供的RSA算法 一次最多只能加密长度为117字节的数据~~~~~