Esempio n. 1
0
        public void ToCapiPrivateKeyBlob_PublicKeyOnly()
        {
            RSA rsa = RSA.Create();

            rsa.FromXmlString(strongNamePublicKeyString);
            byte[] publicKey = CryptoConvert.ToCapiPrivateKeyBlob(rsa);
        }
Esempio n. 2
0
        public void ToCapiPrivateKeyBlob_PublicKeyOnly_DSA()
        {
            DSA dsa = DSA.Create();

            dsa.FromXmlString(dsaPubKeyString);
            byte[] pubkey = CryptoConvert.ToCapiPrivateKeyBlob(dsa);
        }
Esempio n. 3
0
        public void ToCapiPrivateKeyBlob_DSA()
        {
            DSA dsa = DSA.Create();

            dsa.FromXmlString(dsaKeyPairString);
            byte[] keypair = CryptoConvert.ToCapiPrivateKeyBlob(dsa);
            AssertEquals("KeyPair", dsaPrivBlob, keypair);
        }
Esempio n. 4
0
        public void ToCapiPrivateKeyBlob()
        {
            RSA rsa = RSA.Create();

            rsa.FromXmlString(strongNameString);
            byte[] keypair = CryptoConvert.ToCapiPrivateKeyBlob(rsa);
            AssertEquals("KeyPair", strongName, keypair);
        }
Esempio n. 5
0
 public byte[] ExportCspBlob(bool includePrivateParameters)
 {
     byte[] blob = null;
     if (includePrivateParameters)
     {
         blob = CryptoConvert.ToCapiPrivateKeyBlob(this);
     }
     else
     {
         blob = CryptoConvert.ToCapiPublicKeyBlob(this);
     }
     return(blob);
 }
        public byte[] ExportCspBlob(bool includePrivateParameters)
        {
            byte[] blob = null;
            if (includePrivateParameters)
            {
                blob = CryptoConvert.ToCapiPrivateKeyBlob(this);
            }
            else
            {
                blob = CryptoConvert.ToCapiPublicKeyBlob(this);
            }

            // ALGID (bytes 4-7) - default is KEYX
            // 00 24 00 00 (for CALG_RSA_SIGN)
            // 00 A4 00 00 (for CALG_RSA_KEYX)
            blob[5] = (byte)(((store != null) && (store.Parameters.KeyNumber == AT_SIGNATURE)) ? 0x24 : 0xA4);
            return(blob);
        }
Esempio n. 7
0
 public byte[] GetBytes()
 {
     return(CryptoConvert.ToCapiPrivateKeyBlob(RSA));
 }
        public void Save(string filename, string password)
        {
            if (filename == null)
            {
                throw new ArgumentNullException("filename");
            }

            byte[]     blob = null;
            FileStream fs   = File.Open(filename, FileMode.Create, FileAccess.Write);

            try
            {
                // header
                byte[] empty = new byte [4];
                byte[] data  = BitConverterLE.GetBytes(magic);
                fs.Write(data, 0, 4);   // magic
                fs.Write(empty, 0, 4);  // reserved
                data = BitConverterLE.GetBytes(keyType);
                fs.Write(data, 0, 4);   // key type

                encrypted = (password != null);
                blob      = CryptoConvert.ToCapiPrivateKeyBlob(rsa);
                if (encrypted)
                {
                    data = BitConverterLE.GetBytes(1);
                    fs.Write(data, 0, 4); // encrypted
                    data = BitConverterLE.GetBytes(16);
                    fs.Write(data, 0, 4); // saltlen
                    data = BitConverterLE.GetBytes(blob.Length);
                    fs.Write(data, 0, 4); // keylen

                    byte[] salt = new byte [16];
                    RC4    rc4  = RC4.Create();
                    byte[] key  = null;
                    try
                    {
                        // generate new salt (16 bytes)
                        RandomNumberGenerator rng = RandomNumberGenerator.Create();
                        rng.GetBytes(salt);
                        fs.Write(salt, 0, salt.Length);
                        key = DeriveKey(salt, password);
                        if (Weak)
                        {
                            Array.Clear(key, 5, 11);
                        }
                        ICryptoTransform enc = rc4.CreateEncryptor(key, null);
                        // we don't encrypt the header part of the BLOB
                        enc.TransformBlock(blob, 8, blob.Length - 8, blob, 8);
                    }
                    finally
                    {
                        Array.Clear(salt, 0, salt.Length);
                        Array.Clear(key, 0, key.Length);
                        rc4.Clear();
                    }
                }
                else
                {
                    fs.Write(empty, 0, 4); // encrypted
                    fs.Write(empty, 0, 4); // saltlen
                    data = BitConverterLE.GetBytes(blob.Length);
                    fs.Write(data, 0, 4);  // keylen
                }

                fs.Write(blob, 0, blob.Length);
            }
            finally
            {
                // BLOB may include an uncrypted keypair
                Array.Clear(blob, 0, blob.Length);
                fs.Close();
            }
        }
Esempio n. 9
0
        public void Save(string filename, string password)
        {
            if (filename == null)
            {
                throw new ArgumentNullException(nameof(filename));
            }
            byte[]     numArray1  = (byte[])null;
            FileStream fileStream = File.Open(filename, FileMode.Create, FileAccess.Write);

            try
            {
                byte[] buffer = new byte[4];
                byte[] bytes1 = BitConverterLE.GetBytes(2964713758U);
                fileStream.Write(bytes1, 0, 4);
                fileStream.Write(buffer, 0, 4);
                byte[] bytes2 = BitConverterLE.GetBytes(this.keyType);
                fileStream.Write(bytes2, 0, 4);
                this.encrypted = password != null;
                numArray1      = CryptoConvert.ToCapiPrivateKeyBlob(this.rsa);
                if (this.encrypted)
                {
                    byte[] bytes3 = BitConverterLE.GetBytes(1);
                    fileStream.Write(bytes3, 0, 4);
                    byte[] bytes4 = BitConverterLE.GetBytes(16);
                    fileStream.Write(bytes4, 0, 4);
                    byte[] bytes5 = BitConverterLE.GetBytes(numArray1.Length);
                    fileStream.Write(bytes5, 0, 4);
                    byte[] numArray2 = new byte[16];
                    RC4    rc4       = RC4.Create();
                    byte[] rgbKey    = (byte[])null;
                    try
                    {
                        RandomNumberGenerator.Create().GetBytes(numArray2);
                        fileStream.Write(numArray2, 0, numArray2.Length);
                        rgbKey = this.DeriveKey(numArray2, password);
                        if (this.Weak)
                        {
                            Array.Clear((Array)rgbKey, 5, 11);
                        }
                        rc4.CreateEncryptor(rgbKey, (byte[])null).TransformBlock(numArray1, 8, numArray1.Length - 8, numArray1, 8);
                    }
                    finally
                    {
                        Array.Clear((Array)numArray2, 0, numArray2.Length);
                        Array.Clear((Array)rgbKey, 0, rgbKey.Length);
                        rc4.Clear();
                    }
                }
                else
                {
                    fileStream.Write(buffer, 0, 4);
                    fileStream.Write(buffer, 0, 4);
                    byte[] bytes3 = BitConverterLE.GetBytes(numArray1.Length);
                    fileStream.Write(bytes3, 0, 4);
                }
                fileStream.Write(numArray1, 0, numArray1.Length);
            }
            finally
            {
                Array.Clear((Array)numArray1, 0, numArray1.Length);
                fileStream.Close();
            }
        }
Esempio n. 10
0
        public void Save(string filename, string password)
        {
            if (filename == null)
            {
                throw new ArgumentNullException("filename");
            }
            byte[]     array      = null;
            FileStream fileStream = File.Open(filename, FileMode.Create, FileAccess.Write);

            try
            {
                byte[] array2 = new byte[4];
                byte[] bytes  = BitConverterLE.GetBytes(2964713758u);
                fileStream.Write(bytes, 0, 4);
                fileStream.Write(array2, 0, 4);
                bytes = BitConverterLE.GetBytes(this.keyType);
                fileStream.Write(bytes, 0, 4);
                this.encrypted = (password != null);
                array          = CryptoConvert.ToCapiPrivateKeyBlob(this.rsa);
                if (this.encrypted)
                {
                    bytes = BitConverterLE.GetBytes(1);
                    fileStream.Write(bytes, 0, 4);
                    bytes = BitConverterLE.GetBytes(16);
                    fileStream.Write(bytes, 0, 4);
                    bytes = BitConverterLE.GetBytes(array.Length);
                    fileStream.Write(bytes, 0, 4);
                    byte[] array3 = new byte[16];
                    RC4    rc     = RC4.Create();
                    byte[] array4 = null;
                    try
                    {
                        RandomNumberGenerator randomNumberGenerator = RandomNumberGenerator.Create();
                        randomNumberGenerator.GetBytes(array3);
                        fileStream.Write(array3, 0, array3.Length);
                        array4 = this.DeriveKey(array3, password);
                        if (this.Weak)
                        {
                            Array.Clear(array4, 5, 11);
                        }
                        ICryptoTransform cryptoTransform = rc.CreateEncryptor(array4, null);
                        cryptoTransform.TransformBlock(array, 8, array.Length - 8, array, 8);
                    }
                    finally
                    {
                        Array.Clear(array3, 0, array3.Length);
                        Array.Clear(array4, 0, array4.Length);
                        rc.Clear();
                    }
                }
                else
                {
                    fileStream.Write(array2, 0, 4);
                    fileStream.Write(array2, 0, 4);
                    bytes = BitConverterLE.GetBytes(array.Length);
                    fileStream.Write(bytes, 0, 4);
                }
                fileStream.Write(array, 0, array.Length);
            }
            finally
            {
                Array.Clear(array, 0, array.Length);
                fileStream.Close();
            }
        }
Esempio n. 11
0
 public byte[] GetBytes() =>
 CryptoConvert.ToCapiPrivateKeyBlob(this.RSA);