Csharp/C Sharp/File Stream/File Cryptography
Содержание
Illustrates asymmetric cryptography
<source lang="csharp"> /* Mastering Visual C# .NET by Jason Price, Mike Gunderloy Publisher: Sybex; ISBN: 0782129110
- /
/*
Example19_11.cs illustrates asymmetric cryptography
- /
using System; using System.IO; using System.Security.Cryptography; public class Example19_11 {
public static void Main() { // Create a new crypto provider RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(); // Data to encrypt Byte[] testData = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; // Encrypt the data Byte[] encryptedData = rsa.Encrypt(testData, false); Console.WriteLine("Encrypted data:"); for(int i=0; i<encryptedData.GetLength(0); i++) { Console.Write("{0} ", encryptedData[i]); } Console.WriteLine(); // Decrypt the data Byte[] decryptedData = rsa.Decrypt(encryptedData, false); Console.WriteLine("Decrypted Data:"); for(int i=0; i<decryptedData.GetLength(0); i++) { Console.Write("{0} ", decryptedData[i]); } Console.WriteLine(); }
}
</source>
Illustrates declarative role-based security
<source lang="csharp"> /* Mastering Visual C# .NET by Jason Price, Mike Gunderloy Publisher: Sybex; ISBN: 0782129110
- /
/*
Example19_8.cs illustrates declarative role-based security
- /
using System; using System.IO; using System.Security.Permissions; [PrincipalPermissionAttribute(SecurityAction.Demand, Role="Administrators")] public class Example19_8 {
public static void Main() { // Create a new file to work with FileStream fsOut = File.Create(@"c:\\temp\\test.txt"); // Create a StreamWriter to handle writing StreamWriter sw = new StreamWriter(fsOut); // And write some data sw.WriteLine(""Twas brillig, and the slithy toves"); sw.WriteLine("Did gyre and gimble in the wabe."); sw.Flush(); sw.Close(); }
}
</source>
Illustrates decrypting a file
<source lang="csharp"> /* Mastering Visual C# .NET by Jason Price, Mike Gunderloy Publisher: Sybex; ISBN: 0782129110
- /
/*
Example19_10.cs illustrates decrypting a file
- /
using System; using System.IO; using System.Security.Cryptography; public class Example19_10 {
public static void Main() { // Create a new crypto provider TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider(); // open the file containing the key and IV FileStream fsKeyIn = File.OpenRead(@"c:\temp\encrypted.key"); // use a BinaryReader to read formatted data from the file BinaryReader br = new BinaryReader(fsKeyIn); // read data from the file and close it tdes.Key = br.ReadBytes(24); tdes.IV = br.ReadBytes(8); // Open the encrypted file FileStream fsIn = File.OpenRead(@"c:\\temp\\encrypted.txt"); // Create a cryptostream to decrypt from the filestream CryptoStream cs = new CryptoStream(fsIn, tdes.CreateDecryptor(), CryptoStreamMode.Read); // Create a StreamReader to format the input StreamReader sr = new StreamReader(cs); // And decrypt the data Console.WriteLine(sr.ReadToEnd()); sr.Close(); }
}
</source>
Illustrates demanding permissions
<source lang="csharp"> /* Mastering Visual C# .NET by Jason Price, Mike Gunderloy Publisher: Sybex; ISBN: 0782129110
- /
/*
Example19_5.cs illustrates demanding permissions
- /
using System; using System.IO; using System.Security.Permissions; [FileIOPermissionAttribute(SecurityAction.Demand, All=@"c:\\temp")] public class Example19_5 {
public static void MakeFile() { // Create a new file to work with FileStream fsOut = File.Create(@"c:\\temp\\test.txt"); // Create a StreamWriter to handle writing StreamWriter sw = new StreamWriter(fsOut); // And write some data sw.WriteLine(""Twas brillig, and the slithy toves"); sw.WriteLine("Did gyre and gimble in the wabe."); sw.Flush(); sw.Close(); }
}
</source>
Illustrates encrypting a file
<source lang="csharp"> /* Mastering Visual C# .NET by Jason Price, Mike Gunderloy Publisher: Sybex; ISBN: 0782129110
- /
/*
Example19_9.cs illustrates encrypting a file
- /
using System; using System.IO; using System.Security.Cryptography; public class Example19_9 {
public static void Main() { // Create a new file to work with FileStream fsOut = File.Create(@"c:\temp\encrypted.txt"); // Create a new crypto provider TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider(); // Create a cryptostream to encrypt to the filestream CryptoStream cs = new CryptoStream(fsOut, tdes.CreateEncryptor(), CryptoStreamMode.Write); // Create a StreamWriter to format the output StreamWriter sw = new StreamWriter(cs); // And write some data sw.WriteLine(""Twas brillig, and the slithy toves"); sw.WriteLine("Did gyre and gimble in the wabe."); sw.Flush(); sw.Close(); // save the key and IV for future use FileStream fsKeyOut = File.Create(@"c:\\temp\encrypted.key"); // use a BinaryWriter to write formatted data to the file BinaryWriter bw = new BinaryWriter(fsKeyOut); // write data to the file bw.Write( tdes.Key ); bw.Write( tdes.IV ); // flush and close bw.Flush(); bw.Close(); }
}
</source>