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); }
public IActionResult EncryptMessage([FromBody] VigenereModel message) { var plainText = message.Message; var alphabet = message.Alphabet; var password = message.Password; Substitution cipher = new Substitution(alphabet); message.Message = cipher.Encrypt(plainText, password); return(Json(message)); }
/// <summary> /// Converts a missiong information into a password. /// </summary> /// <param name="info">Mission to convert.</param> /// <returns>The password.</returns> public static string Convert(WonderSMail info) { if (info == null) { throw new ArgumentNullException(nameof(info)); } // Serialize the structure into a bit stream DataStream stream = new DataStream(); BitWriter writer = new BitWriter(stream); writer.Write(info.MailType, 4); writer.Write(info.MissionType, 4); writer.Write(info.MissionSubType, 4); writer.Write(info.SourceClientId, 11); writer.Write(info.TargetClientId, 11); writer.Write(info.TargetClientFemale, 11); writer.Write(info.RewardObjectId, 10); writer.Write(info.RewardType, 4); writer.Write(info.RewardId, 11); writer.Write(info.RestrictionType, 1); writer.Write(info.RestrictionParam, 11); writer.Write(info.Random, 24); writer.Write(info.LocationId, 8); writer.Write(info.FloorNumber, 8); writer.Write(info.Requirement, 8); // Write the stream into an array for the rounds. // We allocate an extra space for the checksum (first uint) // and the null terminator (last byte). byte[] binary = new byte[stream.Length + 5]; stream.Position = 0; stream.Read(binary, 4, (int)stream.Length); // Create checksum uint crc32 = Crc32.Calculate(binary, 4, binary.Length - 5); byte[] crc32Bytes = BitConverter.GetBytes(crc32); binary[0] = crc32Bytes[0]; binary[1] = crc32Bytes[1]; binary[2] = crc32Bytes[2]; binary[3] = crc32Bytes[3]; // Do encryption rounds // The key is the checksum, we don't encrypt the null terminator. Scramble.Encrypt(crc32Bytes[0], binary, 4, binary.Length - 5); string password = Substitution.Encrypt(binary, PasswordLength); password = Permutation.Encrypt(password, false); return(password); }
/// <summary> /// Converts a missiong information into a password. /// </summary> /// <param name="info">Mission to convert.</param> /// <returns>The password.</returns> public static string Convert(MissionMail info) { if (info == null) { throw new ArgumentNullException(nameof(info)); } // Serialize the structure into a bit stream DataStream stream = new DataStream(); BitWriter writer = new BitWriter(stream); writer.Write((byte)info.Type, 4); writer.Write(info.LocationId, 7); writer.Write(info.FloorNumber, 7); if (info.Type == MissionState.Sos) { writer.Write(info.Random, 24); } writer.Write(info.UID, 64); writer.Write((byte)info.ClientLanguage, 4); writer.Write(info.ClientName, 80, EncodingName); if (info.Type != MissionState.Sos) { writer.Write(info.ObjectID1, 10); writer.Write(info.ObjectID2, 10); } writer.Write(info.RescuerUID, 64); writer.Write((byte)info.GameType, 2); // Write the stream into an array for the rounds. // We allocate an extra space for the checksum (first byte) // and the null terminator (last byte). byte[] binary = new byte[stream.Length + 2]; stream.Position = 0; stream.Read(binary, 1, binary.Length - 2); // Create checksum byte checksum = Checksum.Calculate(binary, 1, binary.Length - 1); binary[0] = checksum; // Do encryption rounds // The key is the checksum, we don't encrypt the null terminator. Scramble.Encrypt(checksum, binary, 1, binary.Length - 2); string password = Substitution.Encrypt(binary, PasswordLength); password = Permutation.Encrypt(password, true); return(password); }
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); } }