public long Decrypt(Tuple tuple, Key.PrivateKey key) { var a = tuple.a; var b = tuple.b; var p = key.P; var x = key.X; // q = (a^x)^-1 var q = modular_pow(a, p - 1 - x, p); var m = (b * q) % p; return m; }
public void Encrypt(string fileName, Key.PublicKey key) { tuples.Clear(); FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read); // Block Message Length based on value of P in key int blockLength = getBitLength(key.P); int n_blockLength = blockLength; // byte to read from file int n_byte = 8; int byte_read = fs.ReadByte(); // Message that want to be encrypt long message = 0; // Process Message while (byte_read >= 0) { int temp_bit = -1; while (byte_read >= 0 && message < key.P && n_blockLength > 0) { if (n_byte == 0) { // Read next byte of file byte_read = fs.ReadByte(); n_byte = 8; if (byte_read < 0) break; } int bit = (byte_read >> (n_byte - 1)) % 2 == 0 ? 0 : 1; message = (message << 1 )+ bit; n_byte--; n_blockLength--; } // if Message more than block or P if (message >= key.P || n_blockLength < 0) { temp_bit = (byte_read >> (n_byte - 1)) % 2 == 0 ? 0 : 1; message = message >> 1; } long k = key.P-2; // Must be random long a = modular_pow(key.G, k, key.P); long b = modular_pow(message, key.Y, k, key.P); Tuple t = new Tuple(); t.a = a; t.b = b; tuples.Add(t); message = 0; n_blockLength = blockLength; // if only the temp_bit have any value if (temp_bit >= 0) { message = message << 1 + temp_bit; n_blockLength--; } } fs.Close(); }
public void Encrypt(string fileName, Key.PublicKey key) { tuples.Clear(); FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read); // Block Message Length based on value of P in key int blockLength = getByteLength(key.P); int n_blockLength = blockLength; System.Diagnostics.Debug.WriteLine(blockLength); // byte to read from file int byte_read = 0; // Message that want to be encrypt long message = 0; // Process Message while (byte_read >= 0) { int temp_byte = -1; while (byte_read >= 0 && message < key.P && n_blockLength > 0) { byte_read = fs.ReadByte(); if (byte_read < 0) break; message = 256 * message + byte_read; n_blockLength--; System.Diagnostics.Debug.WriteLine((char)byte_read); } // if Message more than block or P if (message >= key.P ) { temp_byte = byte_read; message = message/256; } long k = 1520; // Must be random long a = modular_pow(key.G, k, key.P); long b = modular_pow(message, key.Y, k, key.P); Tuple t = new Tuple(); t.a = a; t.b = b; tuples.Add(t); System.Diagnostics.Debug.WriteLine("process :" + (char)message + "," + message); //Console.WriteLine(message); message = 0; n_blockLength = blockLength; // if only the temp_bit have any value if (temp_byte >= 0) { message = message*256 + temp_byte; n_blockLength--; } } System.Diagnostics.Debug.WriteLine("Count Encrypt : " + tuples.Count()); fs.Close(); }
public List<Tuple> ReadTuplesFromFile(string fileName, Key.PrivateKey key) { var tuples = new List<Tuple>(); // Read text - init BinaryReader br = new BinaryReader(File.OpenRead(fileName)); // Get block length int blockLength = getByteLength(key.P); int pos = 0; char currentChar; string hexString; while (pos < br.BaseStream.Length) { br.ReadChar(); // ( ++pos; currentChar = br.ReadChar(); ++pos; hexString = ""; while (currentChar != ',') { hexString += currentChar; currentChar = br.ReadChar(); ++pos; } long a = long.Parse(hexString, System.Globalization.NumberStyles.HexNumber); currentChar = br.ReadChar(); ++pos; hexString = ""; while (currentChar != ')') { hexString += currentChar; currentChar = br.ReadChar(); ++pos; } long b = long.Parse(hexString, System.Globalization.NumberStyles.HexNumber); Tuple t = new Tuple(); t.a = a; t.b = b; tuples.Add(t); } return tuples; }