public string Encrypt(string plainText, RSAPublicKey publicKey) { byte[] plainTextAsBytes = Encoding.UTF8.GetBytes(plainText); BigInteger plainTextAsNumber = new BigInteger(plainTextAsBytes); if (plainTextAsNumber > publicKey.N) { // Chunking is not suported since it was implemented already in Assignment 3. throw new Exception("The Master Key cannot exceed n"); } BigInteger cipherTextAsBigInteger = Helpers.SquareAndMultiply(plainTextAsNumber, publicKey.E, publicKey.N); byte[] cipherTextAsBytes = cipherTextAsBigInteger.ToByteArray(); string cipherText = Convert.ToBase64String(cipherTextAsBytes); return(cipherText); }
public void RunTest() { int desiredBitSize = 32; BigInteger p; BigInteger q; BigInteger n = GenerateN(desiredBitSize, out p, out q); BigInteger phi = (p - 1) * (q - 1); BigInteger e = GenerateE(phi); BigInteger d = Helpers.MultiplicativeInverse(e, phi); RSAPublicKey publicKey = new RSAPublicKey(n, e); // PUBLIC KEY RECEIVED. ENCRYPT SOMETHING. int message = 4; BigInteger cipherText = Helpers.SquareAndMultiply(message, e, n); Console.WriteLine("cipherText: " + cipherText); BigInteger plainText = Helpers.SquareAndMultiply(cipherText, d, n); Console.WriteLine("plainText: " + plainText); }
public BigInteger Encrypt(BigInteger plainTextAsBigInteger, RSAPublicKey publicKey) { BigInteger cipherTextAsBigInteger = Helpers.SquareAndMultiply(plainTextAsBigInteger, publicKey.E, publicKey.N); return(cipherTextAsBigInteger); }