public string Encrypt(string clearText) { StringBuilder output = new StringBuilder(); KeyIndexer key = new KeyIndexer(); for (int i = 0; i < Key.Length; i++) { for (int j = 0; j < alphabet.Length; j++) { if (Key[i] == alphabet[j]) { key.Add(i, j); } } } key.KeyedBy = KeyedBy.Value; for (int i = 0; i < Key.Length; i++) { for (int j = key[i]; j < clearText.Length; j += Key.Length) { output.Append(clearText[j]); } } return(output.ToString()); }
/// <summary> /// Set the data before serialization /// </summary> /// <param name="data">The list of </param> public void SetData(IList <ContentCatalogDataEntry> data) { if (data == null) { return; } var providers = new KeyIndexer <string>(data.Select(s => s.Provider), 10); var internalIds = new KeyIndexer <string>(data.Select(s => s.InternalId), data.Count); var keys = new KeyIndexer <object>(data.SelectMany(s => s.Keys), data.Count * 3); var types = new KeyIndexer <Type>(data.Select(s => s.ResourceType), 50); keys.Add(data.SelectMany(s => s.Dependencies)); var keyIndexToEntries = new KeyIndexer <List <ContentCatalogDataEntry>, object>(keys.values, s => new List <ContentCatalogDataEntry>(), keys.values.Count); var entryToIndex = new Dictionary <ContentCatalogDataEntry, int>(data.Count); var extraDataList = new List <byte>(); var entryIndexToExtraDataIndex = new Dictionary <int, int>(); int extraDataIndex = 0; //create buckets of key to data entry for (int i = 0; i < data.Count; i++) { var e = data[i]; int extraDataOffset = -1; if (e.Data != null) { var len = SerializationUtilities.WriteObjectToByteList(e.Data, extraDataList); if (len > 0) { extraDataOffset = extraDataIndex; extraDataIndex += len; } } entryIndexToExtraDataIndex.Add(i, extraDataOffset); entryToIndex.Add(e, i); foreach (var k in e.Keys) { keyIndexToEntries[k].Add(e); } } m_ExtraDataString = Convert.ToBase64String(extraDataList.ToArray()); //create extra entries for dependency sets Dictionary <int, object> hashSources = new Dictionary <int, object>(); int originalEntryCount = data.Count; for (int i = 0; i < originalEntryCount; i++) { var entry = data[i]; if (entry.Dependencies == null || entry.Dependencies.Count < 2) { continue; } int hashCode = CalculateCollectedHash(entry.Dependencies, hashSources); bool isNew = false; keys.Add(hashCode, ref isNew); if (isNew) { //if this combination of dependecies is new, add a new entry and add its key to all contained entries var deps = entry.Dependencies.Select(d => keyIndexToEntries[d][0]).ToList(); keyIndexToEntries.Add(hashCode, deps); foreach (var dep in deps) { dep.Keys.Add(hashCode); } } //reset the dependency list to only contain the key of the new set entry.Dependencies.Clear(); entry.Dependencies.Add(hashCode); } //serialize internal ids and providers m_InternalIds = internalIds.values.ToArray(); m_ProviderIds = providers.values.ToArray(); m_resourceTypes = types.values.Select(t => new SerializedType() { Value = t }).ToArray(); m_Keys = keys.values.Where(x => x != null) .Select(x => x.ToString()) .ToArray(); //serialize entries { var entryData = new byte[data.Count * k_ByteSize * k_EntryDataItemPerEntry + k_ByteSize]; var entryDataOffset = SerializationUtilities.WriteInt32ToByteArray(entryData, data.Count, 0); for (int i = 0; i < data.Count; i++) { var e = data[i]; entryDataOffset = SerializationUtilities.WriteInt32ToByteArray(entryData, internalIds.map[e.InternalId], entryDataOffset); entryDataOffset = SerializationUtilities.WriteInt32ToByteArray(entryData, providers.map[e.Provider], entryDataOffset); entryDataOffset = SerializationUtilities.WriteInt32ToByteArray(entryData, e.Dependencies.Count == 0 ? -1 : keyIndexToEntries.map[e.Dependencies[0]], entryDataOffset); entryDataOffset = SerializationUtilities.WriteInt32ToByteArray(entryData, e.ComputeDependencyHash(), entryDataOffset); entryDataOffset = SerializationUtilities.WriteInt32ToByteArray(entryData, entryIndexToExtraDataIndex[i], entryDataOffset); entryDataOffset = SerializationUtilities.WriteInt32ToByteArray(entryData, keys.map[e.Keys.First()], entryDataOffset); entryDataOffset = SerializationUtilities.WriteInt32ToByteArray(entryData, types.map[e.ResourceType], entryDataOffset); } m_EntryDataString = Convert.ToBase64String(entryData); } //serialize keys and mappings { var entryCount = keyIndexToEntries.values.Aggregate(0, (a, s) => a += s.Count); var bucketData = new byte[4 + keys.values.Count * 8 + entryCount * 4]; var keyData = new List <byte>(keys.values.Count * 10); keyData.AddRange(BitConverter.GetBytes(keys.values.Count)); int keyDataOffset = 4; int bucketDataOffset = SerializationUtilities.WriteInt32ToByteArray(bucketData, keys.values.Count, 0); for (int i = 0; i < keys.values.Count; i++) { var key = keys.values[i]; bucketDataOffset = SerializationUtilities.WriteInt32ToByteArray(bucketData, keyDataOffset, bucketDataOffset); keyDataOffset += SerializationUtilities.WriteObjectToByteList(key, keyData); var entries = keyIndexToEntries[key]; bucketDataOffset = SerializationUtilities.WriteInt32ToByteArray(bucketData, entries.Count, bucketDataOffset); foreach (var e in entries) { bucketDataOffset = SerializationUtilities.WriteInt32ToByteArray(bucketData, entryToIndex[e], bucketDataOffset); } } m_BucketDataString = Convert.ToBase64String(bucketData); m_KeyDataString = Convert.ToBase64String(keyData.ToArray()); } }
/// <summary> /// Set the data before serialization /// </summary> /// <param name="data">The list of </param> public void SetData(IList <ContentCatalogDataEntry> data) { if (data == null) { return; } var providers = new KeyIndexer <string>(data.Select(s => s.Provider), 10); var internalIds = new KeyIndexer <string>(data.Select(s => s.InternalId), data.Count); var keys = new KeyIndexer <object>(data.SelectMany(s => s.Keys), data.Count * 3); keys.Add(data.SelectMany(s => s.Dependencies)); var keyIndexToEntries = new KeyIndexer <List <ContentCatalogDataEntry>, object>(keys.values, s => new List <ContentCatalogDataEntry>(), keys.values.Count); var entryToIndex = new Dictionary <ContentCatalogDataEntry, int>(data.Count); var extraDataList = new List <byte>(); var entryIndexToExtraDataIndex = new Dictionary <int, int>(); int extraDataIndex = 0; //create buckets of key to data entry for (int i = 0; i < data.Count; i++) { var e = data[i]; int extraDataOffset = -1; if (e.Data != null) { var len = SerializationUtilities.WriteObjectToByteList(e.Data, extraDataList); if (len > 0) { extraDataOffset = extraDataIndex; extraDataIndex += len; } } entryIndexToExtraDataIndex.Add(i, extraDataOffset); entryToIndex.Add(e, i); foreach (var k in e.Keys) { keyIndexToEntries[k].Add(e); } } m_ExtraDataString = Convert.ToBase64String(extraDataList.ToArray()); //create extra entries for dependency sets int originalEntryCount = data.Count; for (int i = 0; i < originalEntryCount; i++) { var entry = data[i]; if (entry.Dependencies == null || entry.Dependencies.Count < 2) { continue; } //seed and and factor values taken from https://stackoverflow.com/questions/1646807/quick-and-simple-hash-code-combinations int hashCode = 1009; foreach (var dep in entry.Dependencies) { hashCode = hashCode * 9176 + dep.GetHashCode(); } bool isNew = false; keys.Add(hashCode, ref isNew); if (isNew) { //if this combination of dependecies is new, add a new entry and add its key to all contained entries var deps = entry.Dependencies.Select(d => keyIndexToEntries[d][0]).ToList(); keyIndexToEntries.Add(hashCode, deps); foreach (var dep in deps) { dep.Keys.Add(hashCode); } } //reset the dependency list to only contain the key of the new set entry.Dependencies.Clear(); entry.Dependencies.Add(hashCode); } //serialize internal ids and providers m_InternalIds = internalIds.values.ToArray(); m_ProviderIds = providers.values.ToArray(); //serialize entries { var entryData = new byte[data.Count * 4 * 5 + 4]; var entryDataOffset = SerializationUtilities.WriteInt32ToByteArray(entryData, data.Count, 0); for (int i = 0; i < data.Count; i++) { var e = data[i]; entryDataOffset = SerializationUtilities.WriteInt32ToByteArray(entryData, internalIds.map[e.InternalId], entryDataOffset); entryDataOffset = SerializationUtilities.WriteInt32ToByteArray(entryData, providers.map[e.Provider], entryDataOffset); entryDataOffset = SerializationUtilities.WriteInt32ToByteArray(entryData, e.Dependencies.Count == 0 ? -1 : keyIndexToEntries.map[e.Dependencies[0]], entryDataOffset); entryDataOffset = SerializationUtilities.WriteInt32ToByteArray(entryData, e.ComputeDependencyHash(), entryDataOffset); entryDataOffset = SerializationUtilities.WriteInt32ToByteArray(entryData, entryIndexToExtraDataIndex[i], entryDataOffset); } m_EntryDataString = Convert.ToBase64String(entryData); } //serialize keys and mappings { var entryCount = keyIndexToEntries.values.Aggregate(0, (a, s) => a += s.Count); var bucketData = new byte[4 + keys.values.Count * 8 + entryCount * 4]; var keyData = new List <byte>(keys.values.Count * 10); keyData.AddRange(BitConverter.GetBytes(keys.values.Count)); int keyDataOffset = 4; int bucketDataOffset = SerializationUtilities.WriteInt32ToByteArray(bucketData, keys.values.Count, 0); for (int i = 0; i < keys.values.Count; i++) { var key = keys.values[i]; bucketDataOffset = SerializationUtilities.WriteInt32ToByteArray(bucketData, keyDataOffset, bucketDataOffset); keyDataOffset += SerializationUtilities.WriteObjectToByteList(key, keyData); var entries = keyIndexToEntries[key]; bucketDataOffset = SerializationUtilities.WriteInt32ToByteArray(bucketData, entries.Count, bucketDataOffset); foreach (var e in entries) { bucketDataOffset = SerializationUtilities.WriteInt32ToByteArray(bucketData, entryToIndex[e], bucketDataOffset); } } m_BucketDataString = Convert.ToBase64String(bucketData); m_KeyDataString = Convert.ToBase64String(keyData.ToArray()); } }
public string Decrypt(string cipherText) { int[] num = new int[Key.Length]; int[] back = new int[Key.Length]; string[] columns = new string[Key.Length]; int minNum; StringBuilder output = new StringBuilder(); minNum = cipherText.Length / Key.Length; KeyIndexer key = new KeyIndexer(); for (int i = 0; i < Key.Length; i++) { for (int j = 0; j < alphabet.Length; j++) { if (Key[i] == alphabet[j]) { key.Add(i, j); } } } key.ReindexValues(); for (int i = 0; i < num.Length; i++) { num[i] = minNum; back[key[i]] = i; } int k = 0; for (int j = minNum * Key.Length; j < cipherText.Length; j++) { num[key[k++]]++; } for (int i = 0; i < Key.Length; i++) { columns[back[i]] = cipherText.Substring(0, num[i]); cipherText = cipherText.Substring(num[i], cipherText.Length - num[i]); } for (int i = 0; i < minNum + 1; i++) { for (int j = 0; j < columns.Length; j++) { if (columns[j].Length > i) { output.Append(columns[j][i]); } } } return output.ToString(); //StringBuilder output = new StringBuilder(cipherText.ToUpper()); //StringBuilder descramble = new StringBuilder(cipherText.ToUpper()); //Dictionary<int, int> temp = new Dictionary<int, int>(); //for (int i = 0; i < Key.Length; i++) //{ // for (int j = 0; j < alphabet.Length; j++) // { // if (Key[i] == alphabet[j]) // { // temp.Add(j, i); // } // } //} //List<int> locations = new List<int>(temp.Keys); //locations.Sort(); //Dictionary<int, int> indices = new Dictionary<int, int>(); //for (int i = 0; i < locations.Count; i++) //{ // indices.Add(i, temp[locations[i]]); //} //int rows = output.Length / Key.Length; //if (output.Length % Key.Length != 0) //{ // rows++; //} //int k = 0; //for (int j = 0; j < rows; j++) //{ // for (int i = j; i < output.Length; i += rows) // { // descramble[k++] = cipherText[i]; // } //} //k = 0; //for (int j = 0; j < Key.Length; j++) //{ // for (int i = 0; i < output.Length; i += Key.Length) // { // if (i + indices[j] < output.Length && i + j < output.Length) // { // output[i + indices[j]] = descramble[j + i]; // } // } //} //return output.ToString(); }
public string Encrypt(string clearText) { StringBuilder output = new StringBuilder(); KeyIndexer key = new KeyIndexer(); for (int i = 0; i < Key.Length; i++) { for (int j = 0; j < alphabet.Length; j++) { if (Key[i] == alphabet[j]) { key.Add(i, j); } } } key.KeyedBy = KeyedBy.Value; for (int i = 0; i < Key.Length; i++) { for (int j = key[i]; j < clearText.Length; j += Key.Length) { output.Append(clearText[j]); } } return output.ToString(); }
public string Decrypt(string cipherText) { int[] num = new int[Key.Length]; int[] back = new int[Key.Length]; string[] columns = new string[Key.Length]; int minNum; StringBuilder output = new StringBuilder(); minNum = cipherText.Length / Key.Length; KeyIndexer key = new KeyIndexer(); for (int i = 0; i < Key.Length; i++) { for (int j = 0; j < alphabet.Length; j++) { if (Key[i] == alphabet[j]) { key.Add(i, j); } } } key.ReindexValues(); for (int i = 0; i < num.Length; i++) { num[i] = minNum; back[key[i]] = i; } int k = 0; for (int j = minNum * Key.Length; j < cipherText.Length; j++) { num[key[k++]]++; } for (int i = 0; i < Key.Length; i++) { columns[back[i]] = cipherText.Substring(0, num[i]); cipherText = cipherText.Substring(num[i], cipherText.Length - num[i]); } for (int i = 0; i < minNum + 1; i++) { for (int j = 0; j < columns.Length; j++) { if (columns[j].Length > i) { output.Append(columns[j][i]); } } } return(output.ToString()); //StringBuilder output = new StringBuilder(cipherText.ToUpper()); //StringBuilder descramble = new StringBuilder(cipherText.ToUpper()); //Dictionary<int, int> temp = new Dictionary<int, int>(); //for (int i = 0; i < Key.Length; i++) //{ // for (int j = 0; j < alphabet.Length; j++) // { // if (Key[i] == alphabet[j]) // { // temp.Add(j, i); // } // } //} //List<int> locations = new List<int>(temp.Keys); //locations.Sort(); //Dictionary<int, int> indices = new Dictionary<int, int>(); //for (int i = 0; i < locations.Count; i++) //{ // indices.Add(i, temp[locations[i]]); //} //int rows = output.Length / Key.Length; //if (output.Length % Key.Length != 0) //{ // rows++; //} //int k = 0; //for (int j = 0; j < rows; j++) //{ // for (int i = j; i < output.Length; i += rows) // { // descramble[k++] = cipherText[i]; // } //} //k = 0; //for (int j = 0; j < Key.Length; j++) //{ // for (int i = 0; i < output.Length; i += Key.Length) // { // if (i + indices[j] < output.Length && i + j < output.Length) // { // output[i + indices[j]] = descramble[j + i]; // } // } //} //return output.ToString(); }
public string Decrypt(string cipherText) { KeyIndexer oto = new KeyIndexer(); for (int i = 0; i < Key.Length; i++) { oto.Add(i, Key[i]); } oto.KeyedBy = KeyedBy.Value; int lastChar = cipherText.Length % 3;//r=0, 2 means no special consideration when decrypting, mod = 1 means single should be double char int numPositions = 2 * (cipherText.Length / 3); if (lastChar != 0) { numPositions++; } int numRows = numPositions / Key.Length; int remainder = numPositions % Key.Length; string[,] pieces = new string[numRows + 1, Key.Length]; int k = 0; for (int i = 0; i < oto.Count; i++) { int x = oto[i]; for (int j = 0; j < numRows + (x < remainder ? 1 : 0); j++) { if ((x & 0x01) == 0) { if ((j & 0x01) == 0) { pieces[j, x] = cipherText[k++].ToString(); if (k < cipherText.Length && !(x == remainder - 1 && lastChar == 1 && j == numRows - 1 + (x < remainder ? 1 : 0))) { pieces[j, x] += cipherText[k++].ToString(); } } else { pieces[j, x] = cipherText[k++].ToString(); } } else { if ((j & 0x01) == 1) { pieces[j, x] = cipherText[k++].ToString(); if (k < cipherText.Length && !(x == remainder - 1 && lastChar == 1 && j == numRows - 1 + (x < remainder ? 1 : 0))) { pieces[j, x] += cipherText[k++].ToString(); } } else { pieces[j, x] = cipherText[k++].ToString(); } } } } StringBuilder output = new StringBuilder(); for (int i = 0; i < numRows + (remainder > 0?1:0); i++) { for (int j = 0; j < Key.Length; j++) { output.Append(pieces[i, j] ?? ""); } } return(output.ToString()); }