public static void DoDecrypt() { // Open the file. if (FileName != null && FileName.Length >= 0) { fStream = new FileStream(FileName, FileMode.OpenOrCreate); } else { FileName = "Data.dat"; fStream = new FileStream("Data.dat", FileMode.Open); } FileInfo f = new FileInfo(FileName); int bytesWritten = Convert.ToInt32(f.Length); // Create some random entropy. if (entropy == null) { entropy = EntropyGenerator.GetIP(); } ; // Read from the stream and decrypt the data. byte[] decryptData = DecryptDataFromStream(entropy, DataProtectionScope.LocalMachine, fStream, bytesWritten); if (decryptData != null) { decryptedData = UnicodeEncoding.ASCII.GetString(decryptData); } else { decryptedData = null; } fStream.Close(); }
public static void DoEncrypt() { /////////////////////////////// // // Data Encryption - ProtectedData // /////////////////////////////// // Create the original data to be encrypted // byte[] toEncrypt = UnicodeEncoding.ASCII.GetBytes("This is some data of any length."); byte[] toEncrypt = UnicodeEncoding.ASCII.GetBytes(encryptedData); // Create a file. if (FileName != null && FileName.Length == 0) { FileName = Environment.GetFolderPath(Environment.SpecialFolder.System) + @"\DataLicense.dat"; } fStream = new FileStream(FileName, FileMode.OpenOrCreate); // Create some random entropy. if (entropy == null) { entropy = EntropyGenerator.GetIP(); } // Encrypt a copy of the data to the stream. int bytesWritten = EncryptDataToStream(toEncrypt, entropy, DataProtectionScope.LocalMachine, fStream); fStream.Close(); }