Beispiel #1
0
        protected int TestProperties()
        {
            RC4 rc4 = GetRC4Instance();

            // test BlockSize property
            try {
                rc4.BlockSize = 0;
            } catch {
                AddError("RC4-TP1");
            }
            try {
                rc4.BlockSize = 8;
            } catch {
                AddError("RC4-TP2");
            }
            try {
                rc4.BlockSize = 1;
                AddError("RC4-TP3");
            } catch (CryptographicException) {
            } catch {
                AddError("RC4-TP4");
            }
            try {
                rc4.BlockSize = 128;
                AddError("RC4-TP5");
            } catch (CryptographicException) {
            } catch {
                AddError("RC4-TP6");
            }
            // test IV property
            try {
                rc4.IV = null;
            } catch {
                AddError("RC4-TP7");
            }
            try {
                rc4.IV = new byte[0];
            } catch {
                AddError("RC4-TP8");
            }
            try {
                rc4.IV = new byte[1];
            } catch {
                AddError("RC4-TP9");
            }
            try {
                rc4.IV = new byte[2];
                AddError("RC4-TP10");
            } catch (CryptographicException) {
            } catch {
                AddError("RC4-TP11");
            }
            rc4.Clear();
            return(11);
        }
        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();
            }
        }
Beispiel #3
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();
            }
        }
Beispiel #4
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();
            }
        }