private static StringBuilder encodeByte(StringBuilder s, GeneratorLFSR generatorLFSR) { for (int i = 0; i < s.Length; i++) // makes XOR with xor and x for all digits { s[i] = toChar(toInt(s[i]) ^ generatorLFSR.getOneBitStandard()); } return(s); }
private static StringBuilder encodeByte3(StringBuilder s, GeneratorLFSR generatorLFSR) { for (int i = 0; i < s.Length; i++) // makes XOR for all digits { s[i] = toChar(toInt(s[i]) ^ generatorLFSR.getOneBitExtended()); generatorLFSR.updateTmp(toInt(s[i])); } return(s); }
private static StringBuilder decodeByte(StringBuilder s, GeneratorLFSR generatorLFSR) { int y; for (int i = 0; i < s.Length; i++) // makes XOR for all digits { y = toInt(s[i]); s[i] = toChar((y ^ generatorLFSR.getOneBitExtended())); generatorLFSR.updateTmp(y); } return(s); }
private static byte[] exercise3encode(byte[] fileBytes, GeneratorLFSR generatorLFSR) { generatorLFSR.reset(); byte[] encodedBytes = new byte[fileBytes.Length]; StringBuilder s = new StringBuilder(); for (int i = 0; i < fileBytes.Length; i++) // for every byte in file { s.Clear(); s.Insert(0, Convert.ToString(fileBytes[i], 2).PadLeft(8, '0')); // we have string of 8 digits representing one byte. Example 00001100 s = encodeByte3(s, generatorLFSR); encodedBytes[i] = Convert.ToByte(s.ToString(), 2); } return(encodedBytes); }
private static string exercise1(GeneratorLFSR generatorLFSR, string seed) { String s = ""; Console.WriteLine(); for (int i = 0; i < seed.Length; i++) { for (int j = 0; j < generatorLFSR.tmp.Length; j++) { Console.Write(generatorLFSR.tmp[j]); } Console.WriteLine(); s += generatorLFSR.getOneBitStandard(); } return(s); }
static void Main(string[] args) { int i; string s = "", f = ""; // seed, polynomial function string fileName = "test"; GeneratorLFSR generatorLFSR; byte[] fileBytes; byte[] encodedBytes; byte[] decodedBytes; BinaryWriter br; bool b = true; string action = ""; s = "1101"; f = "1001"; //setFunctionAndSeed(ref f, ref s); generatorLFSR = new GeneratorLFSR(f, s); // testing, generate four bits //Console.Write("{0,26}", "Y: "); //Console.WriteLine("{0}{1}{2}{3}", generatorLFSR.getOneBit(), generatorLFSR.getOneBit(), generatorLFSR.getOneBit(), generatorLFSR.getOneBit()); while (b) { Console.WriteLine("\n****************************************************************************************************************\n" + "f: " + f + "\n" + "seed: " + s + "\n" + "file: " + fileName + "\n" + "last action: " + action + "\n" + "Choose action: change function and seed(c) | change file(cf) | resetGenerator(r) | 1 | 2e | 2d | 3e | 3d | q"); action = Console.ReadLine(); switch (action) { case "c": setFunctionAndSeed(ref f, ref s); generatorLFSR.init(f, s); break; case "cf": setFile(ref fileName); break; case "r": generatorLFSR.reset(); break; case "1": string e1 = exercise1(generatorLFSR, s); Console.WriteLine("\nY: " + e1); break; case "2e": // reading all bytes from file fileBytes = File.ReadAllBytes(System.IO.Directory.GetCurrentDirectory() + @"\" + fileName + ".bin"); // encoding encodedBytes = exercise2encode(fileBytes, generatorLFSR); // write all bytes into the file br = new BinaryWriter(new FileStream(System.IO.Directory.GetCurrentDirectory() + @"\" + fileName + "encoded.bin", FileMode.Create)); br.Write(encodedBytes); br.Close(); break; case "2d": // reading all bytes from file fileBytes = File.ReadAllBytes(System.IO.Directory.GetCurrentDirectory() + @"\" + fileName + "encoded.bin"); // decoding decodedBytes = exercise2decode(fileBytes, generatorLFSR); // write all bytes into the file br = new BinaryWriter(new FileStream(System.IO.Directory.GetCurrentDirectory() + @"\" + fileName + ".bin", FileMode.Create)); br.Write(decodedBytes); br.Close(); break; case "3e": // reading all bytes from file fileBytes = File.ReadAllBytes(System.IO.Directory.GetCurrentDirectory() + @"\" + fileName + ".bin"); // encoding encodedBytes = exercise3encode(fileBytes, generatorLFSR); // write all bytes into the file br = new BinaryWriter(new FileStream(System.IO.Directory.GetCurrentDirectory() + @"\" + fileName + "encoded.bin", FileMode.Create)); br.Write(encodedBytes); br.Close(); break; case "3d": // reading all bytes from file fileBytes = File.ReadAllBytes(System.IO.Directory.GetCurrentDirectory() + @"\" + fileName + "encoded.bin"); // decoding decodedBytes = exercise3decode(fileBytes, generatorLFSR); // write all bytes into the file br = new BinaryWriter(new FileStream(System.IO.Directory.GetCurrentDirectory() + @"\" + fileName + ".bin", FileMode.Create)); br.Write(decodedBytes); br.Close(); break; case "q": b = false; break; } } Console.ReadKey(); }