/// <summary> /// Validate a password by checking the checksum. /// </summary> /// <param name="password">Password to validate.</param> /// <returns><c>true</c> if the password is valid, <c>false</c> otherwise.</returns> public static bool ValidatePassword(string password) { if (string.IsNullOrEmpty(password)) { throw new ArgumentNullException(nameof(password)); } // Sanitaze and check password password = password.Replace(" ", ""); password = password.Replace(Environment.NewLine, ""); password = password.ToUpper(); if (password.Length != PasswordLength) { throw new ArgumentException("Invalid password length"); } // Do decryption rounds byte[] binary; try { password = Permutation.Decrypt(password, false); binary = Substitution.Decrypt(password); Scramble.Decrypt(binary[0], binary, 4, binary.Length - 5); } catch { return(false); } // Validate checksum uint crc32 = BitConverter.ToUInt32(binary, 0); uint newCrc32 = Crc32.Calculate(binary, 4, binary.Length - 5); return(crc32 == newCrc32); }
/// <summary> /// Converts a password into mail information. /// </summary> /// <param name="password">The password to convert.</param> /// <returns>Mail information from the password.</returns> public static WonderSMail Convert(string password) { if (string.IsNullOrEmpty(password)) { throw new ArgumentNullException(nameof(password)); } // Sanitaze and check password password = password.Replace(" ", ""); password = password.Replace("\t", ""); password = password.Replace(Environment.NewLine, ""); password = password.ToUpper(); if (password.Length != PasswordLength) { throw new ArgumentException("Invalid password length"); } // Do decryption rounds // The last byte for "scramble" is ignored. It should be the null // terminator 0x00. password = Permutation.Decrypt(password, false); byte[] binary = Substitution.Decrypt(password); Scramble.Decrypt(binary[0], binary, 4, binary.Length - 5); // Validate checksum uint crc32 = BitConverter.ToUInt32(binary, 0); uint newCrc32 = Crc32.Calculate(binary, 4, binary.Length - 5); if (crc32 != newCrc32) { throw new FormatException("Invalid crc32"); } // Convert the binary password into the structure. // Write the array into a stream to use the BitReader. DataStream stream = new DataStream(); stream.Write(binary, 4, binary.Length - 4); BitReader reader = new BitReader(stream); WonderSMail info = new WonderSMail(); info.MailType = reader.ReadByte(4); info.MissionType = reader.ReadByte(4); info.MissionSubType = reader.ReadByte(4); info.SourceClientId = reader.ReadUInt16(11); info.TargetClientId = reader.ReadUInt16(11); info.TargetClientFemale = reader.ReadUInt16(11); info.RewardObjectId = reader.ReadUInt16(10); info.RewardType = reader.ReadByte(4); info.RewardId = reader.ReadUInt16(11); info.RestrictionType = reader.ReadByte(1); info.RestrictionParam = reader.ReadUInt16(11); info.Random = reader.ReadUInt32(24); info.LocationId = reader.ReadByte(8); info.FloorNumber = reader.ReadByte(8); info.Requirement = reader.ReadByte(8); return(info); }
/// <summary> /// Validate a password by checking the checksum. /// </summary> /// <param name="password">Password to validate.</param> /// <returns><c>true</c> if the password is valid, <c>false</c> otherwise.</returns> public static bool ValidatePassword(string password) { if (string.IsNullOrEmpty(password)) { throw new ArgumentNullException(nameof(password)); } // Sanitaze and check password password = password.Replace(" ", ""); password = password.Replace(Environment.NewLine, ""); password = password.ToUpper(); if (password.Length != PasswordLength) { throw new ArgumentException("Invalid password length"); } // Do decryption rounds byte[] binary; try { password = Permutation.Decrypt(password, true); binary = Substitution.Decrypt(password); Scramble.Decrypt(binary[0], binary, 1, binary.Length - 2); } catch { return(false); } // Validate checksum byte checksum = binary[0]; byte newChecksum = Checksum.Calculate(binary, 1, binary.Length - 1); return(checksum == newChecksum); }
public void EncryptDecryptBasic() { Substitution sub = new Substitution(new Alphabet("PHQGIUMEAYLNOFDXJKRCVSTZWB")); var encrypted = sub.Encrypt(TextExample); Assert.AreEqual("GIUIFGCEIIPRCTPNN", encrypted); var decrypted = sub.Decrypt(encrypted); Assert.AreEqual(TextExample, decrypted); }
/// <summary> /// Converts a password into mail information. /// </summary> /// <param name="password">The password to convert.</param> /// <returns>Mail information from the password.</returns> public static MissionMail Convert(string password) { if (string.IsNullOrEmpty(password)) { throw new ArgumentNullException(nameof(password)); } // Sanitaze and check password password = password.Replace(" ", ""); password = password.Replace(Environment.NewLine, ""); password = password.ToUpper(); if (password.Length != PasswordLength) { throw new ArgumentException("Invalid password length"); } // Do decryption rounds // The last byte for "scramble" is ignored. It should be the null // terminator 0x00. password = Permutation.Decrypt(password, true); byte[] binary = Substitution.Decrypt(password); Scramble.Decrypt(binary[0], binary, 1, binary.Length - 2); // Validate checksum byte checksum = binary[0]; // The scramble key is the checksum too. byte newChecksum = Checksum.Calculate(binary, 1, binary.Length - 1); if (checksum != newChecksum) { throw new FormatException("Invalid checksum"); } // Convert the binary password into the structure. // Write the array into a stream to use the BitReader. DataStream stream = new DataStream(); stream.Write(binary, 1, binary.Length - 1); BitReader reader = new BitReader(stream); MissionMail info = new MissionMail(); info.Type = (MissionState)reader.ReadByte(4); info.LocationId = reader.ReadByte(7); info.FloorNumber = reader.ReadByte(7); info.Random = (info.Type == MissionState.Sos) ? reader.ReadUInt32(24) : 0x00; info.UID = reader.ReadUInt64(64); info.ClientLanguage = (GameLanguage)reader.ReadByte(4); info.ClientName = reader.ReadString(80, EncodingName); info.ObjectID1 = (info.Type == MissionState.Sos) ? (ushort)0x00 : reader.ReadUInt16(10); info.ObjectID2 = (info.Type == MissionState.Sos) ? (ushort)0x00 : reader.ReadUInt16(10); info.RescuerUID = reader.ReadUInt64(64); info.GameType = (GameType)reader.ReadByte(2); return(info); }
public IActionResult DecryptMessage([FromBody] VigenereModel message) { var encryptedText = message.Message; var alphabet = message.Alphabet; var password = message.Password; Substitution cipher = new Substitution(alphabet); message.Message = cipher.Decrypt(encryptedText, password); return(Json(message)); }
public void Multigraph_SubstitutionTest() { Substitution substitution = new Substitution(Utility.KeyedEnglishAlphabet("KRYPTOS").ToStringArray()); for (int i = 0; i < 25; i++) { substitution.Key = substitution.ScrambledAlphabet(); generated = substitution.GenerateRandomLetters(); cipher = substitution.Encrypt(generated); clear = substitution.Decrypt(cipher); CollectionAssert.AreEqual(generated, clear); } }
public void Unigraph_SubstitutionTest() { Substitution substitution = new Substitution(Utility.KeyedEnglishAlphabet("KRYPTOS")); cipher = ""; clear = ""; generated = ""; for (int i = 0; i < 25; i++) { substitution.Key = substitution.ScrambledAlphabet(); generated = substitution.GenerateRandomString(); cipher = substitution.Encrypt(generated); clear = substitution.Decrypt(cipher); Assert.AreEqual(generated, clear); } }
public static void RunThirdTask() { var basePath = $"{Directory.GetParent(Directory.GetCurrentDirectory()).Parent.Parent}\\Task3\\Data"; string allText = File.ReadAllText($"{basePath}\\bigrams_percentages.json"); var bigrams = JsonConvert.DeserializeObject <Dictionary <string, float> >(allText) .Select(t => new EtalonMember(t.Key, t.Value)).ToList(); allText = File.ReadAllText($"{basePath}\\trigrams_percentages.json"); var trigrams = JsonConvert.DeserializeObject <Dictionary <string, float> >(allText) .Select(t => new EtalonMember(t.Key, t.Value)).ToList(); string thirdTaskEncryptedMessage = "EFFPQLEKVTVPCPYFLMVHQLUEWCNVWFYGHYTCETHQEKLPVMSAKSPVPAPVYWMVHQLUSPQLYWLASLFVWPQLMVHQLUPLRPSQLULQESPBLWPCSVRVWFLHLWFLWPUEWFYOTCMQYSLWOYWYETHQEKLPVMSAKSPVPAPVYWHEPPLUWSGYULEMQTLPPLUGUYOLWDTVSQETHQEKLPVPVSMTLEUPQEPCYAMEWWYTYWDLUULTCYWPQLSEOLSVOHTLUYAPVWLYGDALSSVWDPQLNLCKCLRQEASPVILSLEUMQBQVMQCYAHUYKEKTCASLFPYFLMVHQLUPQLHULIVYASHEUEDUEHQBVTTPQLVWFLRYGMYVWMVFLWMLSPVTTBYUNESESADDLSPVYWCYAMEWPUCPYFVIVFLPQLOLSSEDLVWHEUPSKCPQLWAOKLUYGMQEUEMPLUSVWENLCEWFEHHTCGULXALWMCEWETCSVSPYLEMQYGPQLOMEWCYAGVWFEBECPYASLQVDQLUYUFLUGULXALWMCSPEPVSPVMSBVPQPQVSPCHLYGMVHQLUPQLWLRPOEDVMETBYUFBVTTPENLPYPQLWLRPTEKLWZYCKVPTCSTESQPQULLGYAUMEHVPETFWMEHVPETBZMEHVPETB"; string alphabet = new string(SingleByteXorAttacker.OneLetterEnglishFrequency.Select(c => c.Key).ToArray()); string thirdTaskKey = "EKMFLGDQVZNTOWYHXUSPAIBRCJ".ToLower(); var substitution = new Substitution(alphabet, new List <string> { thirdTaskKey }); Console.WriteLine(substitution.Decrypt(thirdTaskEncryptedMessage.ToLower())); //var substitutionAttacker = new SubstitutionAttacker( // encryptedText: thirdTaskEncryptedMessage, // individualSetMembersCount: 1, // minPopulationSize: 40, // maxPopulationSize: 100, // iterationsCount: 1000, // mutationPercentage: 0.02F, // bestPercentage: 30, // twoLettersFrequencies: bigrams, // threeLettersFrequencies: trigrams, // twoLettersFittingQuotientCoef: 0.5F, // threeLettersFittingQuotientCoef: 1.5F); //List<IndividualSet> keySets = substitutionAttacker.Evaluate().Result; //for(int i = 0; i < 10; i++) //{ // Console.WriteLine($"Key set {i}:"); // keySets[i].ForEach(Console.WriteLine); // Console.WriteLine(); //} }