Пример #1
0
        public static byte[] SymmetricEnryption(byte[] clearData, SymmetricKeys keys)
        {
            //0. declare the algorithm to use
            Rijndael myAlg = Rijndael.Create();

            //1. first we generate the secret key and iv
            //var keys = GenerateKeys();

            //2. load the data into a memoryStream
            MemoryStream msIn = new MemoryStream(clearData);

            msIn.Position = 0; //Making sure that the pointer of the byte to read next is at the beginning so we encrypt everything

            //3. declare where to store the encrypted data
            MemoryStream msOut = new MemoryStream();

            //4. declaring a Stream which handles data encryption
            CryptoStream cs = new CryptoStream(msOut, myAlg.CreateEncryptor(keys.SecretKey, keys.Iv), CryptoStreamMode.Write);

            //5. we start the encrypting engine
            msIn.CopyTo(cs);

            //6. make sure that the data is all written (flushed) into msOut
            cs.FlushFinalBlock();

            //7
            cs.Close();

            //8
            return(msOut.ToArray());
        }
Пример #2
0
        public static MemoryStream HybridDecrypt(MemoryStream encFile, string privateKey)
        {
            encFile.Position = 0;

            //1) Read enc key
            byte[] retrievedEncKey = new byte[128];
            encFile.Read(retrievedEncKey, 0, 128);

            //2) Read enc iv
            byte[] retrievedEncIv = new byte[128];
            encFile.Read(retrievedEncIv, 0, 128);

            //3) Decrypt using the privatekey (asymmetric) the 1 and 2
            byte[] decKey = AsymmetricDecrypt(retrievedEncKey, privateKey);
            byte[] decIv  = AsymmetricDecrypt(retrievedEncIv, privateKey);

            SymmetricKeys keys = new SymmetricKeys();

            keys.SecretKey = decKey;
            keys.Iv        = decIv;

            //4) Read the rest of the file (which is the actual file content)

            MemoryStream actualencryptedfilecontent = new MemoryStream();

            encFile.CopyTo(actualencryptedfilecontent);

            //5 Symmetrically dec what you read in no 4 using what you dec in step no 3
            byte[] bytes   = actualencryptedfilecontent.ToArray();
            byte[] decFile = SymmetricDecrypt(bytes, keys);

            MemoryStream actualFile = new MemoryStream(decFile);

            return(actualFile);
        }
Пример #3
0
        public static SymmetricKeys GenerateKeys()
        {
            Rijndael           myAlg       = Rijndael.Create();
            Rfc2898DeriveBytes myGenerator = new Rfc2898DeriveBytes(password, salt);
            SymmetricKeys      keys        = new SymmetricKeys()
            {
                SecretKey = myGenerator.GetBytes(myAlg.KeySize / 8),
                Iv        = myGenerator.GetBytes(myAlg.BlockSize / 8)
            };

            return(keys);
        }
Пример #4
0
        public static SymmetricKeys GenerateKeys()
        {
            // Password + Salt >>>>> Algorithm >>> Secret Key + IV (which is the input needed by the encryption algorithm)

            Rijndael myAlg = Rijndael.Create();

            Rfc2898DeriveBytes myGenerator = new Rfc2898DeriveBytes(password, salt);

            SymmetricKeys keys = new SymmetricKeys()
            { //1 byte =  8 bits
                SecretKey = myGenerator.GetBytes(myAlg.KeySize / 8),
                Iv        = myGenerator.GetBytes(myAlg.BlockSize / 8)
            };

            return(keys);
        }
Пример #5
0
        public static MemoryStream HybridEncrypt(MemoryStream clearFile, string publicKey)
        {
            //1. Generate the symmetric keys
            Rijndael myAlg = Rijndael.Create();

            myAlg.GenerateKey();
            myAlg.GenerateIV();
            var key = myAlg.Key;
            var iv  = myAlg.IV;

            //2. Encrypting the clearFile using Symmetric Encryption

            SymmetricKeys keys = new SymmetricKeys();

            keys.SecretKey = key;
            keys.Iv        = iv;


            //3. Asymmetrically encrypt using the public key, the sym key and iv above
            //string keyS = Convert.ToBase64String(key);
            byte[] encryptedKey = AsymmetricEnrypt(key, publicKey);

            //4. Store the above encrypted data n one file
            byte[] encryptedIv = AsymmetricEnrypt(iv, publicKey);

            MemoryStream msOut = new MemoryStream();

            msOut.Write(encryptedKey, 0, encryptedKey.Length);
            msOut.Write(encryptedIv, 0, encryptedIv.Length);

            byte[] bytes          = clearFile.ToArray();
            byte[] encryptedBytes = SymmetricEnryption(bytes, keys);

            MemoryStream encryptedfileContent = new MemoryStream(encryptedBytes);

            encryptedfileContent.Position = 0;
            encryptedfileContent.CopyTo(msOut);

            return(msOut);
        }