private static void Substring(string logRootPath, EncodedText et) { SubstringKeyAnalyzer sa = new SubstringKeyAnalyzer(et, 5, 3); ILogger substringKeyAnalyzerLogger = new FileLogger(logRootPath + "\\substring.log"); sa.Analyze(substringKeyAnalyzerLogger); }
private static void CoincidenceKeyLength(string logRootPath, EncodedText et) { CoincidenceKeyLengthAnalyzer ca = new CoincidenceKeyLengthAnalyzer(et); ILogger coincidenceKeyLengthAnalyzerLogger = new FileLogger(logRootPath + "\\coincidence-key.log"); ca.Analyze(coincidenceKeyLengthAnalyzerLogger); }
/// <summary> /// Encodes a text from Harvest Moon: Save the Homeland. /// </summary> /// <param name="Text">The string to be encoded</param> /// <param name="Data">The full path to the data file that should be created</param> /// <param name="Pointers">The full path to the pointers file that should be created</param> public static void Encode(string Text, string Data, string Pointers) { EncodedText Encoded = Encode(Text); File.WriteAllBytes(Data, Encoded.Data); File.WriteAllBytes(Pointers, Encoded.Pointers); }
public EncodedText AddEncodedText(string initText, string encodedText, string keyword) { using (var db = new Context()) { Text text = db.Texts.FirstOrDefault(t => t.Value == initText); if (ReferenceEquals(text, null)) { text = db.Texts.Add(new Text() { Value = initText }); } db.SaveChanges(); EncodedText encoded = db.EncodedTexts.Add(new EncodedText() { InitialTextId = text.Id, InitialText = text, Value = encodedText, Keyword = keyword }); db.SaveChanges(); return(encoded); } }
private static void CoincidenceKeyText(string logRootPath, EncodedText et) { CoincidenceKeyTextAnalyzer ca = new CoincidenceKeyTextAnalyzer(et, 1); ILogger coincidenceKeyTextAnalyzerLogger = new FileLogger(logRootPath + "\\coincidence-text.log"); ca.Analyze(coincidenceKeyTextAnalyzerLogger); }
private static void Decode(string logRootPath, EncodedText et) { Key key = new Key("БРУНО".ToCharArray(), Alphabet.Russian); IDecoder dec = new Decoder(); dec.Decode(et, key).WriteFile(logRootPath + "\\source-text.txt"); }
public EncodedText GetEncodedText(string initText, string keyword) { using (var db = new Context()) { EncodedText encoded = db.EncodedTexts.FirstOrDefault(t => t.InitialText.Value == initText && t.Keyword == keyword); return(encoded); } }
public static Dictionary <char, long> GetLettersOccurrences(EncodedText et) { Dictionary <char, long> result = CoincidenceKeyLengthAnalyzer.GetLetterOccurrenceDictionary(et.Alphabet); for (long i = 0, l = et.Length; i < l && et[i] != '\0'; ++i) { result[et[i]]++; } return(result); }
private static double GetCoincidenceIndex(EncodedText et) { Dictionary <char, long> lettersOccurences = CoincidenceKeyLengthAnalyzer.GetLettersOccurrences(et); long sum = 0; foreach (KeyValuePair <char, long> letterOccurrences in lettersOccurences) { sum += letterOccurrences.Value * letterOccurrences.Value; } return((double)sum / (et.Length * et.Length)); }
static void Task4Solver(string logRootPath) { EncodedText et = new EncodedText(logRootPath + "\\encoded.txt", Alphabet.Russian); //CoincidenceKeyText(logRootPath, et); //CoincidenceKeyLength(logRootPath, et); //Substring(logRootPath, et); Decode(logRootPath, et); }
public Text GetDecodedText(string encodedText, string keyword) { using (var db = new Context()) { EncodedText encoded = db.EncodedTexts.FirstOrDefault(text => text.Value == encodedText && text.Keyword == keyword); if (ReferenceEquals(encoded, null)) { return(null); } return(encoded.InitialText); } }
private void Cipher_SelectionChanged(object sender, SelectionChangedEventArgs e) { EncodedText.Clear(); DecodedText.Clear(); tbTextfromDecrHash.Clear(); tbKey.Clear(); if (Cipher.SelectedIndex == 0 || Cipher.SelectedIndex == 2) { tbKey.IsHitTestVisible = true; } else { tbKey.IsHitTestVisible = false; tbKey.Clear(); } }
static private void Task2Solver(string logRootPath) { SourceText st = new SourceText(logRootPath + "\\source.txt", Alphabet.Russian); Key key = ReadKey(logRootPath + "\\key.txt", Alphabet.Russian); IEncoder encoder = new VigenereCode.Core.Crypto.Encoder(); EncodedText outText = encoder.Encode(st, key); outText.WriteFile(logRootPath + "\\result.txt"); CoincidenceKeyLengthAnalyzer ca = new CoincidenceKeyLengthAnalyzer(outText); ILogger coincidenceKeyLengthAnalyzerLogger = new FileLogger(logRootPath + "\\KasiskiTest.log"); ca.Analyze(coincidenceKeyLengthAnalyzerLogger); //IDecoder decoder = new Decoder(); //(decoder.Decode(outText, key)).WriteFile(logRootPath + "\\source-back.txt"); }
public Text AddDecodedText(string initText, string decodedText, string keyword) { using (var db = new Context()) { Text text = db.Texts.FirstOrDefault(t => t.Value == initText); if (ReferenceEquals(text, null)) { text = db.Texts.Create(); } text.Value = initText; db.SaveChanges(); EncodedText encoded = db.EncodedTexts.FirstOrDefault(t => t.Value == initText && t.Keyword == keyword); if (ReferenceEquals(encoded, null)) { encoded = db.EncodedTexts.Create(); } encoded.InitialTextId = text.Id; encoded.InitialText = text; encoded.Keyword = keyword; encoded.Value = decodedText; return(encoded.InitialText); } }
public IEnumerable <KasiskiResultItem> AddKasiskiResult(string text, IEnumerable <Tuple <int, double> > results) { using (var db = new Context()) { EncodedText encoded = db.EncodedTexts.FirstOrDefault(t => t.Value == text); if (ReferenceEquals(encoded, null)) { encoded = db.EncodedTexts.Add(new EncodedText() { Value = text }); } db.SaveChanges(); KasiskiResult result = db.KasiskiResults.Add(new KasiskiResult() { EncodedTextId = encoded.Id, EncodedTextField = encoded }); db.SaveChanges(); var items = new List <KasiskiResultItem>(); foreach (var res in results) { var r = db.KasiskiResultItems.Add(new KasiskiResultItem() { Size = res.Item1, Probability = res.Item2, KasiskiResultField = result, KasiskiResultId = result.Id }); db.SaveChanges(); items.Add(r); } result.Results = items; db.SaveChanges(); return(items); } }
/// <exception cref="Mp3net.InvalidDataException"></exception> protected internal override void UnpackFrameData(byte[] bytes) { text = new EncodedText(bytes[0], BufferTools.CopyBuffer(bytes, 1, bytes.Length - 1)); }
/// <summary> /// Encodes a text from Harvest Moon: Save the Homeland. /// </summary> /// <param name="Text">The string to be encoded</param> /// <returns>The encoded data</returns> public static EncodedText Encode(string Text) { EncodedText Output = new EncodedText(); string[] Table = GetTable(); using (MemoryStream Data = new MemoryStream()) { using (MemoryStream Pointers = new MemoryStream()) { BinaryWriter Writer = new BinaryWriter(Data); BinaryWriter Pointer = new BinaryWriter(Pointers); string[] Dialogs = Text.Split(new string[] { Table[2] }, StringSplitOptions.RemoveEmptyEntries); foreach (string Dialog in Dialogs) { Align(Data, 4); Pointer.Write((uint)Data.Position); byte Header = 0; byte Mask = 0; long Position = 0; long HeaderPosition = Data.Position; for (int Index = 0; Index < Dialog.Length; Index++) { if ((Mask >>= 1) == 0) { Data.WriteByte(0); Position = Data.Position; Data.Seek(HeaderPosition, SeekOrigin.Begin); Data.WriteByte(Header); Data.Seek(Position, SeekOrigin.Begin); HeaderPosition = Position - 1; Header = 0; Mask = 0x80; } if (Index + 2 <= Dialog.Length && Dialog.Substring(Index, 2) == Environment.NewLine) { //Line break Data.WriteByte(0); Index++; } else if (Index + 2 <= Dialog.Length && Dialog.Substring(Index, 2) == "\\x") { //Unknown data = Hex code string Hex = Dialog.Substring(Index + 2, 4); ushort Value = ushort.Parse(Hex, NumberStyles.HexNumber); if (Value > 0xff) { Writer.Write(Value); Header |= Mask; } else { Data.WriteByte((byte)Value); } Index += 5; } else { //Character int Value = -1; string Character = Dialog.Substring(Index, 1); if (Character == "[") { //Slow search method for table elements with more than 1 character for (int TblIndex = 0; TblIndex < Table.Length; TblIndex++) { string TblValue = Table[TblIndex]; if (TblValue == null || Index + TblValue.Length > Dialog.Length) { continue; } if (Dialog.Substring(Index, TblValue.Length) == TblValue) { Value = TblIndex; Index += TblValue.Length - 1; break; } } } else { Value = Array.IndexOf(Table, Character); } if (Value > -1) { if (Value > 0xff) { Writer.Write((ushort)Value); Header |= Mask; } else { Data.WriteByte((byte)Value); if (Value == 7) { Mask <<= 1; } } } else { Data.WriteByte(0x10); //Unknown, add space } } } //End of dialog Position = Data.Position; if (Header != 0) { Data.Seek(HeaderPosition, SeekOrigin.Begin); Data.WriteByte((byte)Header); } Data.Seek(Position, SeekOrigin.Begin); if (Mask == 1) { Data.WriteByte(0); } Data.WriteByte(2); } Align(Data, 4); Pointer.Write((uint)Data.Length); Align(Data, 0x10); Align(Pointers, 0x10); Output.Data = Data.ToArray(); Output.Pointers = Pointers.ToArray(); } } return(Output); }
public SubstringKeyAnalyzer(EncodedText et, int subStringLength = 3, int occurrencesFilter = 4) { encodedText = et; substrLength = subStringLength; this.occurrencesFilter = occurrencesFilter; }
private void EncodeText(object sender, RoutedEventArgs e) { // Caesar cipher encode if (Cipher.Text == "Caesar cipher" && tbKey.Text != "") { lbDecText.Content = " Your Decoded text"; Decode.Content = "Decode"; key = Convert.ToInt32(tbKey.Text); if (EncodedText.Text != "") { EncodedText.Clear(); } try { foreach (var item in charsToRemove) { YourText.Text = YourText.Text.Replace(item, string.Empty); } char[] text = YourText.Text.ToCharArray().Where(s => !char.IsWhiteSpace(s)).ToArray(); for (int i = 0; i < text.Length; i++) { for (int j = 0; j <= alphabet.Length && j <= alphabetUpper.Length; j++) { if (text[i].ToString() == alphabet[j].ToString() || text[i].ToString() == alphabetUpper[j].ToString()) { char.ToLower(text[i]); EncodedText.Text += Convert.ToChar(alphabet[(j + key) % alphabet.Length]); break; } } } } catch (Exception ex) { MessageBox.Show(ex.Message); } } // HASHFUNC encode if (Cipher.Text == "HashFunc") { lbDecText.Content = " Comparable hash"; Decode.Content = "Check!"; string text = YourText.Text; if (string.IsNullOrEmpty(text)) { text = string.Empty; } using (System.Security.Cryptography.SHA512Managed sha = new System.Security.Cryptography.SHA512Managed()) { byte[] textData = Encoding.Default.GetBytes(text); byte[] hash = sha.ComputeHash(textData); EncodedText.Text = BitConverter.ToString(hash).Replace("-", string.Empty); } current = EncodedText.Text; } // Vigenere cipher encode if (Cipher.Text == "Vigenere cipher") { lbDecText.Content = " Your Decoded text"; Decode.Content = "Decode"; if (EncodedText.Text != "") { EncodedText.Clear(); } foreach (var item in charsToRemove) { YourText.Text = YourText.Text.Replace(item, string.Empty); } char[] text = YourText.Text.ToCharArray().Where(s => !char.IsWhiteSpace(s)).ToArray(); char[] key = tbKey.Text.ToCharArray(); try { char[,] Vigenere_Table = new char[26, 26]; int temp = 0; for (int i = 0; i < alphabet.Length; i++) { for (int j = 0; j < 26; j++) { temp = j + i; if (temp >= 26) { temp = temp % 26; } Vigenere_Table[i, j] = alphabet[temp]; } } for (int t = 0, k = 0; t < text.Length || k < key.Length; t++, k++) { if (t >= text.Length) { break; } if (k == key.Length /*t % key.Length == 0*/) { k = 0; for (int y = 0; y <= alphabet.Length; y++) { if (text[t].ToString() == alphabet[y].ToString()) { Ytext = y; for (int x = 0; x <= alphabet.Length; x++) { if (key[k].ToString() == alphabet[x].ToString()) { Xkey = x; EncodedText.Text += Vigenere_Table[Ytext, Xkey].ToString(); break; } } break; } } } else { for (int y = 0; y <= alphabet.Length; y++) { if (text[t].ToString() == alphabet[y].ToString()) { Ytext = y; for (int x = 0; x <= alphabet.Length; x++) { if (key[k].ToString() == alphabet[x].ToString()) { Xkey = x; EncodedText.Text += Vigenere_Table[Ytext, Xkey].ToString(); break; } } break; } } } } } catch (Exception ex) { MessageBox.Show(ex.Message); } } }
public CoincidenceKeyTextAnalyzer(EncodedText et, int keyLength) { encodedText = et; this.keyLength = keyLength; oftenLetter = et.Alphabet.GetOftenLetter(); }
public OccurenceAnalyzer(EncodedText et, int occurrenceFilter) { encodedText = et; this.occurrenceFilter = occurrenceFilter; }
public ID3v2TextFrameData(bool unsynchronisation, EncodedText text) : base(unsynchronisation) { this.text = text; }
SourceText IDecoder.Decode(EncodedText text, Key key) { return(new SourceText(DecodeToText(EncodeDigitalRecord(text.GetDigitCode(), key), text.Alphabet), text.Alphabet)); }
public virtual void SetText(EncodedText text) { this.text = text; }
public CoincidenceKeyLengthAnalyzer(EncodedText et) { encodedText = et; }