网络安全

位置:IT落伍者 >> 网络安全 >> 浏览文章

DOTNET的加密技术应用


发布日期:2021年08月12日
 
DOTNET的加密技术应用

using System;

using SystemText;

using SystemSecurity;

using SystemSecurityCryptography;

using SystemIO;

namespace EncryptClasses

{

/// <summary>

/// 此处定义的是DES加密为了便于今后的管理和维护

/// 请不要随便改动密码或者改变了密码后请一定要

/// 牢记先前的密码否则将会照成不可预料的损失

/// </summary>

public class DESEncrypt

{

#region member fields

private string iv=;

private string key=;

private Encoding encoding=new UnicodeEncoding();

private DES des;

#endregion

/// <summary>

/// 构造函数

/// </summary>

public DESEncrypt()

{

des=new DESCryptoServiceProvider();

}

#region propertys

/// <summary>

/// 设置加密密钥

/// </summary>

public string EncryptKey

{

get{return thiskey;}

set

{

thiskey=value;

}

}

/// <summary>

/// 要加密字符的编码模式

/// </summary>

public Encoding EncodingMode

{

get{return thisencoding;}

set{thisencoding=value;}

}

#endregion

#region methods

/// <summary>

/// 加密字符串并返回加密后的结果

/// </summary>

/// <param name=str></param>

/// <returns></returns>

public string EncryptString(string str)

{

byte[] ivb=EncodingASCIIGetBytes(thisiv);

byte[] keyb=EncodingASCIIGetBytes(thisEncryptKey);//得到加密密钥

byte[] toEncrypt=thisEncodingModeGetBytes(str);//得到要加密的内容

byte[] encrypted;

ICryptoTransform encryptor=desCreateEncryptor(keybivb);

MemoryStream msEncrypt=new MemoryStream();

CryptoStream csEncrypt=new CryptoStream(msEncryptencryptorCryptoStreamModeWrite);

csEncryptWrite(toEncrypttoEncryptLength);

csEncryptFlushFinalBlock();

encrypted=msEncryptToArray();

csEncryptClose();

msEncryptClose();

return thisEncodingModeGetString(encrypted);

}

/// <summary>

/// 加密指定的文件如果成功返回True否则false

/// </summary>

/// <param name=filePath>要加密的文件路径</param>

/// <param name=outPath>加密后的文件输出路径</param>

public void EncryptFile(string filePathstring outPath)

{

bool isExist=FileExists(filePath);

if(isExist)//如果存在

{

byte[] ivb=EncodingASCIIGetBytes(thisiv);

byte[] keyb=EncodingASCIIGetBytes(thisEncryptKey);

//得到要加密文件的字节流

FileStream fin=new FileStream(filePathFileModeOpenFileAccessRead);

StreamReader reader=new StreamReader(finthisEncodingMode);

string dataStr=readerReadToEnd();

byte[] toEncrypt=thisEncodingModeGetBytes(dataStr);

finClose();

FileStream fout=new FileStream(outPathFileModeCreateFileAccessWrite);

ICryptoTransform encryptor=desCreateEncryptor(keybivb);

CryptoStream csEncrypt=new CryptoStream(foutencryptorCryptoStreamModeWrite);

try

{

//加密得到的文件字节流

csEncryptWrite(toEncrypttoEncryptLength);

csEncryptFlushFinalBlock();

}

catch(Exception err)

{

throw new ApplicationException(errMessage);

}

finally

{

try

{

foutClose();

csEncryptClose();

}

catch

{

;

}

}

}

else

{

throw new FileNotFoundException(没有找到指定的文件);

}

}

/// <summary>

/// 文件加密函数的重载版本如果不指定输出路径

/// 那么原来的文件将被加密后的文件覆盖

/// </summary>

/// <param name=filePath></param>

public void EncryptFile(string filePath)

{

thisEncryptFile(filePathfilePath);

}

/// <summary>

/// 解密给定的字符串

/// </summary>

/// <param name=str>要解密的字符</param>

/// <returns></returns>

public string DecryptString(string str)

{

byte[] ivb=EncodingASCIIGetBytes(thisiv);

byte[] keyb=EncodingASCIIGetBytes(thisEncryptKey);

byte[] toDecrypt=thisEncodingModeGetBytes(str);

byte[] deCrypted=new byte[toDecryptLength];

ICryptoTransform deCryptor=desCreateDecryptor(keybivb);

MemoryStream msDecrypt=new MemoryStream(toDecrypt);

CryptoStream csDecrypt=new CryptoStream(msDecryptdeCryptorCryptoStreamModeRead);

try

{

csDecryptRead(deCrypteddeCryptedLength);

}

catch(Exception err)

{

throw new ApplicationException(errMessage);

}

finally

{

try

{

msDecryptClose();

csDecryptClose();

}

catch{;}

}

return thisEncodingModeGetString(deCrypted);

}

/// <summary>

/// 解密指定的文件

/// </summary>

/// <param name=filePath>要解密的文件路径</param>

/// <param name=outPath>解密后的文件输出路径</param>

public void DecryptFile(string filePathstring outPath)

{

bool isExist=FileExists(filePath);

if(isExist)//如果存在

{

byte[] ivb=EncodingASCIIGetBytes(thisiv);

byte[] keyb=EncodingASCIIGetBytes(thisEncryptKey);

FileInfo file=new FileInfo(filePath);

byte[] deCrypted=new byte[fileLength];

//得到要解密文件的字节流

FileStream fin=new FileStream(filePathFileModeOpenFileAccessRead);

//解密文件

try

{

ICryptoTransform decryptor=desCreateDecryptor(keybivb);

CryptoStream csDecrypt=new CryptoStream(findecryptorCryptoStreamModeRead);

csDecryptRead(deCrypteddeCryptedLength);

}

catch(Exception err)

{

throw new ApplicationException(errMessage);

}

finally

{

try

{

finClose();

}

catch{;}

}

FileStream fout=new FileStream(outPathFileModeCreateFileAccessWrite);

foutWrite(deCrypteddeCryptedLength);

foutClose();

}

else

{

throw new FileNotFoundException(指定的解密文件没有找到);

}

}

/// <summary>

/// 解密文件的重载版本如果没有给出解密后文件的输出路径

/// 则解密后的文件将覆盖先前的文件

/// </summary>

/// <param name=filePath></param>

public void DecryptFile(string filePath)

{

thisDecryptFile(filePathfilePath);

}

#endregion

}

/// <summary>

/// MD加密类注意经MD加密过的信息是不能转换回原始数据的

/// 请不要在用户敏感的信息中使用此加密技术比如用户的密码

/// 请尽量使用对称加密

/// </summary>

public class MDEncrypt

{

private MD md;

public MDEncrypt()

{

md=new MDCryptoServiceProvider();

}

/// <summary>

/// 从字符串中获取散列值

/// </summary>

/// <param name=str>要计算散列值的字符串</param>

/// <returns></returns>

public string GetMDFromString(string str)

{

byte[] toCompute=EncodingUnicodeGetBytes(str);

byte[] hashed=mdComputeHash(toComputetoComputeLength);

return EncodingASCIIGetString(hashed);

}

/// <summary>

/// 根据文件来计算散列值

/// </summary>

/// <param name=filePath>要计算散列值的文件路径</param>

/// <returns></returns>

public string GetMDFromFile(string filePath)

{

bool isExist=FileExists(filePath);

if(isExist)//如果文件存在

{

FileStream stream=new FileStream(filePathFileModeOpenFileAccessRead);

StreamReader reader=new StreamReader(streamEncodingUnicode);

string str=readerReadToEnd();

byte[] toHash=EncodingUnicodeGetBytes(str);

byte[] hashed=mdComputeHash(toHashtoHashLength);

streamClose();

return EncodingASCIIGetString(hashed);

}

else//文件不存在

{

throw new FileNotFoundException(指定的文件没有找到);

}

}

}

/// <summary>

/// 用于数字签名的hash类

/// </summary>

public class MACTripleDESEncrypt

{

private MACTripleDES mact;

private string __key=ksnch;

private byte[] __data=null;

public MACTripleDESEncrypt()

{

mact=new MACTripleDES();

}

/// <summary>

/// 获取或设置用于数字签名的密钥

/// </summary>

public string Key

{

get{return this__key;}

set

{

int keyLength=valueLength;

int[] keyAllowLengths=new int[]{};

bool isRight=false;

foreach(int i in keyAllowLengths)

{

if(keyLength==keyAllowLengths[i])

{

isRight=true;

break;

}

}

if(!isRight)

throw new ApplicationException(用于数字签名的密钥长度必须是值之一);

else

this__key=value;

}

}

/// <summary>

/// 获取或设置用于数字签名的用户数据

/// </summary>

public byte[] Data

{

get{return this__data;}

set{this__data=value;}

}

/// <summary>

/// 得到签名后的hash值

/// </summary>

/// <returns></returns>

public string GetHashValue()

{

if(thisData==null)

throw new NotSetSpecialPropertyException(没有设置要进行数字签名的用户+

数据(property:Data));

byte[] key=EncodingASCIIGetBytes(thisKey);

thismactKey=key;

byte[] hash_b=thismactComputeHash(thismactComputeHash(thisData));

return EncodingASCIIGetString(hash_b);

}

}

}

               

上一篇:常见dotNet加密保护工具分析介绍

下一篇:看看大网站到底是如何保障网络安全的