public void RunTest() { // Done by sender //BigInteger p = 2357; BigInteger p = this.GenerateP(32); //BigInteger alpha = 2; BigInteger alpha = Helpers.GetRandomBigInteger(1, p - 1); //BigInteger a = 1751; BigInteger a = Helpers.GetRandomBigInteger(1, p - 2); BigInteger alphaToA = Helpers.SquareAndMultiply(alpha, a, p); ElGamalPublicKey publicKey = new ElGamalPublicKey(p, alpha, alphaToA); ElGamalPrivateKey privateKey = new ElGamalPrivateKey(a); // Done by receiver int message = 2035; //BigInteger k = 1520; // Random number 1 <= k <= p-2 BigInteger k = Helpers.GetRandomBigInteger(1, p - 2); // Random number 1 <= k <= p-2 BigInteger gamma = Helpers.SquareAndMultiply(alpha, k, p); BigInteger delta = (message * Helpers.SquareAndMultiply(publicKey.AlphaToA, k, p)) % p; Tuple <BigInteger, BigInteger> cipherText = new Tuple <BigInteger, BigInteger>(gamma, delta); // Done by receiver BigInteger gammaToTheNegativeA = Helpers.SquareAndMultiply(gamma, (p - 1 - a), p); BigInteger plainText = gammaToTheNegativeA * delta % p; Console.WriteLine("plainText: " + message); Console.WriteLine("cipherText: (" + gamma + "," + delta + ")"); Console.WriteLine("plainText: " + plainText); }
public Tuple <BigInteger, BigInteger> Encrypt(string plainText, ElGamalPublicKey publicKey) { byte[] plainTextAsBytes = Encoding.UTF8.GetBytes(plainText); BigInteger plainTextAsNumber = new BigInteger(plainTextAsBytes); if (plainTextAsNumber > publicKey.P) { throw new Exception("The Master Key cannot exceed n"); } BigInteger k = Helpers.GetRandomBigInteger(1, publicKey.P - 2); BigInteger gamma = Helpers.SquareAndMultiply(publicKey.Alpha, k, publicKey.P); BigInteger delta = (plainTextAsNumber * Helpers.SquareAndMultiply(publicKey.AlphaToA, k, publicKey.P)) % publicKey.P; Tuple <BigInteger, BigInteger> cipherText = new Tuple <BigInteger, BigInteger>(gamma, delta); return(cipherText); }
public List <Tuple <BigInteger, BigInteger> > EncryptWithChunking(string plainText, ElGamalPublicKey publicKey) { List <string> plainTextChunks = new List <string>(); chunkIfRequired(plainText, publicKey.P, plainTextChunks); List <Tuple <BigInteger, BigInteger> > encryptedChunks = new List <Tuple <BigInteger, BigInteger> >(); BigInteger k = Helpers.GetRandomBigInteger(1, publicKey.P - 2); BigInteger gamma = Helpers.SquareAndMultiply(publicKey.Alpha, k, publicKey.P); foreach (string chunk in plainTextChunks) { byte[] chunkAsBytes = Encoding.UTF8.GetBytes(chunk); BigInteger chunkAsNumber = new BigInteger(chunkAsBytes); BigInteger delta = (chunkAsNumber * Helpers.SquareAndMultiply(publicKey.AlphaToA, k, publicKey.P)) % publicKey.P; Tuple <BigInteger, BigInteger> encryptedChunk = new Tuple <BigInteger, BigInteger>(gamma, delta); encryptedChunks.Add(encryptedChunk); } return(encryptedChunks); }