public string GetSecretKey(byte[] envelope)
        {
            RSAPKCS1KeyExchangeDeformatter kedfm = new RSAPKCS1KeyExchangeDeformatter(_alg);

            Byte[] secretkeybyte = kedfm.DecryptKeyExchange(envelope);
            return(new UnicodeEncoding().GetString(secretkeybyte));
        }
        public static byte[] decrypt(byte[] dataBytes)
        {
            if (!isInit)
            {
                RSA();
                isInit = true;
            }

            // by default this will create a 128 bits AES (Rijndael) object
            SymmetricAlgorithm sa = SymmetricAlgorithm.Create();

            byte[] keyex = new byte[rsa.KeySize >> 3];
            Buffer.BlockCopy(dataBytes, 0, keyex, 0, keyex.Length);

            RSAPKCS1KeyExchangeDeformatter def = new RSAPKCS1KeyExchangeDeformatter(rsa);

            byte[] key = def.DecryptKeyExchange(keyex);

            byte[] iv = new byte[sa.IV.Length];
            Buffer.BlockCopy(dataBytes, keyex.Length, iv, 0, iv.Length);

            ICryptoTransform ct = sa.CreateDecryptor(key, iv);

            byte[] decrypt = ct.TransformFinalBlock(dataBytes, keyex.Length + iv.Length, dataBytes.Length - (keyex.Length + iv.Length));
            return(decrypt);
        }
        protected override void ProcessAsTls1()
        {
            AsymmetricAlgorithm privKey = null;
            ServerContext       context = (ServerContext)this.Context;

            // Select the private key information
            // Select the private key information
            privKey = context.SslStream.RaisePrivateKeySelection(
                new X509Certificate(context.ServerSettings.Certificates[0].RawData),
                null);

            if (privKey == null)
            {
                throw new TlsException(AlertDescription.UserCancelled, "Server certificate Private Key unavailable.");
            }

            // Read client premaster secret
            byte[] clientSecret = this.ReadBytes(this.ReadInt16());

            // Decrypt premaster secret
            RSAPKCS1KeyExchangeDeformatter deformatter = new RSAPKCS1KeyExchangeDeformatter(privKey);

            byte[] preMasterSecret = deformatter.DecryptKeyExchange(clientSecret);

            // Create master secret
            this.Context.Negotiating.Cipher.ComputeMasterSecret(preMasterSecret);

            // Create keys
            this.Context.Negotiating.Cipher.ComputeKeys();

            // Initialize Cipher Suite
            this.Context.Negotiating.Cipher.InitializeCipher();
        }
        public void KeyExchangeTooBig()
        {
            AsymmetricKeyExchangeDeformatter keyex = new RSAPKCS1KeyExchangeDeformatter(key);

            byte[] EM = new byte [(key.KeySize >> 3) + 1];
            // invalid format
            byte[] M = keyex.DecryptKeyExchange(EM);
        }
Esempio n. 5
0
        /// <summary>秘密鍵生成</summary>
        /// <param name="exchangeKeyOfAlice">Aliceの交換鍵</param>
        /// <param name="iv">初期化ベクター</param>
        public void GeneratePrivateKey(byte[] exchangeKeyOfAlice, byte[] iv)
        {
            this._aes = new AesCryptoServiceProvider();
            RSAPKCS1KeyExchangeDeformatter keyExchangeDeformatter = new RSAPKCS1KeyExchangeDeformatter(this._asa);

            this._aes.Key = keyExchangeDeformatter.DecryptKeyExchange(exchangeKeyOfAlice);
            this._aes.IV  = iv;
        }
        public void CapiKeyExchange128()
        {
            byte[] M  = { 0xd4, 0x36, 0xe9, 0x95, 0x69, 0xfd, 0x32, 0xa7, 0xc8, 0xa0, 0x5b, 0xbc, 0x90, 0xd3, 0x2c, 0x49 };
            byte[] EM = { 0x2D, 0xA3, 0xB0, 0xED, 0x1F, 0x13, 0x13, 0xBA, 0xAA, 0x26, 0xA7, 0x00, 0x76, 0x94, 0x0A, 0xDA, 0xFB, 0x4E, 0x14, 0x98, 0xD3, 0xF6, 0x26, 0x65, 0xCE, 0x7E, 0xB9, 0x23, 0xEF, 0xDE, 0x6E, 0xAB, 0x72, 0x33, 0xF3, 0x6F, 0xA9, 0x9B, 0xEC, 0x18, 0xC9, 0xB7, 0xC7, 0xE8, 0xE8, 0x55, 0xC4, 0x83, 0x1E, 0xF5, 0xDA, 0xCF, 0x5A, 0x53, 0xB0, 0x60, 0x42, 0xF4, 0x55, 0xEE, 0x00, 0x80, 0x92, 0x28, 0xA9, 0x0E, 0x2D, 0x9D, 0x49, 0x10, 0x65, 0x00, 0x21, 0x82, 0xCC, 0x05, 0xA3, 0x62, 0xAD, 0xCC, 0x5B, 0xE3, 0x8E, 0xAE, 0x01, 0x96, 0x81, 0xF6, 0x7B, 0x52, 0xB9, 0x6F, 0xE3, 0x06, 0x3A, 0x48, 0x4D, 0x87, 0xB9, 0xA3, 0xEA, 0x69, 0xD1, 0xFE, 0x8D, 0x82, 0x33, 0xE3, 0x05, 0xEB, 0x00, 0xA2, 0xA6, 0xDC, 0x95, 0xE4, 0xAC, 0x4E, 0xF4, 0x03, 0xC3, 0xFE, 0xA2, 0xE8, 0xB6, 0xBB, 0xBE, 0xD1 };

            AsymmetricKeyExchangeDeformatter keyback = new RSAPKCS1KeyExchangeDeformatter(key);

            byte[] Mback = keyback.DecryptKeyExchange(EM);
            AssertEquals("RSAPKCS1KeyExchangeFormatter 1", M, Mback);
        }
        public void MonoKeyExchangeMin()
        {
            byte[] M  = { 0x01 };
            byte[] EM = { 0x73, 0x34, 0xAF, 0xE5, 0x45, 0x53, 0x4A, 0x93, 0x25, 0x77, 0x6F, 0x80, 0x06, 0xAD, 0x7C, 0x87, 0xB9, 0xE8, 0x1E, 0x5C, 0xBB, 0x9B, 0x3F, 0xDC, 0x9C, 0x65, 0x71, 0xE6, 0x50, 0x82, 0xDC, 0x77, 0x6C, 0x6B, 0xA6, 0x39, 0x18, 0x0B, 0x33, 0x54, 0x4E, 0x65, 0x32, 0x6C, 0x53, 0x70, 0x9B, 0xEA, 0x7C, 0x83, 0x0D, 0xBF, 0x8B, 0x48, 0x5B, 0x0F, 0xCB, 0x27, 0x7D, 0x8D, 0x18, 0xD7, 0xA5, 0x13, 0x33, 0x3C, 0xC8, 0xB0, 0xF4, 0x12, 0x52, 0x24, 0x3C, 0x2A, 0xD2, 0xDF, 0x7C, 0x0B, 0xCB, 0x7C, 0x26, 0x28, 0x5F, 0x88, 0x1E, 0x22, 0x98, 0x68, 0x04, 0x12, 0x6E, 0x9F, 0x2D, 0xFE, 0x7A, 0xEF, 0xC3, 0x9D, 0x87, 0x44, 0x46, 0xCA, 0xA2, 0x81, 0xF2, 0xE7, 0xBA, 0x9D, 0x17, 0x68, 0x96, 0xA2, 0x3F, 0xB3, 0xB4, 0x43, 0x34, 0x2D, 0x7D, 0x56, 0xF5, 0xFC, 0x40, 0xEB, 0x31, 0xB0, 0x0C, 0x99 };

            AsymmetricKeyExchangeDeformatter keyback = new RSAPKCS1KeyExchangeDeformatter(key);

            byte[] Mback = keyback.DecryptKeyExchange(EM);
            AssertEquals("RSAPKCS1KeyExchangeDeformatter 1", M, Mback);
        }
        public void MonoKeyExchange160()
        {
            byte[] M  = { 0xd4, 0x36, 0xe9, 0x95, 0x69, 0xfd, 0x32, 0xa7, 0xc8, 0xa0, 0x5b, 0xbc, 0x90, 0xd3, 0x2c, 0x49, 0x00, 0x00, 0x00, 0x00 };
            byte[] EM = { 0x31, 0x2B, 0x21, 0x0F, 0x1D, 0x75, 0xCE, 0xDF, 0x00, 0xC4, 0xC2, 0x50, 0x59, 0x13, 0xDA, 0xF4, 0xE4, 0x73, 0xD3, 0x26, 0xC7, 0xBD, 0xAF, 0xDC, 0x73, 0xB1, 0xC0, 0x32, 0xE3, 0xE9, 0x91, 0x4C, 0x1F, 0x74, 0x29, 0x8C, 0xD6, 0xFD, 0x4C, 0x8C, 0xD2, 0x30, 0xED, 0xEF, 0x97, 0xF1, 0x91, 0xFF, 0xD8, 0x3D, 0x04, 0xD2, 0x2D, 0xB7, 0x20, 0x25, 0x1D, 0x47, 0xBA, 0xEA, 0x3D, 0xE2, 0x7D, 0x9C, 0x41, 0x0C, 0x5C, 0x63, 0xBC, 0xB7, 0xFA, 0xDD, 0x30, 0x19, 0x3E, 0xD2, 0x5F, 0x1B, 0xBC, 0x59, 0x0A, 0x54, 0x0A, 0xE0, 0x82, 0x5D, 0x05, 0xA4, 0xDC, 0x23, 0x71, 0x33, 0x84, 0x68, 0xDA, 0x8C, 0x7A, 0x23, 0x2E, 0x16, 0x28, 0x3E, 0x43, 0x24, 0x30, 0x69, 0xD4, 0x43, 0x7F, 0x82, 0xA8, 0xAC, 0xFF, 0xCC, 0xA6, 0x62, 0x20, 0x61, 0x5F, 0x03, 0xEE, 0x7C, 0x9E, 0x5C, 0xB2, 0xA0, 0xE4, 0xC6 };

            AsymmetricKeyExchangeDeformatter keyback = new RSAPKCS1KeyExchangeDeformatter(key);

            byte[] Mback = keyback.DecryptKeyExchange(EM);
            AssertEquals("RSAPKCS1KeyExchangeFormatter 1", M, Mback);
        }
        public void MonoKeyExchangeMax()
        {
            byte[] M  = new byte [(key.KeySize >> 3) - 11];
            byte[] EM = { 0xB4, 0x17, 0xE4, 0x8A, 0x14, 0xB1, 0x9B, 0x08, 0xBE, 0xBF, 0xD3, 0xD1, 0xCD, 0xE5, 0xB1, 0x0D, 0x38, 0x08, 0x01, 0x31, 0x10, 0xDA, 0x8A, 0xB9, 0xE9, 0x4E, 0x2F, 0x94, 0x2F, 0x40, 0x36, 0x04, 0x57, 0x54, 0xAC, 0x22, 0xC1, 0x6B, 0x35, 0x10, 0xF9, 0xA9, 0xEA, 0x36, 0xC9, 0x13, 0x84, 0x95, 0xCB, 0xDE, 0x9C, 0x01, 0x66, 0x32, 0x01, 0xA1, 0xB2, 0xDB, 0x4F, 0x11, 0x10, 0x2D, 0x13, 0x36, 0x52, 0x30, 0x78, 0x65, 0x00, 0x7A, 0xD8, 0x5B, 0x47, 0xA6, 0x19, 0x9C, 0xFA, 0x76, 0x1A, 0x44, 0x92, 0x3E, 0xE3, 0x5A, 0x0B, 0x56, 0x4D, 0x2D, 0x54, 0x7B, 0x07, 0x5C, 0xA7, 0x14, 0x86, 0x52, 0x0A, 0x8F, 0x11, 0xE2, 0x32, 0xED, 0x3C, 0x21, 0xF8, 0x56, 0x0D, 0x38, 0xAC, 0x24, 0x4A, 0x32, 0xB3, 0x4F, 0xA3, 0xB1, 0x02, 0xC7, 0x8A, 0x22, 0xE6, 0x9C, 0x78, 0xEB, 0x98, 0x4B, 0x92, 0x24 };

            AsymmetricKeyExchangeDeformatter keyback = new RSAPKCS1KeyExchangeDeformatter(key);

            byte[] Mback = keyback.DecryptKeyExchange(EM);
            AssertEquals("RSAPKCS1KeyExchangeFormatter 1", M, Mback);
        }
        public void CapiKeyExchange160()
        {
            byte[] M  = { 0xd4, 0x36, 0xe9, 0x95, 0x69, 0xfd, 0x32, 0xa7, 0xc8, 0xa0, 0x5b, 0xbc, 0x90, 0xd3, 0x2c, 0x49, 0x00, 0x00, 0x00, 0x00 };
            byte[] EM = { 0x10, 0x79, 0x3A, 0x88, 0x04, 0x4B, 0xA5, 0x18, 0xD6, 0xCE, 0x97, 0x9B, 0xFF, 0xE8, 0xB4, 0xF5, 0x8D, 0x60, 0x07, 0xCD, 0x5F, 0x89, 0xA6, 0xCF, 0x5B, 0x90, 0x96, 0xC7, 0xF6, 0xD7, 0xF2, 0xCA, 0x7C, 0x13, 0x5A, 0x62, 0xB4, 0xED, 0xF4, 0xD7, 0x5C, 0x99, 0x4C, 0x07, 0xF4, 0x9F, 0x96, 0xE6, 0xBF, 0x2B, 0x82, 0x85, 0x38, 0x2C, 0x03, 0xBD, 0x61, 0x07, 0xF6, 0x05, 0x15, 0x55, 0xBF, 0xA9, 0x3B, 0xF5, 0x10, 0x96, 0x81, 0x01, 0x58, 0x5F, 0x61, 0x43, 0x52, 0x77, 0x71, 0x9C, 0x92, 0xEF, 0xD5, 0xE2, 0x60, 0x3F, 0x82, 0x69, 0x9F, 0xAF, 0xC2, 0xE1, 0x68, 0xB7, 0x5E, 0x62, 0xAC, 0x61, 0x6A, 0x1B, 0x46, 0x03, 0xF6, 0x7C, 0x20, 0x47, 0xF7, 0x6E, 0x7D, 0x35, 0x2A, 0xF6, 0x9C, 0xDA, 0x8A, 0xED, 0xAC, 0x1A, 0xC8, 0xF6, 0x4E, 0x7D, 0x21, 0xAC, 0x18, 0xEB, 0xA7, 0x68, 0xE0, 0xE2 };

            AsymmetricKeyExchangeDeformatter keyback = new RSAPKCS1KeyExchangeDeformatter(key);

            byte[] Mback = keyback.DecryptKeyExchange(EM);
            AssertEquals("RSAPKCS1KeyExchangeFormatter 1", M, Mback);
        }
        public void MonoKeyExchange128()
        {
            byte[] M  = { 0xd4, 0x36, 0xe9, 0x95, 0x69, 0xfd, 0x32, 0xa7, 0xc8, 0xa0, 0x5b, 0xbc, 0x90, 0xd3, 0x2c, 0x49 };
            byte[] EM = { 0xAA, 0x95, 0x6D, 0x40, 0xA7, 0x26, 0x23, 0x4E, 0xA9, 0xCB, 0x83, 0x55, 0xCE, 0x2F, 0xDD, 0x80, 0xEA, 0xC8, 0x61, 0x25, 0x57, 0xF9, 0x86, 0x46, 0x2E, 0xD9, 0xAD, 0xA1, 0x90, 0x22, 0x6A, 0x1F, 0xCF, 0x24, 0x9D, 0x3A, 0x65, 0x75, 0xF6, 0x9E, 0xBD, 0xC0, 0xBB, 0x8F, 0xC0, 0xC3, 0x20, 0x45, 0xC9, 0x8C, 0x5F, 0xEA, 0xF9, 0xE3, 0x1E, 0x95, 0xA0, 0xAD, 0xD6, 0xB6, 0x3C, 0x9B, 0x03, 0x9F, 0xB0, 0x57, 0x32, 0x2F, 0x98, 0x0E, 0x94, 0x8C, 0x6E, 0xA7, 0x9F, 0x40, 0xCF, 0xAD, 0x6E, 0xDB, 0x38, 0x9F, 0xF5, 0x43, 0xD1, 0x70, 0xF9, 0xCA, 0x3A, 0x2E, 0x0B, 0xB9, 0x34, 0x12, 0x0F, 0x09, 0x5B, 0x6B, 0xB9, 0xFD, 0x7E, 0xC6, 0xFC, 0xA1, 0x9A, 0x48, 0xEA, 0x3A, 0xED, 0x77, 0x24, 0xA5, 0x3B, 0x8B, 0xFB, 0xF1, 0x2B, 0x9D, 0xED, 0x0A, 0xB5, 0x05, 0xDC, 0x59, 0xA8, 0x1F, 0x17, 0xC9 };

            AsymmetricKeyExchangeDeformatter keyback = new RSAPKCS1KeyExchangeDeformatter(key);

            byte[] Mback = keyback.DecryptKeyExchange(EM);
            AssertEquals("RSAPKCS1KeyExchangeFormatter 1", M, Mback);
        }
        public void CapiKeyExchangeMin()
        {
            byte[] M  = { 0x01 };
            byte[] EM = { 0x50, 0x33, 0xF3, 0x42, 0x52, 0x59, 0x71, 0x2D, 0x6E, 0x25, 0x5E, 0x06, 0xC3, 0x27, 0x94, 0xA6, 0xD1, 0x8E, 0x13, 0x90, 0x54, 0x5C, 0x12, 0x58, 0x7A, 0xC9, 0xB6, 0x3F, 0x4D, 0x2E, 0x97, 0xCC, 0x3A, 0x94, 0x24, 0xE8, 0x11, 0x1F, 0xD6, 0x7F, 0x37, 0x36, 0xAB, 0x6F, 0x3F, 0xB4, 0x1B, 0xB8, 0x13, 0x87, 0xC8, 0xBE, 0x00, 0x24, 0x02, 0x0F, 0xF6, 0x2E, 0xEA, 0x48, 0x8A, 0x6F, 0xC8, 0xF6, 0x0B, 0xAB, 0xF4, 0x02, 0xA5, 0xE2, 0x5A, 0xAA, 0xB5, 0x9E, 0xC2, 0x6E, 0xFF, 0xA6, 0xEC, 0xEC, 0xD0, 0xA2, 0x3F, 0x00, 0x93, 0xE9, 0xF3, 0xAA, 0x08, 0xA2, 0xD2, 0x11, 0x1B, 0x3F, 0x3E, 0x59, 0xB0, 0xBA, 0x47, 0x17, 0x8F, 0xF4, 0xEB, 0x34, 0xA5, 0xC4, 0xA4, 0x09, 0x43, 0xC4, 0x7B, 0x71, 0x2C, 0x4B, 0x9E, 0x2D, 0x22, 0x96, 0xBB, 0x52, 0xDD, 0x2B, 0x59, 0xED, 0xD6, 0xCA, 0xEB, 0xE6 };

            AsymmetricKeyExchangeDeformatter keyback = new RSAPKCS1KeyExchangeDeformatter(key);

            byte[] Mback = keyback.DecryptKeyExchange(EM);
            AssertEquals("RSAPKCS1KeyExchangeDeformatter 1", M, Mback);
        }
        public void CapiKeyExchangeMax()
        {
            byte[] M  = new byte [(key.KeySize >> 3) - 11];
            byte[] EM = { 0x4B, 0x3F, 0x77, 0xE1, 0xA0, 0x6C, 0xD9, 0xFA, 0x19, 0x69, 0x21, 0xC4, 0x67, 0x2B, 0x0F, 0x2A, 0x0E, 0xCB, 0xAF, 0xAD, 0x08, 0xA5, 0xD2, 0x9B, 0xDC, 0x04, 0xDE, 0x8F, 0x13, 0xE4, 0x81, 0x25, 0xAF, 0xC5, 0x82, 0x51, 0xA9, 0x39, 0xAF, 0x82, 0xFF, 0xC7, 0x4F, 0x04, 0xE4, 0x21, 0xAC, 0xEE, 0x2F, 0x44, 0x78, 0x11, 0x29, 0x74, 0x3F, 0x74, 0xC1, 0x38, 0xC5, 0x43, 0x29, 0x2F, 0x0C, 0x7B, 0xDB, 0x2E, 0xE5, 0xA8, 0x6A, 0xEE, 0x6A, 0x14, 0xCC, 0x4E, 0x53, 0x8C, 0x0C, 0xEE, 0x23, 0x24, 0xDC, 0x9B, 0x75, 0x7C, 0xAD, 0x0C, 0xAC, 0x13, 0xC5, 0x02, 0x9E, 0x5D, 0x65, 0x76, 0xCB, 0xD4, 0xBF, 0x70, 0x43, 0xBE, 0x28, 0x67, 0x3F, 0x5D, 0x93, 0x38, 0x67, 0x4B, 0x25, 0x59, 0xF7, 0x8E, 0x4F, 0xCE, 0x2B, 0x2F, 0xA7, 0x4C, 0x68, 0x4C, 0xCC, 0x5F, 0xF3, 0x0A, 0xB7, 0xAA, 0x54, 0x7C };

            AsymmetricKeyExchangeDeformatter keyback = new RSAPKCS1KeyExchangeDeformatter(key);

            byte[] Mback = keyback.DecryptKeyExchange(EM);
            AssertEquals("RSAPKCS1KeyExchangeFormatter 1", M, Mback);
        }
        public void Bug79320()
        {
            string s = "hdphq/mn8goBi43YGPkmOfPj5vXjHrKPJkT4mLT3l+XzLttHMLC4/yBYkuzlXtbrl2jtAJRb6oA8UcQFalUMnCa09LDZrgNU2yySn7YbiG8raSq7u2nfDCbPu+c8T9fyHxrCHrX0zeqqImX33csIn6rIrQZ8HKcMsoQso4qtS2A=";

            byte [] bytes = Convert.FromBase64String(s);
            RSACryptoServiceProvider r = new RSACryptoServiceProvider();

            r.FromXmlString("<RSAKeyValue><Modulus>iSObDmmhDgrl4NiLaviFcpv4NdysBWJcqiVz3AQbPdajtXaQQ8VJdfRkixah132yKOFGCWZhHS3EuPMh8dcNwGwta2nh+m2IV6ktzI4+mZ7CSNAsmlDY0JI+H8At1vKvNArlC5jkVGuliYroJeSU/NLPLNYgspi7TtXGy9Rfug8=</Modulus><Exponent>EQ==</Exponent><P>pd4svtxrnTWXVSb151/JFgT9szI6dxQ5pAFPd4A4yuxLLEay2W2z7d9LVk5siMFhZ10uTJGWzNP5pSgLT8wdww==</P><Q>06j6m4cGRc3uoKVuFFGA19JG3Bi4tDBEQHebEc/Y3+eThrOasYIRrQmGUfqWnd9eFitO9GOaVJ0muNDV7NOxxQ==</Q><DP>OoqmYXr4zhLqHg3AM4s36adomZlBz6zJDLUrGx4yKYCTAJFsTL1OkDCxLYUXP1NPjeSm7dkIDA6UWGh8doRGvQ==</DP><DQ>PkDCLb5NI5br1OVcnJBxMGsFyEOBnmiMi2545x8DjSX+Nq1LnZ6555ljvcIsTIz9jgy83nel3KaxCS5dCWtwhQ==</DQ><InverseQ>OrFYaG7wTqim/bub4qY0CvIfhsjG4/4MEabg0UFTf/+tekKas7DDKg2TD5BS2q0O3XEt7xIfp0S6dpOAnrlyGQ==</InverseQ><D>IESc9FUW1iCuj0ICr8IBSCSy3383iMvZkXI5YPHoSskXdf3Hl3m27pPbbAVTQcM4+o9bxfn4u5JMZ8C8sV/G/8Cfl4ss1NVMbZOecvVObRqRpqXaveq5fN2X0EklH1wzm5w3O8cMXdbC/hc0gKUqaMjFVn1zpf3zVjpOkY0eGRE=</D></RSAKeyValue>");
            RSAPKCS1KeyExchangeDeformatter def = new RSAPKCS1KeyExchangeDeformatter(r);

            Assert.IsNotNull(def.DecryptKeyExchange(bytes));
        }
        public void KeyExchange128bits()
        {
            AsymmetricKeyExchangeFormatter keyex = new RSAPKCS1KeyExchangeFormatter(key);

            byte[] M  = { 0xd4, 0x36, 0xe9, 0x95, 0x69, 0xfd, 0x32, 0xa7, 0xc8, 0xa0, 0x5b, 0xbc, 0x90, 0xd3, 0x2c, 0x49 };
            byte[] EM = keyex.CreateKeyExchange(M, typeof(Rijndael));

            AsymmetricKeyExchangeDeformatter keyback = new RSAPKCS1KeyExchangeDeformatter(key);

            byte[] Mback = keyback.DecryptKeyExchange(EM);
            AssertEquals("RSAPKCS1KeyExchangeFormatter 1", M, Mback);
        }
        public void KeyExchangeMin()
        {
            AsymmetricKeyExchangeFormatter keyex = new RSAPKCS1KeyExchangeFormatter(key);

            byte[] M  = { 0x01 };
            byte[] EM = keyex.CreateKeyExchange(M);

            AsymmetricKeyExchangeDeformatter keyback = new RSAPKCS1KeyExchangeDeformatter(key);

            byte[] Mback = keyback.DecryptKeyExchange(EM);
            AssertEquals("RSAPKCS1KeyExchangeFormatter 1", M, Mback);
        }
        public void KeyExchangeMax()
        {
            AsymmetricKeyExchangeFormatter keyex = new RSAPKCS1KeyExchangeFormatter(key);

            byte[] M  = new byte [(key.KeySize >> 3) - 11];
            byte[] EM = keyex.CreateKeyExchange(M);

            AsymmetricKeyExchangeDeformatter keyback = new RSAPKCS1KeyExchangeDeformatter(key);

            byte[] Mback = keyback.DecryptKeyExchange(EM);
            AssertEquals("RSAPKCS1KeyExchangeFormatter 1", M, Mback);
        }
        static void DecryptStream(Stream inputStream, Stream outputStream, AsymmetricAlgorithm keyEncryption, SymmetricAlgorithm dataEncryption)
        {
            if (null == inputStream)
            {
                throw new ArgumentNullException(nameof(inputStream));
            }
            if (null == outputStream)
            {
                throw new ArgumentNullException(nameof(outputStream));
            }
            if (null == keyEncryption)
            {
                throw new ArgumentNullException(nameof(keyEncryption));
            }
            if (null == dataEncryption)
            {
                throw new ArgumentNullException(nameof(dataEncryption));
            }

            var encryptedDEK = inputStream.ReadLengthAndBytes(maxBytes: 2048);
            var encryptedIV  = inputStream.ReadLengthAndBytes(maxBytes: 2048);

            var keyDeformatter = new RSAPKCS1KeyExchangeDeformatter(keyEncryption);

            dataEncryption.Key = keyDeformatter.DecryptKeyExchange(encryptedDEK);
            dataEncryption.IV  = keyDeformatter.DecryptKeyExchange(encryptedIV);

            // About...
            // Trace.WriteLine($"Decrypting. KEK: {keyEncryption.GetType().Name} / {keyEncryption.KeySize} bits");
            // Trace.WriteLine($"Decrypting. DEK: {dataEncryption.GetType().Name} / {dataEncryption.KeySize} bits / BlockSize: {dataEncryption.BlockSize} bits");

            // Read the encrypted data.
            // Note: Disposing the CryptoStream also disposes the inputStream. There is no keepOpen option.
            using (var transform = dataEncryption.CreateDecryptor())
                using (var cryptoStream = new CryptoStream(inputStream, transform, CryptoStreamMode.Read))
                {
                    cryptoStream.CopyTo(outputStream, bufferSize: dataEncryption.BlockSize * 4);
                }
        }
Esempio n. 19
0
        /// <summary>
        /// Decrypts the target key with the CryptoManager's private RSA key.  It is assumbed that the
        /// @rijKey was previously encrypted with the CryptoManager's public key.
        /// </summary>
        /// <param name="rijKey"></param>
        /// <returns></returns>
        public byte[] DecryptRijndaelKey(byte[] rijKey)
        {
            byte[] key = new byte[0];
            lock (this)
            {
                //m_RSA.ImportCspBlob(m_PrivateKey);
                m_RSA.FromXmlString(Encoding.UTF8.GetString(m_PrivateKey));
                m_RSA_Exch_Def.SetKey(m_RSA);
                key = m_RSA_Exch_Def.DecryptKeyExchange(rijKey);
            }

            return(key);
        }
Esempio n. 20
0
 public static void TestKnownValuePkcs1()
 {
     using (RSA rsa = RSAFactory.Create())
     {
         rsa.ImportParameters(TestData.RSA1024Params);
         byte[] encrypted =
             ("7061adb87a8759f0a0dc6ece42f5b63bf186f845237c6b16bf824b303812486efbb8f5febb681902228a609d4330a6c21abf0fc0d271"
              + "ba63d1d0d9e486668270c2dbf73ab33055dfc0b797938557b99c0e9a535605c0a4bceefe5a37594732bb566ab026e4e8d5ce47d0967d"
              + "f1c66e7ee4d39d804f6d558670222d708f943eb0").HexToByteArray();
         RSAPKCS1KeyExchangeDeformatter deformatter = new RSAPKCS1KeyExchangeDeformatter(rsa);
         byte[] plain         = deformatter.DecryptKeyExchange(encrypted);
         byte[] expectedPlain = { 0x41, 0x42, 0x43 };
         Assert.Equal(expectedPlain, plain);
     }
 }
Esempio n. 21
0
 public static void TestKnownValuePkcs1()
 {
     using (RSA rsa = RSAFactory.Create())
     {
         rsa.ImportParameters(TestData.RSA1024Params);
         byte[] encrypted =
             ( "7061adb87a8759f0a0dc6ece42f5b63bf186f845237c6b16bf824b303812486efbb8f5febb681902228a609d4330a6c21abf0fc0d271"
             + "ba63d1d0d9e486668270c2dbf73ab33055dfc0b797938557b99c0e9a535605c0a4bceefe5a37594732bb566ab026e4e8d5ce47d0967d"
             + "f1c66e7ee4d39d804f6d558670222d708f943eb0").HexToByteArray();
         RSAPKCS1KeyExchangeDeformatter deformatter = new RSAPKCS1KeyExchangeDeformatter(rsa);
         byte[] plain = deformatter.DecryptKeyExchange(encrypted);
         byte[] expectedPlain = { 0x41, 0x42, 0x43 };
         Assert.Equal(expectedPlain, plain);
     }
 }
Esempio n. 22
0
        protected override void ProcessAsTls1()
        {
            ServerContext       serverContext       = (ServerContext)base.Context;
            AsymmetricAlgorithm asymmetricAlgorithm = serverContext.SslStream.RaisePrivateKeySelection(new X509Certificate(serverContext.ServerSettings.Certificates[0].RawData), null);

            if (asymmetricAlgorithm == null)
            {
                throw new TlsException(AlertDescription.UserCancelled, "Server certificate Private Key unavailable.");
            }
            byte[] rgbIn = base.ReadBytes((int)base.ReadInt16());
            RSAPKCS1KeyExchangeDeformatter rsapkcs1KeyExchangeDeformatter = new RSAPKCS1KeyExchangeDeformatter(asymmetricAlgorithm);

            byte[] preMasterSecret = rsapkcs1KeyExchangeDeformatter.DecryptKeyExchange(rgbIn);
            base.Context.Negotiating.Cipher.ComputeMasterSecret(preMasterSecret);
            base.Context.Negotiating.Cipher.ComputeKeys();
            base.Context.Negotiating.Cipher.InitializeCipher();
        }
Esempio n. 23
0
        /// <inheritdoc />
        protected internal override byte[] Decrypt(byte[] data, byte[] iv)
        {
#if NETCOREAPP1_0
            switch (this.algorithm)
            {
            case AsymmetricAlgorithm.RsaPkcs1:
                return(this.Rsa.Decrypt(data, RSAEncryptionPadding.Pkcs1));

            case AsymmetricAlgorithm.RsaOaepSha1:
                return(this.Rsa.Decrypt(data, RSAEncryptionPadding.OaepSHA1));

            case AsymmetricAlgorithm.RsaOaepSha256:
                return(this.Rsa.Decrypt(data, RSAEncryptionPadding.OaepSHA256));

            case AsymmetricAlgorithm.RsaOaepSha384:
                return(this.Rsa.Decrypt(data, RSAEncryptionPadding.OaepSHA384));

            case AsymmetricAlgorithm.RsaOaepSha512:
                return(this.Rsa.Decrypt(data, RSAEncryptionPadding.OaepSHA512));

            default:
                throw new PlatformNotSupportedException();
            }
#else
            AsymmetricKeyExchangeDeformatter keyExchange;
            switch (this.Algorithm)
            {
            case AsymmetricAlgorithm.RsaOaepSha1:
            case AsymmetricAlgorithm.RsaOaepSha256:
            case AsymmetricAlgorithm.RsaOaepSha384:
            case AsymmetricAlgorithm.RsaOaepSha512:
                keyExchange = new RSAOAEPKeyExchangeDeformatter(this.Rsa);
                break;

            case AsymmetricAlgorithm.RsaPkcs1:
                keyExchange = new RSAPKCS1KeyExchangeDeformatter(this.Rsa);
                break;

            default:
                throw new NotSupportedException();
            }

            return(keyExchange.DecryptKeyExchange(data));
#endif
        }
Esempio n. 24
0
        /// <summary>
        /// Decode a message sent from client to server.
        /// </summary>
        /// <param name="content">Message to decode and validate.</param>
        /// <returns>
        /// Decoded message, in plain text.
        /// </returns>
        /// <exception cref="LicenseException">
        /// If message is not valid or it has been tampered.
        /// </exception>
        public static string DecodeMessageFromClient(string content)
        {
            var data = SplitMultipart(content);

            if (data.Count() < 1)
            {
                throw new LicenseException("Data is not valid (too few data sections).");
            }

            var contactData = data.First();

            using (var rsa = RSA.Create())
            {
                rsa.FromXmlString(Keys.PrivateKey); // This will fail for non-private builds
                using (var sa = SymmetricAlgorithm.Create())
                {
                    byte[] keyex = new byte[rsa.KeySize >> 3];
                    Buffer.BlockCopy(contactData, 0, keyex, 0, keyex.Length);

                    var    def = new RSAPKCS1KeyExchangeDeformatter(rsa);
                    byte[] key = def.DecryptKeyExchange(keyex);

                    byte[] iv = new byte[sa.IV.Length];
                    Buffer.BlockCopy(contactData, keyex.Length, iv, 0, iv.Length);

                    using (var ct = sa.CreateDecryptor(key, iv))
                    {
                        var decryptedData = ct.TransformFinalBlock(contactData,
                                                                   keyex.Length + iv.Length,
                                                                   contactData.Length - (keyex.Length + iv.Length));

                        try
                        {
                            return(Encoding.UTF8.GetString(decryptedData));
                        }
                        catch (DecoderFallbackException exception)
                        {
                            // Encoded sequence is invalid, message has been
                            // compromized or encrypted with an invalid public key
                            throw new LicenseException("Data is not valid.", exception);
                        }
                    }
                }
            }
        }
Esempio n. 25
0
        /// <summary>
        /// Decripta tramite chiave RSA uno stream su altro stream
        /// </summary>
        /// <param name="key"></param>
        /// <param name="encInput"></param>
        /// <param name="output"></param>
        public static void RSA_AES_Decrypt(RSA key, Stream encInput, Stream output)
        {
            //Genera un IV e chiave di criptazione random
            var AES = createRijandelAES256();

            try
            {
                var sizeBuffer = new byte[sizeof(int)];
                int sizeLen;

                //Legge IV (size e data) non criptato
                encInput.Read(sizeBuffer, 0, sizeBuffer.Length);
                sizeLen = BitConverter.ToInt32(sizeBuffer, 0);
                var aesIvData = new byte[sizeLen];
                encInput.Read(aesIvData, 0, sizeLen);
                //Imposta Iv su AES
                AES.IV = aesIvData;

                //Legge keylen e AES criptata
                encInput.Read(sizeBuffer, 0, sizeBuffer.Length);
                sizeLen = BitConverter.ToInt32(sizeBuffer, 0);
                var aesKeyData = new byte[sizeLen];
                encInput.Read(aesKeyData, 0, aesKeyData.Length);

                //Decripta AES Key ed imposta su AES
                var def = new RSAPKCS1KeyExchangeDeformatter(key);
                AES.Key = def.DecryptKeyExchange(aesKeyData);

                //Legge resto del file criptato
                using (var cs = new CryptoStream(encInput, AES.CreateDecryptor(), CryptoStreamMode.Read))
                {
                    var buffer = new byte[sizeof(Int16)];
                    int iRead;

                    while ((iRead = cs.Read(buffer, 0, buffer.Length)) > 0)
                    {
                        output.Write(buffer, 0, iRead);
                    }
                }
            }
            finally
            {
                AES.Clear();
            }
        }
Esempio n. 26
0
        public override void HandleClient(TlsContext ctx, KeyExchange serverExchange)
        {
            // Read client premaster secret
            var encryptedPreMaster = ((RSAKeyExchange)serverExchange).encryptedPreMasterSecret;

            if (!ctx.Configuration.HasCredentials)
            {
                throw new TlsException(AlertDescription.BadCertificate, "Server certificate Private Key unavailable.");
            }

            // Decrypt premaster secret
            var deformatter = new RSAPKCS1KeyExchangeDeformatter(ctx.Configuration.PrivateKey);

            using (var preMasterSecret = new SecureBuffer(deformatter.DecryptKeyExchange(encryptedPreMaster))) {
                // Create master secret
                ComputeMasterSecret(ctx, preMasterSecret);
            }
        }
Esempio n. 27
0
        /// <summary>
        /// Performs a simple decryption of input using a
        /// combination of PublicKey + SymmetricKey decryption.
        /// </summary>
        /// <param name="rsa">The RSA.</param>
        /// <param name="input">The input.</param>
        /// <returns></returns>
        public static byte[] SimpleDecrypt(RSA rsa, byte[] input)
        {
            byte[] keyex = new byte[rsa.KeySize >> 3];
            Buffer.BlockCopy(input, 0, keyex, 0, keyex.Length);

            var def = new RSAPKCS1KeyExchangeDeformatter(rsa);

            byte[] key = def.DecryptKeyExchange(keyex);

            var sa = SymmetricAlgorithm.Create("AES");

            byte[] iv = new byte[sa.IV.Length];
            Buffer.BlockCopy(input, keyex.Length, iv, 0, iv.Length);

            var ct = sa.CreateDecryptor(key, iv);

            byte[] decrypt = ct.TransformFinalBlock(input, keyex.Length + iv.Length, input.Length - (keyex.Length + iv.Length));
            return(decrypt);
        }
Esempio n. 28
0
        public static byte[] DecryptKey(byte[] keyData, RSA rsa, bool useOAEP)
        {
            if (keyData == null)
            {
                throw new ArgumentNullException("keyData");
            }
            if (rsa == null)
            {
                throw new ArgumentNullException("rsa");
            }
            if (useOAEP)
            {
                RSAOAEPKeyExchangeDeformatter deformatter = new RSAOAEPKeyExchangeDeformatter(rsa);
                return(deformatter.DecryptKeyExchange(keyData));
            }
            RSAPKCS1KeyExchangeDeformatter deformatter2 = new RSAPKCS1KeyExchangeDeformatter(rsa);

            return(deformatter2.DecryptKeyExchange(keyData));
        }
Esempio n. 29
0
        /// <summary>
        /// Decrypts a secret with asymmetric RSA algorithm.
        /// </summary>
        /// <param name="keySize">Key size (1024, 2048, 4096, ...)</param>
        /// <param name="receiversPrivateKeyBlob">Private key of the receiver</param>
        /// <param name="encryptedSecret">Encrypted secret</param>
        /// <returns>Decrypted secret</returns>
        public static byte[] DecrpytSecret(int keySize, byte[] receiversPrivateKeyBlob, EncryptedSecret encryptedSecret)
        {
            using var receiversPrivateKey = new RSACryptoServiceProvider(dwKeySize: keySize);
            receiversPrivateKey.ImportCspBlob(receiversPrivateKeyBlob);

            using Aes aes = new AesCryptoServiceProvider();
            aes.IV = encryptedSecret.Iv;

            // Decrypt the session key
            RSAPKCS1KeyExchangeDeformatter keyDeformatter = new RSAPKCS1KeyExchangeDeformatter(receiversPrivateKey);
            aes.Key = keyDeformatter.DecryptKeyExchange(encryptedSecret.EncryptedSessionKey);

            // Decrypt the message
            using MemoryStream stream = new MemoryStream();
            using CryptoStream cryptoStream = new CryptoStream(stream, aes.CreateDecryptor(), CryptoStreamMode.Write);
            cryptoStream.Write(encryptedSecret.EncryptedMessage, 0, encryptedSecret.EncryptedMessage.Length);
            cryptoStream.Close();

            return stream.ToArray();
        }
 public static byte[] DecryptIt(RSA rsaer, Algorithms typo, string datatodecrypt)
 {
     byte[] result = null;
     try
     {
         byte[]             encrbytes = System.Convert.FromBase64String(datatodecrypt);
         SymmetricAlgorithm sa        = SymmetricAlgorithm.Create(RSACry.GetAlgorithmName(typo));
         byte[]             keyex     = new byte[(rsaer.KeySize >> 3) - 1];
         Buffer.BlockCopy(encrbytes, 0, keyex, 0, keyex.Length);
         RSAPKCS1KeyExchangeDeformatter def = new RSAPKCS1KeyExchangeDeformatter(rsaer);
         byte[] key = def.DecryptKeyExchange(keyex);
         byte[] iv  = new byte[sa.IV.Length - 1];
         Buffer.BlockCopy(encrbytes, keyex.Length, iv, 0, iv.Length);
         ICryptoTransform ct = sa.CreateDecryptor(key, iv);
         result = ct.TransformFinalBlock(encrbytes, keyex.Length + iv.Length, (encrbytes.Length - 1) - (keyex.Length + iv.Length));
     }
     catch (Exception ex)
     {
         throw new CryptographicException("Unable to decrypt: " + ex.Message);
     }
     return(result);
 }
Esempio n. 31
0
        private static byte[] Decrypt(RSA rsa, byte[] input)
        {
            // by default this will create a 128 bits AES (Rijndael) object
            byte[] decrypt;
            using (var sa = SymmetricAlgorithm.Create())
            {
                var keyex = new byte[rsa.KeySize >> 3];
                Buffer.BlockCopy(input, 0, keyex, 0, keyex.Length);

                var def = new RSAPKCS1KeyExchangeDeformatter(rsa);
                var key = def.DecryptKeyExchange(keyex);

                var iv = new byte[sa.IV.Length];
                Buffer.BlockCopy(input, keyex.Length, iv, 0, iv.Length);

                using (var ct = sa.CreateDecryptor(key, iv))
                {
                    decrypt = ct.TransformFinalBlock(input, keyex.Length + iv.Length, input.Length - (keyex.Length + iv.Length));
                }
            }
            return(decrypt);
        }
    static void Main(string[] args)
    {
        Console.WriteLine("This example shows how to use the public and private key from a certificate to encrypt and decrypt data.\r\n");
        byte[] data = Encoding.ASCII.GetBytes("Hello World!");
        // load the certificate from a file
        Certificate cert = Certificate.CreateFromCerFile(@"client.cer");

        // get an RSA instance that represents the public key of the certificate
        RSA public_key = cert.PublicKey;
        // create a PKCS#1 key exchange formatter instance with the public key
        RSAPKCS1KeyExchangeFormatter kef = new RSAPKCS1KeyExchangeFormatter(public_key);
        // encrypt the data, using the public key from the certificate
        byte[] encrypted = kef.CreateKeyExchange(data);

        // associate the certificate with its private key
        // we set exportable to true because decryption will fail on Windows 98
        // if this flag is not set. If you do not use Windows 98, you should set
        // the exportable flag to false for increased security.
        cert.AssociateWithPrivateKey(@"client.pvk", "test", true);
        // get an RSA instance that represents the private key
        RSA private_key = cert.PrivateKey;
        // create a PKCS#1 key exchange deformatter instance with the private key
        RSAPKCS1KeyExchangeDeformatter ked = new RSAPKCS1KeyExchangeDeformatter(private_key);
        // decrypt the data, using the private key from the certificate
        byte[] decrypted = ked.DecryptKeyExchange(encrypted);

        // print the results in the console
        Console.WriteLine("Input data: " + Encoding.ASCII.GetString(data) + "\r\n");
        Console.WriteLine("Encrypted data:\r\n" + BytesToHex(encrypted) + "\r\n");
        Console.WriteLine("Decrypted data: " + Encoding.ASCII.GetString(decrypted));
        Console.WriteLine("\r\nPress ENTER to continue...");
        Console.ReadLine();

        // clean up
        public_key.Clear();
        private_key.Clear();
    }
Esempio n. 33
0
    private byte[] Decrypt(RSA rsa, byte[] input)
    {
        // by default this will create a 128 bits AES (Rijndael) object
         SymmetricAlgorithm sa = SymmetricAlgorithm.Create ();

         byte[] keyex = new byte [rsa.KeySize >> 3];
         Buffer.BlockCopy (input, 0, keyex, 0, keyex.Length);

         RSAPKCS1KeyExchangeDeformatter def = new RSAPKCS1KeyExchangeDeformatter (rsa);
         byte[] key = def.DecryptKeyExchange (keyex);

         byte[] iv = new byte [sa.IV.Length];
         Buffer.BlockCopy (input, keyex.Length, iv, 0, iv.Length);

         ICryptoTransform ct = sa.CreateDecryptor (key, iv);
         byte[] decrypt = ct.TransformFinalBlock (input, keyex.Length + iv.Length, input.Length - (keyex.Length + iv.Length));
         return decrypt;
    }