/// <summary> /// Demonstrates saving a key file using the PackageFactory class /// </summary> private void SaveKey() { try { // add the time/date expiration stamp if key policy is volatile if (HasPolicy(KeyPolicies.Volatile)) { if (dtVolatileTime.Value.Ticks > DateTime.Now.Ticks) _container.Authority.OptionFlag = dtVolatileTime.Value.Ticks; else throw new Exception("Invalid Expiry time. If a key is marked as Volatile, the expired time must be greater than the current time."); } // get the key tag description if (!string.IsNullOrEmpty(txtKeyDescription.Text)) { byte[] data = new byte[32]; byte[] tag = Encoding.ASCII.GetBytes(txtKeyDescription.Text); Array.Copy(tag, data, tag.Length < 32 ? tag.Length : 32); _container.Authority.PackageTag = data; } // get the number of subkeys to create in this package int keyCount = 1; if (!string.IsNullOrEmpty(txtSubKeyCount.Text) && txtSubKeyCount.Text != "0") int.TryParse(txtSubKeyCount.Text, out keyCount); // create a PackageKey; a key package can contain 1 or many thousands of 'subkeys'. Each subkey set // contains one group of unique random keying material; key, iv, and optional hmac key. // Each key set is used only once for encryption, guaranteeing that a unique set of values is used for every encryption cycle. PackageKey package = new PackageKey( _container.Authority, // the KeyAuthority structure _container.Description, // the CipherDescription structure keyCount, // the number of subkeys to add to this key package IdGenerator()); // the file extension encryption key // create and write the key using (PackageFactory factory = new PackageFactory(_keyFilePath, _container.Authority)) factory.Create(package); // store path _lastKeyPath = Path.GetDirectoryName(_keyFilePath); Reset(); lblStatus.Text = "The Key has been saved!"; } catch (Exception ex) { if (File.Exists(_keyFilePath)) File.Delete(_keyFilePath); string message = ex.Message == null ? "" : ex.Message; MessageBox.Show("An error occured, the key could not be created! " + message); } }
/// <summary> /// Creates a temporary PackageKey on disk, extracts and compares the copy /// <para>Throws an Exception on failure</</para> /// </summary> public static void PackageFactoryTest() { string path = GetTempPath(); KeyGenerator kgen = new KeyGenerator(); // populate a KeyAuthority structure KeyAuthority authority = new KeyAuthority(kgen.GetBytes(16), kgen.GetBytes(16), kgen.GetBytes(16), kgen.GetBytes(32), 0); // cipher paramaters CipherDescription desc = new CipherDescription( SymmetricEngines.RDX, 32, IVSizes.V128, CipherModes.CTR, PaddingModes.X923, BlockSizes.B128, RoundCounts.R14, Digests.Keccak512, 64, Digests.Keccak512); // create the package key PackageKey pkey = new PackageKey(authority, desc, 10); // write a key file using (PackageFactory pf = new PackageFactory(path, authority)) pf.Create(pkey); for (int i = 0; i < pkey.SubKeyCount; i++) { CipherDescription desc2; KeyParams kp1; KeyParams kp2; byte[] ext; byte[] id = pkey.SubKeyID[i]; // get at index using (FileStream stream = new FileStream(path, FileMode.Open)) kp2 = PackageKey.AtIndex(stream, i); // read the package from id using (PackageFactory pf = new PackageFactory(path, authority)) pf.Extract(id, out desc2, out kp1, out ext); // compare key material if (!Compare.AreEqual(kp1.Key, kp2.Key)) throw new Exception(); if (!Compare.AreEqual(kp1.IV, kp2.IV)) throw new Exception(); if (!Compare.AreEqual(pkey.ExtensionKey, ext)) throw new Exception(); if (!desc.Equals(desc2)) throw new Exception(); } if (File.Exists(path)) File.Delete(path); }