private static void RunClient() { Keyring.GlobalKeyring.Add("Sensitive Information", AesEncryptionKey.Create(TripleDesEncryptionKey.Create())); Keyring.GlobalKeyring.Add("Semi-Sensitive Information", AesEncryptionKey.Create()); Keyring.GlobalKeyring.Add("Non-Sensitive Information", TripleDesEncryptionKey.Create()); // Start Client var channelFactory = new ChannelFactory <IService>(new BasicHttpBinding()); var endpoint = new EndpointAddress(Url); var serviceObject = channelFactory.CreateChannel(endpoint); var patients = GetPatients(); for (int i = 0; i < patients.Count; i++) { Console.WriteLine("[{0}] Sending {1} {2} to server.", i, patients[i].FirstName, patients[i].LastName); serviceObject.SetValue(i, patients[i]); } var rng = new Random((int)DateTime.Now.Ticks); var patientIdx = rng.Next(0, patients.Count - 1); Console.WriteLine(); Console.WriteLine("Requesting name from index {0}: {1}", patientIdx, serviceObject.ServerGetName(patientIdx)); Console.WriteLine(); var patient = serviceObject.GetValue(patientIdx); Console.WriteLine("Got back {0} {1} from server.", patient.FirstName, patient.LastName); Console.WriteLine("Press [ENTER] to quit client."); Console.ReadLine(); }
static void Main(string[] args) { // Create 3 new keys with randomly generated key material Keyring.GlobalKeyring.Add("Sensitive Information", AesEncryptionKey.Create(TripleDesEncryptionKey.Create())); Keyring.GlobalKeyring.Add("Semi-Sensitive Information", TripleDesEncryptionKey.Create()); Keyring.GlobalKeyring.Add("Non-Sensitive Information", TripleDesEncryptionKey.Create()); // Create new instances of types to encrypt and fill them with something var encryptedObjBinaryFormatter = new SampleObjectBinaryFormatter().AsEncrypted(); var encryptedObjDataContract = new SampleObjectDataContract().AsEncrypted(); var encryptedObjJson = new SampleObjectJson().AsEncrypted(); encryptedObjBinaryFormatter.Integer = encryptedObjDataContract.Integer = encryptedObjJson.Integer = 12; encryptedObjBinaryFormatter.String = encryptedObjDataContract.String = encryptedObjJson.String = "abc"; Console.WriteLine("Before serialize: " + encryptedObjBinaryFormatter.Integer + ", '" + encryptedObjBinaryFormatter.String + "'"); // Serialize using each of the 3 mainstream serializers var bfProxy = TestBinaryFormatter(encryptedObjBinaryFormatter); var dcProxy = TestDataContractSerializer(encryptedObjDataContract); var jsonProxy = TestJsonSerializer(encryptedObjJson); Console.WriteLine("After deserialize (BinaryFormatter): " + bfProxy.Integer + ", '" + bfProxy.String + "'"); Console.WriteLine("After deserialize (DataContract): " + dcProxy.Integer + ", '" + dcProxy.String + "'"); Console.WriteLine("After deserialize (JSON): " + jsonProxy.Integer + ", '" + jsonProxy.String + "'"); Console.ReadLine(); }
public static void ClassInitialize(TestContext context) { GeneratedKeyring.Add("AES", AesEncryptionKey.Create()); GeneratedKeyring.Add("DES", TripleDesEncryptionKey.Create()); GeneratedKeyring.Add("AES-DES", AesEncryptionKey.Create()); // Warmup var types = DataWrapperExtensions.GetAllEncryptableTypes(true); }
public async Task Is_able_to_create_folder_if_needed() { using var key = new AesEncryptionKey(); var sut = new DevelopKeyValueStore(Path.GetTempPath() + Guid.NewGuid().ToString()); var encrypted = await sut.EncryptAsync(key).ConfigureAwait(false); var decrypted = await sut.DecriptAsync(encrypted).ConfigureAwait(false); Assert.Equal(key, decrypted); }
public async Task Is_able_to_encrypt_and_decrypt_a_key() { using var key = new AesEncryptionKey(); var sut = CreateSut(); var encrypted = await sut.EncryptAsync(key).ConfigureAwait(false); var decrypted = await sut.DecriptAsync(encrypted).ConfigureAwait(false); Assert.Equal(key, decrypted); }
public void TestInitialize() { var generatedKeyring = new Keyring(); generatedKeyring.Add("AES", AesEncryptionKey.Create()); generatedKeyring.Add("DES", TripleDesEncryptionKey.Create()); generatedKeyring.Add("AES-DES", AesEncryptionKey.Create(TripleDesEncryptionKey.Create())); BaseTestObject = new TestableDataContract(); BaseTestObject.Populate(); GeneratedTestObject = new TestableDataContract().AsEncrypted(generatedKeyring); GeneratedTestObject.Populate(); }
public async Task Unable_to_decrypt_if_wrong_password() { using var key = new AesEncryptionKey(); string keyMaterialFolder = Path.GetTempPath() + Guid.NewGuid().ToString(); var sut = new FolderBasedKeyValueStore( keyMaterialFolder, "password"); await sut.EncryptAsync(key).ConfigureAwait(false); //We should not be able to create a sut where already exists a key //with an invalid password Assert.Throws <AggregateException>(() => new FolderBasedKeyValueStore( keyMaterialFolder, "another-password")); }
public async Task Can_resuse_key_upon_dispose() { // create a key encrypt byte[] encrypted; using var key = new AesEncryptionKey(); var sut = new DevelopKeyValueStore(Path.GetTempPath()); encrypted = await sut.EncryptAsync(key).ConfigureAwait(false); // then decrypt with another instance of the keyvalue store. var anotherSut = new DevelopKeyValueStore(Path.GetTempPath()); var decrypted = await anotherSut.DecriptAsync(encrypted).ConfigureAwait(false); Assert.Equal(key, decrypted); }
static void Main(string[] args) { Keyring.GlobalKeyring.Add("Sensitive Information", AesEncryptionKey.Create(TripleDesEncryptionKey.Create())); Keyring.GlobalKeyring.Add("Semi-Sensitive Information", AesEncryptionKey.Create()); Keyring.GlobalKeyring.Add("Non-Sensitive Information", TripleDesEncryptionKey.Create()); var examplePatients = GetExamplePatients(); using (var session = GetSession()) { List <Patient> patients = new List <Patient>(); ITransaction transaction; transaction = session.BeginTransaction(); foreach (var patient in session.Query <Patient>()) { session.Delete(patient); } transaction.Commit(); Keyring.GlobalKeyring.Lock(); transaction = session.BeginTransaction(); foreach (var patient in examplePatients) { session.Save(patient); } transaction.Commit(); patients = session.Query <Patient>().ToList(); Console.WriteLine("WHILE KEYRING IS LOCKED:"); foreach (var patient in patients) { Console.WriteLine("{0} {1} - SSN#{2}", patient.FirstName, patient.LastName, patient.SSN); } Console.WriteLine("WHILE KEYRING IS UNLOCKED:"); Keyring.GlobalKeyring.Unlock(); foreach (var patient in patients) { Console.WriteLine("{0} {1} - SSN#{2}", patient.FirstName, patient.LastName, patient.SSN); } Console.ReadKey(); } }
public async Task CanEncryptAndDecryptStreamWithGenericKey() { const string content = "this test will be encrypted"; byte[] stringContent = Encoding.UTF8.GetBytes(content); using var sourceStream = new MemoryStream(stringContent); using var encryptedStream = new MemoryStream(); using var key = new AesEncryptionKey(); await StaticEncryptor.EncryptAsync(sourceStream, encryptedStream, key).ConfigureAwait(false); //Now decrypt var decryptedMemoryStream = new MemoryStream(); var readingEncryptedStream = new MemoryStream(encryptedStream.ToArray()); await StaticEncryptor.DecryptAsync(readingEncryptedStream, decryptedMemoryStream, key).ConfigureAwait(false); var decryptedString = Encoding.UTF8.GetString(decryptedMemoryStream.ToArray()); Assert.Equal(decryptedString, content); }
private static void DataGeneration() { Keyring.GlobalKeyring.Add("Sensitive Information", AesEncryptionKey.Create(TripleDesEncryptionKey.Create())); Keyring.GlobalKeyring.Add("Semi-Sensitive Information", AesEncryptionKey.Create()); Keyring.GlobalKeyring.Add("Non-Sensitive Information", TripleDesEncryptionKey.Create()); using (var db = new DatabaseContext()) { db.Patients.RemoveRange(db.Patients); foreach (var dataRow in SampleDataCsv.Split('\n')) { var record = dataRow.Split(','); var newPatient = new Patient().AsEncrypted(); newPatient.FirstName = record[0]; newPatient.LastName = record[1]; newPatient.ALT = double.Parse(record[2]); newPatient.AST = double.Parse(record[3]); newPatient.BMI = double.Parse(record[4]); newPatient.CPeptide = double.Parse(record[5]); newPatient.Glucose = double.Parse(record[6]); newPatient.HDL = double.Parse(record[7]); newPatient.SSN = record[8]; newPatient.DOB = DateTime.Parse(record[9]); newPatient.Collected = DateTime.Parse(record[10]); db.Patients.Attach(newPatient); db.Patients.Add(newPatient); } db.SaveChanges(); using (var keyringStream = new FileStream("keyring.dat", FileMode.OpenOrCreate)) { Keyring.GlobalKeyring.ExportToStream(keyringStream); } } }
/* Scenario: * Health Information System application with 3 roles and 2 users. * * Roles (Business Cases): * - "Doctor Only" -> Only Dr. Linda McDonald can see this information * - "Restricted" -> Only the doctor and nurse(s) can see this information * - "Office" -> Office staff can see this information * * It is assumed that patient names are not sensitive information. */ protected override void OnStartup(StartupEventArgs e) { base.OnStartup(e); if (e.Args.Any() && e.Args[0] == "/generate") { var sensitiveKey = new AntiPrintScreenKey(EncryptionKey.KeyAppliesTo.Both, AesEncryptionKey.Create(TripleDesEncryptionKey.Create())); var somewhatSensitiveKey = TripleDesEncryptionKey.Create(); var nonSensitiveKey = TripleDesEncryptionKey.Create(); var keyring = new Keyring(); keyring.Add("Doctor Only", sensitiveKey); keyring.Add("Restricted", somewhatSensitiveKey); keyring.Add("Office", nonSensitiveKey); // John's Keyring using (var johnFs = new FileStream("jthomas.keyring", FileMode.Create)) { keyring.ExportToStream(johnFs, "Restricted", "Office"); } // Linda's Keyring using (var lindaFs = new FileStream("lmcdonald.keyring", FileMode.Create)) { keyring.ExportToStream(lindaFs); } } else { new RecordList().ShowDialog(); } this.Shutdown(); }
public void Functional_TestTimeWindowKey() { Keyring.GlobalKeyring.Add("TimeWindowKey", new TimeWindowKey(DateTime.Now, DateTime.Now.AddSeconds(2), chainedInnerKey: AesEncryptionKey.Create())); var timeWindowTestObject = new TimeWindowKeyTest().AsEncrypted(); timeWindowTestObject.SampleString = "This is a sample string!"; Assert.AreEqual("This is a sample string!", timeWindowTestObject.SampleString); Task.Delay(3000).Wait(); Assert.IsNull(timeWindowTestObject.SampleString); }
public void Initialize() { Keyring.GlobalKeyring.Add("AES-DES", AesEncryptionKey.Create(TripleDesEncryptionKey.Create())); Keyring.GlobalKeyring.Add("DES", TripleDesEncryptionKey.Create()); Keyring.GlobalKeyring.Add("AES", AesEncryptionKey.Create()); }