Encrypting and Decrypting in .net
2009-05-19 05:16
204 查看
VB.NET:
Imports System.Security.Cryptography
Imports System.IO
Imports System.Text
Module Module1
Sub Main()
Dim inFileName As String = "C:\in.txt"
Dim outFileName As String = "C:\out.txt"
Dim password As String = "Joey"
Dim salt As Byte() = Encoding.Unicode.GetBytes("Salt adds to left spaces")
Dim key As New Rfc2898DeriveBytes(password, salt)
Dim rm As New RijndaelManaged()
rm.Key = key.GetBytes(rm.KeySize / 8)
rm.IV = key.GetBytes(rm.BlockSize / 8)
Dim inFile As New FileStream(inFileName, FileMode.Open, FileAccess.Read)
Dim data(inFile.Length - 1) As Byte
inFile.Read(data, 0, data.Length)
Dim encryptor As ICryptoTransform = rm.CreateEncryptor
Dim outFile As New FileStream(outFileName, FileMode.OpenOrCreate, FileAccess.Write)
Dim stream As New CryptoStream(outFile, encryptor, CryptoStreamMode.Write)
stream.Write(data, 0, data.Length)
stream.Close()
outFile.Close()
inFile.Close()
End Sub
End Module
C#:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.IO;
namespace EncryptingAndDecryptingCS
{
class Program
{
static void Main(string[] args)
{
string inFileName = @"C:\in.txt";
string outFileName = @"C:\out.txt";
string password = "Joey";
byte[] salt = Encoding.Unicode.GetBytes("Salt adds to left spaces");
Rfc2898DeriveBytes key = new Rfc2898DeriveBytes(password, salt);
RijndaelManaged rm = new RijndaelManaged();
rm.Key = key.GetBytes(rm.KeySize / 8);
rm.IV = key.GetBytes(rm.BlockSize / 8);
FileStream inFile = new FileStream(inFileName, FileMode.Open, FileAccess.Read);
byte[] data = new byte[inFile.Length];
inFile.Read(data, 0, data.Length);
ICryptoTransform encryptor = rm.CreateEncryptor();
FileStream outFile = new FileStream(outFileName, FileMode.OpenOrCreate, FileAccess.Write);
CryptoStream stream = new CryptoStream(outFile, encryptor, CryptoStreamMode.Write);
stream.Write(data, 0, data.Length);
stream.Close();
outFile.Close();
inFile.Close();
}
}
}
Imports System.Security.Cryptography
Imports System.IO
Imports System.Text
Module Module1
Sub Main()
Dim inFileName As String = "C:\in.txt"
Dim outFileName As String = "C:\out.txt"
Dim password As String = "Joey"
Dim salt As Byte() = Encoding.Unicode.GetBytes("Salt adds to left spaces")
Dim key As New Rfc2898DeriveBytes(password, salt)
Dim rm As New RijndaelManaged()
rm.Key = key.GetBytes(rm.KeySize / 8)
rm.IV = key.GetBytes(rm.BlockSize / 8)
Dim inFile As New FileStream(inFileName, FileMode.Open, FileAccess.Read)
Dim data(inFile.Length - 1) As Byte
inFile.Read(data, 0, data.Length)
Dim encryptor As ICryptoTransform = rm.CreateEncryptor
Dim outFile As New FileStream(outFileName, FileMode.OpenOrCreate, FileAccess.Write)
Dim stream As New CryptoStream(outFile, encryptor, CryptoStreamMode.Write)
stream.Write(data, 0, data.Length)
stream.Close()
outFile.Close()
inFile.Close()
End Sub
End Module
C#:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.IO;
namespace EncryptingAndDecryptingCS
{
class Program
{
static void Main(string[] args)
{
string inFileName = @"C:\in.txt";
string outFileName = @"C:\out.txt";
string password = "Joey";
byte[] salt = Encoding.Unicode.GetBytes("Salt adds to left spaces");
Rfc2898DeriveBytes key = new Rfc2898DeriveBytes(password, salt);
RijndaelManaged rm = new RijndaelManaged();
rm.Key = key.GetBytes(rm.KeySize / 8);
rm.IV = key.GetBytes(rm.BlockSize / 8);
FileStream inFile = new FileStream(inFileName, FileMode.Open, FileAccess.Read);
byte[] data = new byte[inFile.Length];
inFile.Read(data, 0, data.Length);
ICryptoTransform encryptor = rm.CreateEncryptor();
FileStream outFile = new FileStream(outFileName, FileMode.OpenOrCreate, FileAccess.Write);
CryptoStream stream = new CryptoStream(outFile, encryptor, CryptoStreamMode.Write);
stream.Write(data, 0, data.Length);
stream.Close();
outFile.Close();
inFile.Close();
}
}
}
相关文章推荐
- What is Managed Code and Unmanaged Code in .Net Framework
- [Reference]Creating and analyzing minidumps in .NET production applications
- Python语法解析器PLY——lex and yacc in Python - 娄振林专栏 - 博客频道 - CSDN.NET
- Pro .NET 2.0 Windows Forms and Custom Controls in C#
- How to Hash and Salt Passwords in ASP.NET
- Importing and Extending ActiveX Controls in .NET
- Debugging Memory Related Issues in .Net Application Using WinDBG and SOS
- Creating a Cascading Dropdown in ASP.net MVC 3 and jQuery (1)
- Saving and Displaying Photos in SQL Server using ASP.NET and FileUpload Control
- How to detect and avoid memory and resources leaks in .NET applications
- Runtime class and object in .net
- 【recommend】Tracing in .NET and Implementing Your Own Trace Listeners
- Displaying a Sorted, Paged, and Filtered Grid of Data in ASP.NET MVC
- Tracing in .NET and Implementing Your Own Trace Listeners
- New ASP.NET Features and Fixes in Visual Studio 2015 RC
- How To: Use Impersonation and Delegation in ASP.NET 2.0-from MS
- Pro .NET 2.0 Code and Design Standards in C# by Mark Horner
- Addin and Automation Development In VB.NET 2003 (3)
- Pro .NET 2.0 Windows Forms and Custom Controls in VB 2005
- Add-in and Automation Development in VB.NET 2003 (6-7)