public void SetUp()
        {
            var pkg = new PublicKeyGenerator();
            _alice = pkg.MakeKeyPair();
            _bob = pkg.MakeKeyPair();

            //encrypting from bob -> alice
            _forEncryption = new KeyPair(_alice.Public, _bob.Private);
            _forDecryption = new KeyPair(_bob.Public, _alice.Private);
        }
示例#2
0
        public KeyPair MakeKeyPair()
        {
            RSA rsa = GetRsa();

            var objPublicKey = new PublicKey(rsa.ToXmlString(false));
            var objPrivateKey = new PrivateKey(rsa.ToXmlString(true));
            var keyPair = new KeyPair(objPublicKey, objPrivateKey);

            return keyPair;
        }
示例#3
0
        public bool Authenticate(KeyPair authenticationKeyPair, string signedText)
        {
            _rsaCryptoServiceProvider.FromXmlString(authenticationKeyPair.Public.Key);
            string signature = CryptoHelpers.ExtractSignature(signedText);
            string message = CryptoHelpers.StripSignature(signedText);

            if (string.IsNullOrEmpty(signature))
            {
                throw new Exception("Digital signature is missing or not formatted properly.");
            }

            var bytes = TextHelpers.ClearTextToClearBytes(message);
            var sigbytes = TextHelpers.CipherTextToCipherBytes(signature);

            return _rsaCryptoServiceProvider.VerifyData(bytes, HashAlgorithm.Create(), sigbytes);
        }
示例#4
0
        public string Decrypt(KeyPair decryptionKeyPair, string cipherText)
        {
            //they use THEIR private key to decrypt
            _rsaCryptoServiceProvider.FromXmlString(decryptionKeyPair.Private.Key);

            var blockSize = GetModulusSize();
            var plainStream = new MemoryStream();
            var cipherStream = new MemoryStream(TextHelpers.CipherTextToCipherBytes(cipherText));
            var buffer = new byte[blockSize];

            int r;
            while((r=cipherStream.Read(buffer, 0, buffer.Length)) > 0)
            {
                var p = _rsaCryptoServiceProvider.Decrypt(buffer, false);
                plainStream.Write(p, 0, p.Length);
            }
            //TODO: getting extra data here. not sure why
            var clearBytes = plainStream.ToArray();
            return TextHelpers.ClearBytesToClearString(clearBytes);
        }
示例#5
0
        public void Bob()
        {
            var aliceKeyPair = new PublicKeyGenerator().MakeKeyPair();
            var bobKeyPair = new PublicKeyGenerator().MakeKeyPair();
            var crypto = new RsaCryptographyService();

            var message = "ch";

            //message from bob to alice - encrypt with alice public key - sign with bob's private key
            var keyPair = new KeyPair(aliceKeyPair.Public, bobKeyPair.Private);

            var cipherText = crypto.SignAndEncrypt(keyPair, message);
            Console.WriteLine(cipherText);

            //message received - decrypt with alice private - verify with bob's public key
            var k2 = new KeyPair(bobKeyPair.Public, aliceKeyPair.Private);
            var clear = crypto.DecryptAndAuthenticate(k2, cipherText);
            Console.WriteLine(clear);
        }
示例#6
0
 // We use Direct Encryption (PKCS#1 v1.5) - so we require MS Windows 2000 or later with high encryption pack installed.
 public string SignAndEncrypt(KeyPair encryptionKeyPair, string plainText)
 {
     // Use Public Key to encrypt and private key to sign
     var signed = Sign(encryptionKeyPair, plainText);
     var enc = Encrypt(encryptionKeyPair, signed);
     return enc;
 }
示例#7
0
 public string Sign(KeyPair signingKeyPair, string text)
 {
     //Use PrivateKey to sign
     _rsaCryptoServiceProvider.FromXmlString(signingKeyPair.Private.Key);
     var signedData = _rsaCryptoServiceProvider.SignData(TextHelpers.ClearTextToClearBytes(text), HashAlgorithm.Create());
     var signature = TextHelpers.CipherBytesToCipherText(signedData);
     return string.Format("{0}<signature>{1}</signature>", text, signature);
 }
示例#8
0
        public string Encrypt(KeyPair encryptionKeyPair, string plainText)
        {
            //use THEIR public key to encrypt
            _rsaCryptoServiceProvider.FromXmlString(encryptionKeyPair.Public.Key);

            //Get Modulus Size and compare it to length of PlainText
            // If Length of PlainText > (Modulus Size - 11), then PlainText will need to be broken into segments of size (Modulus Size - 11)
            // Each of these segments will be encrypted separately
            //     and will return encrypted strings equal to the Modulus Size (with at least 11 bytes of padding)
            // When decrypting, if the EncryptedText string > Modulus size, it will be split into segments of size equal to Modulus Size
            // Each of these EncryptedText segments will be decrypted individually with the resulting PlainText segments re-assembled.

            var blockSize = GetModulusSize() - 11;
            var plainStream = new MemoryStream(TextHelpers.ClearTextToClearBytes(plainText));
            var cipherStream = new MemoryStream();
            var buffer = new byte[blockSize];

            while(plainStream.Read(buffer, 0, blockSize) > 0)
            {
                var c = _rsaCryptoServiceProvider.Encrypt(buffer, false);
                cipherStream.Write(c, 0, c.Length);
            }

            var cipherBytes = cipherStream.ToArray();
            return TextHelpers.CipherBytesToCipherText(cipherBytes);
        }
示例#9
0
        public string DecryptAndAuthenticate(KeyPair decryptionKeyPair, string cipherText)
        {
            //Use Private key to Decrypt and Public Key to Authenticate

            var plainText = Decrypt(decryptionKeyPair, cipherText);
            if (!Authenticate(decryptionKeyPair, plainText))
            {
                throw new Exception("Message authentication failed.");
            }

            return CryptoHelpers.StripSignature(plainText);
        }