DES加解密算法的实例
- 格式:doc
- 大小:24.00 KB
- 文档页数:3
using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;
using System.IO;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
// DESCryptoServiceProvider desCry= new DESCryptoServiceProvider();
// desCry.GenerateKey();
// desCry.GenerateIV();
// byte[] key = desCry.Key;
// byte[] iv = desCry.IV;
// UnicodeEncoding cncoder = new UnicodeEncoding();
// string s = cncoder.GetString(key);
// string ivs = cncoder.GetString(iv);
// Console.WriteLine(ivs);
// Console.WriteLine(s);
// Console.WriteLine(key.LongLength);
// Console.WriteLine(ivs.Length);
//ÓÃDESËã·¨¼ÓÃÜ
string s = "12345";
string key = "abcdefgh";
string iv = "abcdefgh";
ASCIIEncoding ASC = new ASCIIEncoding();
byte[] keys = ASC.GetBytes(key);
byte[] ivs = ASC.GetBytes(iv);
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
des.Key = keys;
des.IV = ivs;
FileStream filestream = new FileStream("des.txt", FileMode.Create);
CryptoStream crytostream = new CryptoStream(filestream, des.CreateEncryptor(keys, ivs), CryptoStreamMode.Write);
StreamWriter writer = new StreamWriter(crytostream);
writer.WriteLine(s);
writer.Close();
}
}
}
//DES½âÃÜËã·¨
using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;
using System.IO;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
// DESCryptoServiceProvider desCry= new DESCryptoServiceProvider();
// desCry.GenerateKey();
// desCry.GenerateIV();
// byte[] key = desCry.Key;
// byte[] iv = desCry.IV;
// UnicodeEncoding cncoder = new UnicodeEncoding();
// string s = cncoder.GetString(key);
// string ivs = cncoder.GetString(iv);
// Console.WriteLine(ivs);
// Console.WriteLine(s);
// Console.WriteLine(key.LongLength);
// Console.WriteLine(ivs.Length);
//ÓÃDESËã·¨½âÃÜ
string key = "abcdefgh";
string iv = "abcdefgh";
ASCIIEncoding ASC = new ASCIIEncoding();
byte[] keys = ASC.GetBytes(key);
byte[] ivs = ASC.GetBytes(iv);
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
des.Key = keys;
des.IV = ivs;
FileStream filestream = new FileStream("des.txt",
FileMode.Open);
CryptoStream cryStream = new CryptoStream(filestream, des.CreateDecryptor(), CryptoStreamMode.Read);
StreamReader reader = new StreamReader(cryStream);
string s =reader.ReadLine();
Console.WriteLine(s);
}
}
}