在使用
通常我們需要將加密密鑰和初始化向量傳遞給另一個人
那麼如何創建加密密鑰和初始化向量呢?有兩種基本方法
代碼清單
using System;
using System
using System
namespace Encription
{
class Program
{
static void Main(string[] args)
{
AesCryptoServiceProvider acsp = new AesCryptoServiceProvider();
WriteKeyAndIV(acsp);
AesManaged am = new AesManaged();
WriteKeyAndIV(am);
DESCryptoServiceProvider dsp = new DESCryptoServiceProvider();
WriteKeyAndIV(dsp);
TripleDESCryptoServiceProvider tdsp = new TripleDESCryptoServiceProvider();
WriteKeyAndIV(tdsp);
RijndaelManaged rm = new RijndaelManaged();
WriteKeyAndIV(rm);
Console
}
static void WriteKeyAndIV(SymmetricAlgorithm sa)
{
Console
Console
Console
Console
}
static string GetStringFromByte(byte[] bytes)
{
string s=
for (int i =
{
s += bytes[i]
}
return s;
}
}
}
如代碼清單
圖
如圖
代碼清單
using System;
using System
using System
using System
using System
namespace Encription
{
class Program
{
static void Main(string[] args)
{
AesCryptoServiceProvider acsp = new AesCryptoServiceProvider();
WriteKeyAndIV(acsp);
acsp
acsp
WriteKeyAndIV(acsp);
Console
}
static void WriteKeyAndIV(SymmetricAlgorithm sa)
{
Console
Console
Console
Console
}
static string GetStringFromByte(byte[] bytes)
{
string s=
for (int i =
{
s += bytes[i]
}
return s;
}
}
}
如代碼清單
圖
如圖
我們的准備工作完成了
代碼清單
using System;
using System
using System
using System
using System
using System
namespace Sample
{
class Program
{
static AesCryptoServiceProvider acsp = new AesCryptoServiceProvider();
static void Main(string[] args)
{
byte[] key = acsp
byte[] iv = acsp
string s = @
byte[] sbyt = Encoding
byte []Enb = Encript(sbyt
byte []Deb = Decript(Enb
Console
Console
Console
}
public static byte[] Encript(byte[] s
{
MemoryStream mstream = new MemoryStream();
CryptoStream cstream = new CryptoStream(mstream
cstream
cstream
byte[] outb
cstream
mstream
return outb
}
public static byte[] Decript(byte[] s
{
MemoryStream mtream
CryptoStream deStreame = new CryptoStream(mtream
deStreame
deStreame
byte[] outs
mtream
deStreame
return outs
}
public static byte[] GetByteFromstring(string s)
{
return Encoding
}
}
}
如代碼清單
接下來我們介紹Main方法中調用的兩個靜態方法Encript和Decript方法
現在我們看看改程序的運行結果
圖
從圖
From:http://tw.wingwit.com/Article/program/net/201311/11895.html