private IFFX GetCipher(byte[] tweak, FfsConfigurationResponse ffs, FpeEncryptionKeyResponse encryptionKey, int radix) { IFFX cypher; // set the tweak range and radix based on the FFS record long twkmin = ffs.TweakMinLength.HasValue ? ffs.TweakMinLength.Value : 0; long twkmax = ffs.TweakMaxLength.HasValue ? ffs.TweakMaxLength.Value : 0; _fpe = ffs.EncryptionAlgorithm; switch (_fpe.ToLower()) { case FpeNameConstants.ff1: cypher = new FF1(encryptionKey.UnwrappedDataKey, tweak, twkmin, twkmax, radix); break; case FpeNameConstants.ff3_1: cypher = new FF3_1(encryptionKey.UnwrappedDataKey, tweak, radix); break; default: throw new InvalidOperationException($"Unknown FPE Algorithm: {_fpe}"); } return(cypher); }
//////////////////////////////////////////////////////////////////////////////////// private void PersonAllPackagesToGrid(TreeNode Node) { dataGridView1.Columns.Clear(); dataGridView1.ColumnCount = 3; dataGridView1.ColumnHeadersVisible = true; dataGridView1.Columns[0].Name = "GUID"; dataGridView1.Columns[0].Width = 225; dataGridView1.Columns[0].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; dataGridView1.Columns[1].Name = "IDPack"; dataGridView1.Columns[1].Width = 225; dataGridView1.Columns[1].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; dataGridView1.Columns[2].Name = "BatchType"; dataGridView1.Columns[2].Width = 225; dataGridView1.Columns[2].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; int ind = 0; foreach (TreeNode child in Node.Nodes) { string[] row = { child.Text, "Comming Son", "Comming Son" }; if (child.Nodes.Count > 0) { string[] s = child.Nodes[0].Text.Split('_'); string filename = child.Nodes[0].Parent.Text + "_" + s[2] + "_" + s[1] + "_" + s[0] + ".xml"; XDocument myDoc = XDocument.Load(DefPatch + filename); IEnumerable <XElement> FF1; IEnumerable <XElement> FF2; try { FF1 = myDoc.Descendants("_IDPack"); FF2 = myDoc.Descendants("_BatchType"); row[1] = ""; row[2] = ""; if (FF1.Count() > 0) { row[1] = FF1.First().Value; } if (FF2.Count() > 0) { row[2] = FF2.First().Value; } ind = dataGridView1.Rows.Add(row); } catch { } } dataGridView1.Rows[ind].Cells[0].Style.BackColor = Color.Gainsboro; dataGridView1.Rows[ind].Cells[1].Style.BackColor = Color.Gainsboro; dataGridView1.Rows[ind].Cells[2].Style.BackColor = Color.Gainsboro; } }
public void testStress() { int[] keySizes = { 128, 192, 256 }; FF1 ff1 = new FF1(10, 8); // for each key size foreach (int k in keySizes) { // generate a new key in the key size byte[] K = new byte[k / 8]; for (int i = 0; i < k / 8; i++) { K[i] = (byte)i; } // init plaintext to a 1 byte array int[] PT = { k % 10 }; // for each plaintext length for (int j = 0; j < 4; j++) { // make plaintext eight times longer PT = Common.concatenate(PT, PT); PT = Common.concatenate(PT, PT); PT = Common.concatenate(PT, PT); // repeat the test four times for (int i = 0; i < 4; i++) { // create a new tweak array byte[] T = Common.bytestring(i, 8); // encrypt the plaintext int[] CT = ff1.encrypt(K, T, PT); // verify decrypted ciphertext against original plaintext CollectionAssert.AreEquivalent(PT, ff1.decrypt(K, T, CT)); // use the ciphertext as the new plaintext PT = CT; } } } }
///<summary> ///Perform a single test of FF1 encryption and decryption. ///</summary> ///<param name="plaintext">The plaintext input.</param> ///<param name="ciphertext">The expected ciphertext output.</param> ///<param name="key">The AES key.</param> ///<param name="name">The name of the test.</param> ///<param name="radix">The radix used in plaintext and ciphertext arguments.</param> ///<param name="tweak">The tweak.</param> ///<param name="maxTlen">The maximum length of a tweak.</param> private void testFF1Iteration(String name, int radix, int maxTlen, byte[] key, byte[] tweak, int[] plaintext, int[] ciphertext) { // create an FF1 instance FF1 ff1 = new FF1(radix, maxTlen); Assert.IsNotNull(ff1); // create an AES key from the key data byte[] K = key; Console.WriteLine("\n==============================================================\n"); Console.WriteLine(name + "\n"); Console.WriteLine("FF1-AES" + key.Length * 8 + "\n"); Console.WriteLine("Key is " + Common.byteArrayToHexString(key)); Console.WriteLine("Radix = " + radix); Console.WriteLine("--------------------------------------------------------------\n"); Console.WriteLine("PT is <" + Common.intArrayToString(plaintext) + ">\n"); // perform the encryption int[] CT = ff1.encrypt(K, tweak, plaintext); Console.WriteLine("CT is <" + Common.intArrayToString(CT) + ">"); // validate the ciphertext CollectionAssert.AreEquivalent(ciphertext, CT); Console.WriteLine("\n--------------------------------------------------------------\n"); // perform the decryption int[] PT = ff1.decrypt(K, tweak, CT); Console.WriteLine("PT is <" + Common.intArrayToString(PT) + ">"); // validate the recovered plaintext CollectionAssert.AreEquivalent(plaintext, PT); }
public void testEncrypt() { int radix = 8; int maxTlen = 16; FF1 ff1 = new FF1(radix, maxTlen); Assert.IsNotNull(ff1); // set up generic test inputs byte[] key = { (byte)0x2B, (byte)0x7E, (byte)0x15, (byte)0x16, (byte)0x28, (byte)0xAE, (byte)0xD2, (byte)0xA6, (byte)0xAB, (byte)0xF7, (byte)0x15, (byte)0x88, (byte)0x09, (byte)0xCF,(byte)0x4F, (byte)0x3C }; int[] plainText = { 0, 1, 2, 3, 4, 5, 6, 7 }; byte[] K = key; byte[] T = { }; int[] PT = plainText; // null inputs try { K = null; PT = plainText; ff1.encrypt(K, T, PT); } catch (Exception e) { Assert.IsInstanceOf <NullReferenceException>(e); } try { K = key; T = null; PT = plainText; ff1.encrypt(K, T, PT); } catch (Exception e) { Assert.IsInstanceOf <NullReferenceException>(e); } try { K = key; T = new byte[0]; PT = null; ff1.encrypt(K, T, PT); } catch (Exception e) { Assert.IsInstanceOf <NullReferenceException>(e); } // wrong key type try { K = new byte[] { 0, 1, 2, 3, 4, 5 }; T = new byte[0]; PT = plainText; ff1.encrypt(K, T, PT); } catch (Exception) { } // T is too long try { K = key; T = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; PT = plainText; ff1.encrypt(K, T, PT); } catch (Exception) { } // X is too short try { K = key; T = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; PT = new int[] { 1 }; ff1.encrypt(K, T, PT); } catch (Exception) { } // X is too long try { K = key; T = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; PT = new int[Constants.MAXLEN + 1]; ff1.encrypt(K, T, PT); } catch (Exception) { } // X is too short for radix try { K = key; T = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; PT = new int[] { 1, 2 }; ff1.encrypt(K, T, PT); } catch (Exception) { } // d > 16 radix = 128; maxTlen = 16; ff1 = new FF1(radix, maxTlen); Assert.IsNotNull(ff1); K = key; T = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; PT = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32 }; int[] CT = ff1.encrypt(K, T, PT); CollectionAssert.AreEquivalent(PT, ff1.decrypt(K, T, CT)); }
public void testFF1() { FF1 ff1 = new FF1(10, 0); Assert.IsNotNull(ff1); }
//////////////////////////////////////////////////////////////////////////////////// private void AllDocumFromPackageToGrid(TreeNode Node) { dataGridView1.Columns.Clear(); dataGridView1.ColumnCount = 5; dataGridView1.ColumnHeadersVisible = true; dataGridView1.Columns[0].Name = "N"; dataGridView1.Columns[0].Width = 25; dataGridView1.Columns[1].Name = "Header"; dataGridView1.Columns[1].Width = 150; dataGridView1.Columns[2].Name = "Number"; dataGridView1.Columns[2].Width = 125; dataGridView1.Columns[3].Name = "Date"; dataGridView1.Columns[3].Width = 75; dataGridView1.Columns[4].Name = "Barcode"; dataGridView1.Columns[4].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; dataGridView1.Columns[4].FillWeight = 125; int ind = 0; string filename = ""; string[] row = new string[5]; foreach (TreeNode child in Node.Nodes) { string[] s = child.Text.Split('_'); filename = child.Parent.Text + "_" + s[2] + "_" + s[1] + "_" + s[0] + ".xml"; XDocument myDoc = XDocument.Load(DefPatch + filename); IEnumerable <XElement> FF1; IEnumerable <XElement> FF2; IEnumerable <XElement> FF3; IEnumerable <XElement> FF4; try { FF1 = myDoc.Descendants("_Header"); FF2 = myDoc.Descendants("_DocNum"); FF3 = myDoc.Descendants("_DateDoc"); FF4 = myDoc.Descendants("_Barcode"); row[0] = s[0]; row[1] = ""; row[2] = ""; row[3] = ""; row[4] = ""; if (FF1.Count() > 0) { row[1] = FF1.First().Value; } if (FF2.Count() > 0) { row[2] = FF2.First().Value; } if (FF3.Count() > 0) { row[3] = FF3.First().Value; } if (FF4.Count() > 0) { row[4] = FF4.First().Value; } ind = dataGridView1.Rows.Add(row); dataGridView1.Rows[ind].Cells[0].Style.BackColor = Color.Gainsboro; dataGridView1.Rows[ind].Cells[1].Style.BackColor = Color.Gainsboro; dataGridView1.Rows[ind].Cells[2].Style.BackColor = Color.Gainsboro; dataGridView1.Rows[ind].Cells[3].Style.BackColor = Color.Gainsboro; dataGridView1.Rows[ind].Cells[4].Style.BackColor = Color.Gainsboro; } catch { } } }