📄 ch8_10.cs
字号:
using System;
using System.IO ;
using System.Security;
using System.Security.Cryptography;
public class StoreCryptoStream : ICryptoStream
{
static byte[] tag1 = {(byte)'[',(byte)'S',(byte)'a',(byte)'u' ,(byte)'d' ,(byte)'e',(byte)'s' ,(byte)']'};
static byte[] tag2= {(byte)'[',(byte)'S',(byte)'a',(byte)'u' ,(byte)'r' ,(byte)'c',(byte)'2' ,(byte)']'};
FileStream fs;
public StoreCryptoStream(FileStream fout)
{
fs=fout ;
}
public virtual void CloseStream() {fs.Close();}
public virtual void CloseStream(Object obj) {fs.Close();}
public virtual void SetSink(ICryptoStream pstm) {}
public virtual void SetSource(CryptographicObject co) {}
public virtual ICryptoStream GetSink () {return null;}
public virtual void Write(byte[] bin)
{
int len = bin.GetLength(0);
Write(bin, 0, len);
}
public virtual void Write(byte[] bin, int start, int len )
{
fs.Write(bin,start,len);
}
}
public class CH8_10
{
private byte[] symKey ;
private byte[] symIV ;
private bool GenerateKey(string password)
{
try
{
int i;
int len;
char[] cp = password.ToCharArray();
len = cp.GetLength(0) ;
byte[] bt = new byte[len];
for(i=0 ; i<len ;i++)
{
bt[i] =(byte) cp[i];
}
symKey=new byte[8] ;
symIV = new byte[8] ;
SHA1_CSP sha = new SHA1_CSP() ;
sha.Write(bt) ;
sha.CloseStream() ;
for(i=0 ; i<8 ; i++)
{
symKey[i] = sha.Hash[i] ;
}
for(i=8 ; i<16 ; i++)
{
symIV[i-8]= sha.Hash[i] ;
}
return true;
}
catch(Exception e)
{
Console.WriteLine("A Exception Occured in Generating Keys :"+e.ToString()) ;
return false ;
}
}
private void DecryptData(string infile, string outfile)
{
try
{
FileStream fin = new FileStream(infile,FileMode.Open,FileAccess.Read);
FileStream fout = new FileStream(outfile,FileMode.OpenOrCreate,FileAccess.Write);
fout.SetLength(0) ;
byte[] bin = new byte[4096] ;
long totlen = fin.Length ;
long rdlen=8;
int len ;
// Create the basic objects
SymmetricAlgorithm des = new DES_CSP() ;
// Create the stream.
StoreCryptoStream scs = new StoreCryptoStream(fout);
SymmetricStreamDecryptor ssd = des.CreateDecryptor(symKey, symIV);
ssd.SetSink(scs);
scs.SetSource(ssd);
while (rdlen < totlen)
{
len = fin.Read(bin,0,4096);
ssd.Write(bin,0,len);
rdlen = rdlen + len;
}
// Free up the resources
ssd.CloseStream();
fin.Close();
fout.Close();
}
catch(Exception e)
{
Console.WriteLine("An exception occured while decrypting :" +e.ToString());
}
}
public static void Main(string[] args)
{
CH8_10 app = new CH8_10();
app.GenerateKey(args[0]);
app.DecryptData( args[1], args[2] );
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -