Esempio n. 1
0
        public byte[] Decrypt(byte[] message)
        {
            RSAKeys.Key privateKey = RSAKeys.PrivateKey;
            var         size       = privateKey.n.ToByteArray().Length;
            List <byte> decrypted  = new List <byte>(size * message.Length);

            for (var i = 0; i <= message.Length - size; i += size)
            {
                BigInteger encryptedChar = new BigInteger(message.Skip(i).Take(size).ToArray());
                decrypted.AddRange(BigInteger.ModPow(encryptedChar, privateKey.exponent, privateKey.n).ToByteArray());
            }
            return(decrypted.ToArray());
        }
Esempio n. 2
0
        public byte[] Encrypt(byte[] message)
        {
            RSAKeys.Key publicKey = RSAKeys.PublicKey;
            var         size      = publicKey.n.ToByteArray().Length;
            List <byte> encrypted = new List <byte>(size * message.Length);

            foreach (var ch in message)
            {
                byte[] encryptedCharBytes = BigInteger.ModPow(ch, publicKey.exponent, publicKey.n).ToByteArray();
                encrypted.AddRange(encryptedCharBytes);
                encrypted.AddRange(new byte[size - encryptedCharBytes.Length]);
            }
            return(encrypted.ToArray());
        }