主题:.NET下的文件加密技术
类型:技术趋势、解决方案、行业前景、核心算法等
我自己也不是很清楚
开一个帖子大家讨论一下朋友如果关注就顶一下

解决方案 »

  1.   

    一些例子 http://www.example-code.com/csharp/encryption.asp
      

  2.   

    oolongTea:老兄:英文地耶。看不太懂
    不过,谢谢
      

  3.   

    zhe ge wang shang xia zai  qu
      

  4.   

    using System; 
    using System.Security.Cryptography; 
    using System.Text; 
    using System.IO; 
    using System.Windows.Forms; namespace Curllion 

    public class Crypt 

    private byte[] key; 
    private byte[] iv; 
    private System.Text.ASCIIEncoding asciiEncoding; 
    private System.Text.UnicodeEncoding textConverter; 
    private RC2CryptoServiceProvider rc2CSP; 
    public Crypt() 

    InitializeComponent(); 

    private void InitializeComponent() 

    key = new byte[]{106,51,25,141,157,142,23,111,234,159,187,154,215,34,37,204}; 
    iv = new byte[]{135,186,133,136,184,149,153,144}; 
    asciiEncoding = new System.Text.ASCIIEncoding(); 
    textConverter = new System.Text.UnicodeEncoding(); 
    rc2CSP = new RC2CryptoServiceProvider(); 

    /// <summary> 
    /// 新建一个大小为10261B的文件,以便将加密数据写入固定大小的文件。 
    /// </summary> 
    /// <param name="filePath">文件保存的地址,包含文件名</param> 
    public void InitBinFile(string filePath) 

    byte[] tmp = new byte[10261]; 
    try //创建文件流,将其内容全部写入0 

    System.IO.FileStream writeFileStream = new FileStream(filePath, 
    System.IO.FileMode.Create, 
    System.IO.FileAccess.Write, 
    System.IO.FileShare.None,512,false); for(int i = 0 ;i< 10261;i++) 
    tmp[i] = 0; 
    writeFileStream.Write(tmp,0,10261); 
    writeFileStream.Flush(); 
    writeFileStream.Close(); 

    catch(System.IO.IOException) 

    MessageBox.Show("文件操作错误!","错误!",MessageBoxButtons.OK,MessageBoxIcon.Error); 


    /// <summary> 
    /// 将文本数据加密后写入一个文件,其中,这个文件是用InitBinFile建立的,这个文件将被分成十块, 
    /// 用来分别保存10组不同的数据,第一个byte位保留,第2位到第21位分别用来存放每块数据的长度,但 
    /// 一个byte的取值为0-127,所以,用两个byte来存放一个长度。 
    /// </summary> 
    /// <param name="toEncryptText">要加密的文本数据</param> 
    /// <param name="filePath">要写入的文件</param> 
    /// <param name="dataIndex">写入第几块,取值为1--10</param> 
    /// <returns>是否操作成功</returns> 
      

  5.   

    public bool EncryptToFile(string toEncryptText,string filePath,int dataIndex) 

    bool r = false; 
    if(dataIndex > 10 && dataIndex < 1) 

    MessageBox.Show("数据索引的取值范围在1至10之间!","错误!", 
    MessageBoxButtons.OK,MessageBoxIcon.Error); 
    return r; 

    byte[] encrypted; 
    //打开要写入的文件,主要是为了保持原文件的内容不丢失 
    System.IO.FileStream tmpFileStream= new FileStream(filePath, 
    System.IO.FileMode.Open, 
    System.IO.FileAccess.Read, 
    System.IO.FileShare.None,1024,true); byte[] index = new byte[10261]; 
    //将读取的内容写到byte数组 
    tmpFileStream.Read(index,0,10261); 
    tmpFileStream.Close(); 
    //定义基本的加密转换运算 
    System.Security.Cryptography.ICryptoTransform Encryptor = rc2CSP.CreateEncryptor(this.key,this.iv); 
    System.IO.MemoryStream msEncrypt = new MemoryStream(); 
    //在此加密转换流中,加密将从csEncrypt,加密后,结果在msEncrypt流中。 
    System.Security.Cryptography.CryptoStream csEncrypt = new CryptoStream(msEncrypt, 
    Encryptor,CryptoStreamMode.Write); 
    //将要加密的文本转换成UTF-16 编码,保存在tmp数组。 
    byte[] tmp = textConverter.GetBytes(toEncryptText); 
    //将tmp输入csEncrypt,将通过Encryptor来加密。 
    csEncrypt.Write(tmp,0,tmp.Length); 
    //输出到msEnctypt 
    csEncrypt.FlushFinalBlock(); 
    //将流转成byte[] 
    encrypted = msEncrypt.ToArray(); 
    if(encrypted.Length>1024) 

    MessageBox.Show("加密后,数据长度大于1KB,无法保存"); 
    return false; 

    //得到加密后数据的大小,将结果存在指定的位置。 
    index[dataIndex*2 - 1] = Convert.ToByte(Convert.ToString(encrypted.Length/128)); 
    index[dataIndex*2] = Convert.ToByte(Convert.ToString(encrypted.Length%128)); 
    //将加密后的结果写入index(覆盖) 
    for(int i=0;i<encrypted.Length;i++) 
    index[1024*(dataIndex-1)+21+i]=encrypted[i]; 
    //建立文件流 
    tmpFileStream = new FileStream(filePath, 
    System.IO.FileMode.Truncate, 
    System.IO.FileAccess.Write, 
    System.IO.FileShare.None,1024,true); 
    //写文件 
    tmpFileStream.Write(index,0,10261); 
    tmpFileStream.Flush(); 
    r = true; 
    tmpFileStream.Close(); 
    return r; 

    /// <summary> 
    /// 从一个文件中解密出一段文本,其中,这个文件是由InitBinFile建立的,并且由 EncryptToFile加密的 
    /// </summary> 
    /// <param name="filePath">要解密的文件</param> 
    /// <param name="dataIndex">要从哪一个块中解密</param> 
    /// <returns>解密后的文本</returns> 
    public string DecryptFromFile(string filePath,int dataIndex) 

    string r = ""; 
    if(dataIndex > 10 && dataIndex < 1) 

    MessageBox.Show("数据索引的取值范围在1至10之间!","错误!", 
    MessageBoxButtons.OK,MessageBoxIcon.Error); 
    return r; 

    byte[] decrypted; 
    System.IO.FileStream tmpFileStream = new FileStream(filePath, 
    System.IO.FileMode.Open, 
    System.IO.FileAccess.Read, 
    System.IO.FileShare.None,1024,true); System.Security.Cryptography.ICryptoTransform Decryptor = rc2CSP.CreateDecryptor(this.key,this.iv); 
    System.IO.MemoryStream msDecrypt = new MemoryStream(); 
    System.Security.Cryptography.CryptoStream csDecrypt = new CryptoStream(msDecrypt, 
    Decryptor,CryptoStreamMode.Write); 
    byte[] index = new byte[10261]; tmpFileStream.Read(index,0,10261); 
    int startIndex = 1024*(dataIndex-1)+21; 
    int count = index[dataIndex*2 - 1]*128 + index[dataIndex*2]; 
    byte[] tmp = new byte[count]; Array.Copy(index,1024*(dataIndex-1)+21,tmp,0,count); 
    csDecrypt.Write(tmp,0,count); 
    csDecrypt.FlushFinalBlock(); 
    decrypted = msDecrypt.ToArray(); 
    r = textConverter.GetString(decrypted,0,decrypted.Length); 
    tmpFileStream.Close(); 
    return r; 

    /// <summary> 
    /// 将一段文本加密后保存到一个文件 
    /// </summary> 
    /// <param name="toEncryptText">要加密的文本数据</param> 
    /// <param name="filePath">要保存的文件</param> 
    /// <returns>是否加密成功</returns> 
    public bool EncryptToFile(string toEncryptText,string filePath) 

    bool r = false; 
    byte[] encrypted; 
    System.IO.FileStream tmpFileStream = new FileStream(filePath, 
    System.IO.FileMode.OpenOrCreate, 
    System.IO.FileAccess.Write, 
    System.IO.FileShare.None,1024,true); System.Security.Cryptography.ICryptoTransform Encryptor = rc2CSP.CreateEncryptor(this.key,this.iv); 
    System.IO.MemoryStream msEncrypt = new MemoryStream(); 
    System.Security.Cryptography.CryptoStream csEncrypt = new CryptoStream(msEncrypt, 
    Encryptor,CryptoStreamMode.Write); byte[] tmp = textConverter.GetBytes(toEncryptText); 
    csEncrypt.Write(tmp,0,tmp.Length); 
    csEncrypt.FlushFinalBlock(); 
    encrypted = msEncrypt.ToArray(); 
      

  6.   

    tmpFileStream.Write(encrypted,0,encrypted.Length); 
    tmpFileStream.Flush(); 
    r = true; 
    tmpFileStream.Close(); 
    return r; 

    /// <summary> 
    /// 将一个被加密的文件解密 
    /// </summary> 
    /// <param name="filePath">要解密的文件</param> 
    /// <returns>解密后的文本</returns> 
    public string DecryptFromFile(string filePath) 

    string r = ""; 
    byte[] decrypted; 
    System.IO.FileStream tmpFileStream = new FileStream(filePath, 
    System.IO.FileMode.Open, 
    System.IO.FileAccess.Read, 
    System.IO.FileShare.None,1024,true); 
    System.Security.Cryptography.ICryptoTransform Decryptor = rc2CSP.CreateDecryptor(this.key,this.iv); 
    System.IO.MemoryStream msDecrypt = new MemoryStream(); 
    System.Security.Cryptography.CryptoStream csDecrypt = new CryptoStream(msDecrypt, 
    Decryptor,CryptoStreamMode.Write); byte[] tmp = new byte[tmpFileStream.Length]; 
    tmpFileStream.Read(tmp,0,tmp.Length); 
    csDecrypt.Write(tmp,0,tmp.Length); 
    csDecrypt.FlushFinalBlock(); 
    decrypted = msDecrypt.ToArray(); 
    r = textConverter.GetString(decrypted,0,decrypted.Length); 
    tmpFileStream.Close(); 
    return r; 

    //------------------------------------------------------------- 
    /// <summary> 
    /// 将文本数据加密后写入一个文件,其中,这个文件是用InitBinFile建立的,这个文件将被分成十块, 
    /// 用来分别保存10组不同的数据,第一个byte位保留,第2位到第21位分别用来存放每块数据的长度,但 
    /// 一个byte的取值为0-127,所以,用两个byte来存放一个长度。 
    /// </summary> 
    /// <param name="toEncryptText">要加密的文本数据</param> 
    /// <param name="filePath">要写入的文件</param> 
    /// <param name="dataIndex">写入第几块,取值为1--10</param> 
    /// <param name="IV">初始化向量</param> 
    /// <param name="Key">加密密匙</param> 
    /// <returns>是否操作成功</returns> 
    public bool EncryptToFile(string toEncryptText,string filePath,int dataIndex,byte[] IV,byte[] Key) 

    bool r = false; 
    if(dataIndex > 10 && dataIndex < 1) 

    MessageBox.Show("数据索引的取值范围在1至10之间!","错误!", 
    MessageBoxButtons.OK,MessageBoxIcon.Error); 
    return r; 

    byte[] encrypted; 
    //打开要写入的文件,主要是为了保持原文件的内容不丢失 
    System.IO.FileStream tmpFileStream= new FileStream(filePath, 
    System.IO.FileMode.Open, 
    System.IO.FileAccess.Read, 
    System.IO.FileShare.None,1024,true); byte[] index = new byte[10261]; 
    //将读取的内容写到byte数组 
    tmpFileStream.Read(index,0,10261); 
    tmpFileStream.Close(); 
    //定义基本的加密转换运算 
    System.Security.Cryptography.ICryptoTransform Encryptor = rc2CSP.CreateEncryptor(Key,IV); 
    System.IO.MemoryStream msEncrypt = new MemoryStream(); 
    //在此加密转换流中,加密将从csEncrypt,加密后,结果在msEncrypt流中。 
    System.Security.Cryptography.CryptoStream csEncrypt = new CryptoStream(msEncrypt, 
    Encryptor,CryptoStreamMode.Write); 
    //将要加密的文本转换成UTF-16 编码,保存在tmp数组。 
    byte[] tmp = textConverter.GetBytes(toEncryptText); 
    //将tmp输入csEncrypt,将通过Encryptor来加密。 
    csEncrypt.Write(tmp,0,tmp.Length); 
    //输出到msEnctypt 
    csEncrypt.FlushFinalBlock(); 
    //将流转成byte[] 
    encrypted = msEncrypt.ToArray(); 
    if(encrypted.Length>1024) 

    MessageBox.Show("加密后,数据长度大于1KB,无法保存"); 
    return false; 

    //得到加密后数据的大小,将结果存在指定的位置。 
    index[dataIndex*2 - 1] = Convert.ToByte(Convert.ToString(encrypted.Length/128)); 
    index[dataIndex*2] = Convert.ToByte(Convert.ToString(encrypted.Length%128)); 
    //将加密后的结果写入index(覆盖) 
    for(int i=0;i<encrypted.Length;i++) 
    index[1024*(dataIndex-1)+21+i]=encrypted[i]; 
    //建立文件流 
    tmpFileStream = new FileStream(filePath, 
    System.IO.FileMode.Truncate, 
    System.IO.FileAccess.Write, 
    System.IO.FileShare.None,1024,true); 
    //写文件 
    tmpFileStream.Write(index,0,10261); 
    tmpFileStream.Flush(); 
    r = true; 
    tmpFileStream.Close(); 
    return r; 

    /// <summary> 
    /// 从一个文件中解密出一段文本,其中,这个文件是由InitBinFile建立的,并且由 EncryptToFile加密的 
    /// </summary> 
    /// <param name="filePath">要解密的文件</param> 
    /// <param name="dataIndex">要从哪一个块中解密</param> 
    /// <param name="IV">初始化向量</param> 
    /// <param name="Key">解密密匙</param> 
    /// <returns>解密后的文本</returns> 
    public string DecryptFromFile(string filePath,int dataIndex,byte[] IV,byte[] Key) 

    string r = ""; 
    if(dataIndex > 10 && dataIndex < 1) 

    MessageBox.Show("数据索引的取值范围在1至10之间!","错误!", 
    MessageBoxButtons.OK,MessageBoxIcon.Error); 
    return r; 

    byte[] decrypted; 
      

  7.   

    常见用于保证安全的加密或编码算法如下:1、常用密钥算法密钥算法用来对敏感数据、摘要、签名等信息进行加密,常用的密钥算法包括:DES(Data Encryption Standard):数据加密标准,速度较快,适用于加密大量数据的场合; 
    3DES(Triple DES):是基于DES,对一块数据用三个不同的密钥进行三次加密,强度更高; 
    RC2和 RC4:用变长密钥对大量数据进行加密,比 DES 快; 
    IDEA(International Data Encryption Algorithm)国际数据加密算法,使用 128 位密钥提供非常强的安全性; 
    RSA:由 RSA 公司发明,是一个支持变长密钥的公共密钥算法,需要加密的文件快的长度也是可变的; 
    DSA(Digital Signature Algorithm):数字签名算法,是一种标准的 DSS(数字签名标准); 
    AES(Advanced Encryption Standard):高级加密标准,是下一代的加密算法标准,速度快,安全级别高,目前 AES 标准的一个实现是 Rijndael 算法; 
    BLOWFISH,它使用变长的密钥,长度可达448位,运行速度很快; 
    其它算法,如ElGamal、Deffie-Hellman、新型椭圆曲线算法ECC等。2、单向散列算法单向散列函数一般用于产生消息摘要,密钥加密等,常见的有:MD5(Message Digest Algorithm 5):是RSA数据安全公司开发的一种单向散列算法,MD5被广泛使用,可以用来把不同长度的数据块进行暗码运算成一个128位的数值; 
    SHA(Secure Hash Algorithm)这是一种较新的散列算法,可以对任意长度的数据运算生成一个160位的数值; 
    MAC(Message Authentication Code):消息认证代码,是一种使用密钥的单向函数,可以用它们在系统上或用户之间认证文件或消息。HMAC(用于消息认证的密钥散列法)就是这种函数的一个例子。 
    CRC(Cyclic Redundancy Check):循环冗余校验码,CRC校验由于实现简单,检错能力强,被广泛使用在各种数据校验应用中。占用系统资源少,用软硬件均能实现,是进行数据传输差错检测地一种很好的手段(CRC 并不是严格意义上的散列算法,但它的作用与散列算法大致相同,所以归于此类)。3、其它数据算法其它数据算法包括一些常用编码算法及其与明文(ASCII、Unicode 等)转换等,如 Base 64、Quoted Printable、EBCDIC 等。
      

  8.   

    .net已经封装了很多加密算法了
      

  9.   

    (续)
    System.IO.FileStream tmpFileStream = new FileStream(filePath, 
    System.IO.FileMode.Open, 
    System.IO.FileAccess.Read, 
    System.IO.FileShare.None,1024,true); System.Security.Cryptography.ICryptoTransform Decryptor = rc2CSP.CreateDecryptor(Key,IV); 
    System.IO.MemoryStream msDecrypt = new MemoryStream(); 
    System.Security.Cryptography.CryptoStream csDecrypt = new CryptoStream(msDecrypt, 
    Decryptor,CryptoStreamMode.Write); 
    byte[] index = new byte[10261]; tmpFileStream.Read(index,0,10261); 
    int startIndex = 1024*(dataIndex-1)+21; 
    int count = index[dataIndex*2 - 1]*128 + index[dataIndex*2]; 
    byte[] tmp = new byte[count]; Array.Copy(index,1024*(dataIndex-1)+21,tmp,0,count); 
    csDecrypt.Write(tmp,0,count); 
    csDecrypt.FlushFinalBlock(); 
    decrypted = msDecrypt.ToArray(); 
    r = textConverter.GetString(decrypted,0,decrypted.Length); 
    tmpFileStream.Close(); 
    return r; 

    /// <summary> 
    /// 将一段文本加密后保存到一个文件 
    /// </summary> 
    /// <param name="toEncryptText">要加密的文本数据</param> 
    /// <param name="filePath">要保存的文件</param> 
    /// <param name="IV">初始化向量</param> 
    /// <param name="Key">加密密匙</param> 
    /// <returns>是否加密成功</returns> 
    public bool EncryptToFile(string toEncryptText,string filePath,byte[] IV,byte[] Key) 

    bool r = false; 
    byte[] encrypted; 
    System.IO.FileStream tmpFileStream = new FileStream(filePath, 
    System.IO.FileMode.OpenOrCreate, 
    System.IO.FileAccess.Write, 
    System.IO.FileShare.None,1024,true); System.Security.Cryptography.ICryptoTransform Encryptor = rc2CSP.CreateEncryptor(Key,IV); 
    System.IO.MemoryStream msEncrypt = new MemoryStream(); 
    System.Security.Cryptography.CryptoStream csEncrypt = new CryptoStream(msEncrypt, 
    Encryptor,CryptoStreamMode.Write); byte[] tmp = textConverter.GetBytes(toEncryptText); 
    csEncrypt.Write(tmp,0,tmp.Length); 
    csEncrypt.FlushFinalBlock(); 
    encrypted = msEncrypt.ToArray(); 
    tmpFileStream.Write(encrypted,0,encrypted.Length); 
    tmpFileStream.Flush(); 
    r = true; 
    tmpFileStream.Close(); 
    return r; 

    /// <summary> 
    /// 将一个被加密的文件解密 
    /// </summary> 
    /// <param name="filePath">要解密的文件</param> 
    /// <param name="IV">初始化向量</param> 
    /// <param name="Key">解密密匙</param> 
    /// <returns>解密后的文本</returns> 
    public string DecryptFromFile(string filePath,byte[] IV,byte[] Key) 

    string r = ""; 
    byte[] decrypted; 
    System.IO.FileStream tmpFileStream = new FileStream(filePath, 
    System.IO.FileMode.Open, 
    System.IO.FileAccess.Read, 
    System.IO.FileShare.None,1024,true); 
    System.Security.Cryptography.ICryptoTransform Decryptor = rc2CSP.CreateDecryptor(Key,IV); 
    System.IO.MemoryStream msDecrypt = new MemoryStream(); 
    System.Security.Cryptography.CryptoStream csDecrypt = new CryptoStream(msDecrypt, 
    Decryptor,CryptoStreamMode.Write); byte[] tmp = new byte[tmpFileStream.Length]; 
    tmpFileStream.Read(tmp,0,tmp.Length); 
    csDecrypt.Write(tmp,0,tmp.Length); 
    csDecrypt.FlushFinalBlock(); 
    decrypted = msDecrypt.ToArray(); 
    r = textConverter.GetString(decrypted,0,decrypted.Length); 
    tmpFileStream.Close(); 
    return r; 

    /// <summary> 
    /// 设置加密或解密的初始化向量 
    /// </summary> 
    /// <param name="s">长度等于8的ASCII字符集的字符串</param> 
    public void SetIV(string s) 

    if(s.Length != 8) 

    MessageBox.Show("输入的字符串必须为长度为8的且属于ASCII字符集的字符串"); 
    this.iv =null; 
    return; 

    try 

    this.iv = this.asciiEncoding.GetBytes(s); 

    catch(System.Exception) 

    MessageBox.Show("输入的字符串必须为长度为8的且属于ASCII字符集的字符串"); 
    this.iv = null; 


    /// <summary> 
    /// 设置加密或解密的密匙 
    /// </summary> 
    /// <param name="s">长度等于16的ASCII字符集的字符串</param> 
    public void SetKey(string s) 

    if(s.Length != 16) 

    MessageBox.Show("输入的字符串必须为长度为16的且属于ASCII字符集的字符串"); 
    this.key = null; 
    return; 

    try 

    this.key = this.asciiEncoding.GetBytes(s); 
      

  10.   


    catch(System.Exception) 

    MessageBox.Show("输入的字符串必须为长度为16的且属于ASCII字符集的字符串"); 
    this.key = null; 



    }
      

  11.   

    http://community.csdn.net/Expert/topic/5045/5045049.xml?temp=.7106897
    借点人气 找人帮帮忙
      

  12.   

    文件加密主要还是靠算法,.net也集成了一些常用的加密算法。
      

  13.   

    以前在CSDN上也看到过,有兴趣。学习学习
      

  14.   

    既然是加密文件,那一般都采用流密码算法...比如A5和LFSR....
    当然也可以把整个文件当作一个长信息来加密,这样很多对称非对称密码也都可以用...
      

  15.   

    .net集成了一些算法,在security类里边,比如md5等等
      

  16.   

    Michael_Jackson(麦克尔★杰克逊) ( ) 信誉:100    Blog  2006-09-25 17:03:00  得分: 0  
      
       代码没意义,重要的实现思路没有写================================================================
    同意
      
      

  17.   

    借题提一下我原先想建立的思路
    采用一个映像文件,然后可以虚拟出一个逻辑盘,类似虚拟光驱一样的,然后存入这个逻辑盘的文件都会经过加密运算,读取出来的也都会经过解密运算。当要打开这个逻辑盘的时候,必须输入一个密钥,输入的这个密钥并非去核对匹配,而是采用这个密钥进行解密运算,因此,密钥如果输入错误的话,是无法解密的。这个所输入的密钥可以借助外部硬件设备,例如EKey。存入的文件所采用的加密算法可以有多种选择,对称的,非对称的.....
    我感觉加解密不难,关键是虚拟设备的驱动问题。
      

  18.   

    to: littlechen(littlechen)水印技术
    ===========================说下去。
      

  19.   

    to iflang(踩姑娘的小蘑菇)谢谢
      

  20.   

    http://support.microsoft.com/default.aspx?scid=kb%3Bzh-cn%3B307010