Симметричное шифрование данных

10 сентября 2009

Данный статический класс предназначен для удобного симметрического шифрования строки или массива байт:

///
/// Provides base functions for encrypting and decrypting data through the symmetric algorithm
///
public static class Cryptography
{
    ///
    /// Encrypts data
    ///
    /// Data for the encryption
    /// Encryption key
    /// Encrypted data
    public static byte[] Encrypt(byte[] data, string password)
    {
        SymmetricAlgorithm sa = Rijndael.Create();
        ICryptoTransform ct = sa.CreateEncryptor(
            (new PasswordDeriveBytes(password, null)).GetBytes(16),
            new byte[16]);

        MemoryStream ms = new MemoryStream();
        CryptoStream cs = new CryptoStream(ms, ct, CryptoStreamMode.Write);

        cs.Write(data, 0, data.Length);
        cs.FlushFinalBlock();

        return ms.ToArray();
    }

    ///
    /// Encrypts data
    ///
    /// Data for the encryption
    /// Encryption key
    /// Encrypted data
    public static string Encrypt(string data, string password)
    {
        return Convert.ToBase64String(Encrypt(Encoding.UTF8.GetBytes(data), password));
    }

    ///
    /// Decrypts data
    ///
    /// Data for the decryption
    /// Decryption key
    /// Decrypted data
    static public byte[] Decrypt(byte[] data, string password)
    {
        BinaryReader br = new BinaryReader(InternalDecrypt(data, password));
        return br.ReadBytes((int)br.BaseStream.Length);
    }

    ///
    /// Decrypts data
    ///
    /// Data for the decryption
    /// Decryption key
    /// Decrypted data
    static public string Decrypt(string data, string password)
    {
        CryptoStream cs = InternalDecrypt(Convert.FromBase64String(data), password);
        StreamReader sr = new StreamReader(cs);
        return sr.ReadToEnd();
    }

    static CryptoStream InternalDecrypt(byte[] data, string password)
    {
        SymmetricAlgorithm sa = Rijndael.Create();
        ICryptoTransform ct = sa.CreateDecryptor(
            (new PasswordDeriveBytes(password, null)).GetBytes(16),
            new byte[16]);

        MemoryStream ms = new MemoryStream(data);
        return new CryptoStream(ms, ct, CryptoStreamMode.Read);
    }
}

Entry Filed under: Articles. Метки: , , , , , .



2 комментариев Add your own

  • 1.    Salvador  |  ноября 28, 2009 at 01:43

    Sorry. When you make a mistake, don’t look back at it long. Take the reason of the thing into your mind and then look forward. Mistakes are lessons of wisdom. The past cannot be changed. The future is yet in your power. Help me! Can not find sites on the: Strattera liver symptoms. I found only this - strattera contraindications. Varalli findings of fact and conclusions of law: daniel m, strattera. traugott looked that the board could see notices to the centers, strattera. Strattera, i was the casein- effekten with three means who did her family around 100 influence of the telephone. THX :-(, Salvador from Burma.

  • 2.    Caesar  |  декабря 8, 2009 at 01:14

    Hey. Winning is important to me, but what brings me real joy is the experience of being fully engaged in whatever I’m doing.
    I am from Thailand and know bad English, give please true I wrote the following sentence: “Strattera, and those results cannot be written until the people are required.”

    With love :(, Caesar.

Оставить комментарий

Required

Required, hidden

*
Введи слово с картинки :)
Anti-Spam Image

Some HTML allowed:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Trackback this post  |  Subscribe to the comments via RSS Feed


Метки