Exemplo n.º 1
0
        public SymmetricParameters GenerateSymmetricParameters(string password, string salt)
        {
            Rijndael myalg = Rijndael.Create();

            Rfc2898DeriveBytes  mygenerator = new Rfc2898DeriveBytes(password, Encoding.UTF8.GetBytes(salt));
            SymmetricParameters ps          = new SymmetricParameters()
            {
                SecretKey = mygenerator.GetBytes(myalg.KeySize / 8),
                IV        = mygenerator.GetBytes(myalg.BlockSize / 8)
            };

            return(ps);
        }
Exemplo n.º 2
0
        public byte[] Decrypt(string fileData, SymmetricParameters p)
        {
            byte[] fileDataByte = Convert.FromBase64String(fileData);

            Rijndael myalg = Rijndael.Create();

            MemoryStream msOut = new MemoryStream();
            CryptoStream myEncryptingStream = new CryptoStream(msOut, myalg.CreateDecryptor(p.SecretKey, p.IV), CryptoStreamMode.Write);

            MemoryStream msIn = new MemoryStream(fileDataByte);

            msIn.Position = 0;
            msIn.CopyTo(myEncryptingStream);
            myEncryptingStream.Flush();
            myEncryptingStream.FlushFinalBlock();

            msOut.Position = 0;
            return(msOut.ToArray());
        }
Exemplo n.º 3
0
        public string Encrypt(byte[] fileData, SymmetricParameters p)
        {
            string result = String.Empty;

            Rijndael myalg = Rijndael.Create();

            MemoryStream msOut = new MemoryStream();
            CryptoStream myEncryptingStream = new CryptoStream(msOut, myalg.CreateEncryptor(p.SecretKey, p.IV), CryptoStreamMode.Write);

            MemoryStream msIn = new MemoryStream(fileData);

            msIn.Position = 0;
            msIn.CopyTo(myEncryptingStream);
            myEncryptingStream.Flush();
            myEncryptingStream.FlushFinalBlock();

            msOut.Position = 0;

            result = Convert.ToBase64String(msOut.ToArray());

            return(result);
        }