/* 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: empencrypted.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: empencrypted.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(); } }
Author: coder
Illustrates asymmetric cryptography
/*
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
File Move Implementation
using System; using System.IO; class Class1 { static void Main(string[] args) { string [] cla = Environment.GetCommandLineArgs(); if (cla.GetUpperBound(0) == 2) { FileInfo fi = new FileInfo(cla[1]); fi.MoveTo(cla[2]); Console.WriteLine("File Created : " + fi.CreationTime.ToString()); Console.WriteLine("Moved to : " + cla[2]); } else Console.WriteLine ("Usage: mv <source file> <destination file>"); } }
Use the FileInfo Class to Delete Files with Ease
using System; using System.IO; class Class1 { static void Main(string[] args) { string [] cla = Environment.GetCommandLineArgs(); if (cla.GetUpperBound(0) == 1) { FileInfo fi = new FileInfo(cla[1]); fi.Delete(); Console.WriteLine("File : " + cla[1]); Console.WriteLine("Attributes: " + fi.Attributes.ToString()); Console.WriteLine("File Deleted..."); } else Console.WriteLine ("Usage: rm <filename>"); } }
C# Implementation of the File Copy Command
using System; using System.IO; class Class1 { static void Main(string[] args) { string [] cla = Environment.GetCommandLineArgs(); if (cla.GetUpperBound(0) == 2) { FileInfo fi = new FileInfo(cla[1]); fi.CopyTo(cla[2],true); Console.WriteLine("Copied " + fi.Length + " bytes."); } else{ Console.WriteLine ("Usage: cp <input file> <output file>"); } } }
Get file attribute: ReadOnly
using System; using System.IO; static class Test { static void Main() { FileInfo file = new FileInfo("test.cs"); Console.WriteLine(file.Attributes.ToString()); if (file.Attributes == FileAttributes.ReadOnly) { Console.WriteLine("File is read-only (faulty test)."); } if ((file.Attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly) { Console.WriteLine("File is read-only (correct test)."); } } }
Check file Attribute
using System; using System.IO; class MainClass { static void Main() { FileInfo file = new FileInfo("data.txt"); Console.WriteLine(file.Attributes.ToString()); if (file.Attributes == FileAttributes.ReadOnly) { Console.WriteLine("File is read-only (faulty test)."); } if ((file.Attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly) { Console.WriteLine("File is read-only (correct test)."); } } }