【.net】3des加密
来源:互联网 发布:知乎专栏怎么写文章 编辑:程序博客网 时间:2024/04/30 17:46
内存加密解密
文件加密解密
- using System;
- using System.Security.Cryptography;
- using System.Text;
- using System.IO;
- class TrippleDESCSPSample
- {
- static void Main()
- {
- try
- {
- // Create a new TripleDESCryptoServiceProvider object
- // to generate a key and initialization vector (IV).
- TripleDESCryptoServiceProvider tDESalg = new TripleDESCryptoServiceProvider();
- // Create a string to encrypt.
- string sData = "Here is some data to encrypt.";
- // Encrypt the string to an in-memory buffer.
- byte[] Data = EncryptTextToMemory(sData, tDESalg.Key, tDESalg.IV);
- // Decrypt the buffer back to a string.
- string Final = DecryptTextFromMemory(Data, tDESalg.Key, tDESalg.IV);
- // Display the decrypted string to the console.
- Console.WriteLine(Final);
- }
- catch (Exception e)
- {
- Console.WriteLine(e.Message);
- }
- }
- public static byte[] EncryptTextToMemory(string Data, byte[] Key, byte[] IV)
- {
- try
- {
- // Create a MemoryStream.
- MemoryStream mStream = new MemoryStream();
- // Create a CryptoStream using the MemoryStream
- // and the passed key and initialization vector (IV).
- CryptoStream cStream = new CryptoStream(mStream,
- new TripleDESCryptoServiceProvider().CreateEncryptor(Key, IV),
- CryptoStreamMode.Write);
- // Convert the passed string to a byte array.
- byte[] toEncrypt = new ASCIIEncoding().GetBytes(Data);
- // Write the byte array to the crypto stream and flush it.
- cStream.Write(toEncrypt, 0, toEncrypt.Length);
- cStream.FlushFinalBlock();
- // Get an array of bytes from the
- // MemoryStream that holds the
- // encrypted data.
- byte[] ret = mStream.ToArray();
- // Close the streams.
- cStream.Close();
- mStream.Close();
- // Return the encrypted buffer.
- return ret;
- }
- catch(CryptographicException e)
- {
- Console.WriteLine("A Cryptographic error occurred: {0}", e.Message);
- return null;
- }
- }
- public static string DecryptTextFromMemory(byte[] Data, byte[] Key, byte[] IV)
- {
- try
- {
- // Create a new MemoryStream using the passed
- // array of encrypted data.
- MemoryStream msDecrypt = new MemoryStream(Data);
- // Create a CryptoStream using the MemoryStream
- // and the passed key and initialization vector (IV).
- CryptoStream csDecrypt = new CryptoStream(msDecrypt,
- new TripleDESCryptoServiceProvider().CreateDecryptor(Key, IV),
- CryptoStreamMode.Read);
- // Create buffer to hold the decrypted data.
- byte[] fromEncrypt = new byte[Data.Length];
- // Read the decrypted data out of the crypto stream
- // and place it into the temporary buffer.
- csDecrypt.Read(fromEncrypt, 0, fromEncrypt.Length);
- //Convert the buffer into a string and return it.
- return new ASCIIEncoding().GetString(fromEncrypt);
- }
- catch(CryptographicException e)
- {
- Console.WriteLine("A Cryptographic error occurred: {0}", e.Message);
- return null;
- }
- }
- }
文件加密解密
- using System;
- using System.Security.Cryptography;
- using System.Text;
- using System.IO;
- class TrippleDESCSPSample
- {
- static void Main()
- {
- try
- {
- // Create a new TripleDESCryptoServiceProvider object
- // to generate a key and initialization vector (IV).
- TripleDESCryptoServiceProvider tDESalg = new TripleDESCryptoServiceProvider();
- // Create a string to encrypt.
- string sData = "Here is some data to encrypt.";
- string FileName = "CText.txt";
- // Encrypt text to a file using the file name, key, and IV.
- EncryptTextToFile(sData, FileName, tDESalg.Key, tDESalg.IV);
- // Decrypt the text from a file using the file name, key, and IV.
- string Final = DecryptTextFromFile(FileName, tDESalg.Key, tDESalg.IV);
- // Display the decrypted string to the console.
- Console.WriteLine(Final);
- }
- catch (Exception e)
- {
- Console.WriteLine(e.Message);
- }
- }
- public static void EncryptTextToFile(String Data, String FileName, byte[] Key, byte[] IV)
- {
- try
- {
- // Create or open the specified file.
- FileStream fStream = File.Open(FileName,FileMode.OpenOrCreate);
- // Create a CryptoStream using the FileStream
- // and the passed key and initialization vector (IV).
- CryptoStream cStream = new CryptoStream(fStream,
- new TripleDESCryptoServiceProvider().CreateEncryptor(Key,IV),
- CryptoStreamMode.Write);
- // Create a StreamWriter using the CryptoStream.
- StreamWriter sWriter = new StreamWriter(cStream);
- // Write the data to the stream
- // to encrypt it.
- sWriter.WriteLine(Data);
- // Close the streams and
- // close the file.
- sWriter.Close();
- cStream.Close();
- fStream.Close();
- }
- catch(CryptographicException e)
- {
- Console.WriteLine("A Cryptographic error occurred: {0}", e.Message);
- }
- catch(UnauthorizedAccessException e)
- {
- Console.WriteLine("A file access error occurred: {0}", e.Message);
- }
- }
- public static string DecryptTextFromFile(String FileName, byte[] Key, byte[] IV)
- {
- try
- {
- // Create or open the specified file.
- FileStream fStream = File.Open(FileName, FileMode.OpenOrCreate);
- // Create a CryptoStream using the FileStream
- // and the passed key and initialization vector (IV).
- CryptoStream cStream = new CryptoStream(fStream,
- new TripleDESCryptoServiceProvider().CreateDecryptor(Key,IV),
- CryptoStreamMode.Read);
- // Create a StreamReader using the CryptoStream.
- StreamReader sReader = new StreamReader(cStream);
- // Read the data from the stream
- // to decrypt it.
- string val = sReader.ReadLine();
- // Close the streams and
- // close the file.
- sReader.Close();
- cStream.Close();
- fStream.Close();
- // Return the string.
- return val;
- }
- catch(CryptographicException e)
- {
- Console.WriteLine("A Cryptographic error occurred: {0}", e.Message);
- return null;
- }
- catch(UnauthorizedAccessException e)
- {
- Console.WriteLine("A file access error occurred: {0}", e.Message);
- return null;
- }
- }
- }
- 【.net】3des加密
- [.net]标准 3DES 加密/解密
- .NET DES加密、解密
- DES加密之3DES
- DES/3DES加密,解密
- asp.net DES加密解密
- asp.net DES加密解密
- asp.net DES加密解密
- ASP.NET--DES加密字符串
- asp.net DES加密解密
- .NET中的DES加密解密
- .NET使用DES加密解密
- iOS DES加密与3DES加密
- java与.NET通用的3DES加密解密方法
- 3DES 加密解密
- 3DES加密
- 3des加密解密
- MD5、3DES加密
- 基于配置的ORMapping框架浅析之4(面向方面的AOP切面思想引入)
- SQL Server大型服务器:伸缩性、可用性与易管理性
- 说说Google Map的Mapplets
- String StringBuffer StringBuilder 三者的区别
- CListCtrl 详解
- 【.net】3des加密
- PL/SQL基础学习
- How DirectShow Decides Which Filters to Use
- 使用生成后事件(将一些文件自动放入Bin目录下面)
- Google Chrome 试用感受
- 要开始做事了
- SqlDependency 的数据库配置步骤
- ORACLE用户管理
- PL/SQL高级应用的学习1