Esempio n. 1
0
    public static void Main()
    {
        //chave secreta
        byte[] Key = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
        //vetor de inicialização
        byte[] IV = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };

        //Stream de memória
        IO.MemoryStream memstream = new IO.MemoryStream(15);
        //Stream de criptografia
        CP.RC2CryptoServiceProvider provider  = new CP.RC2CryptoServiceProvider();
        CP.ICryptoTransform         transform = provider.CreateEncryptor(Key, IV);
        CP.CryptoStreamMode         mode      = CP.CryptoStreamMode.Write;
        CP.CryptoStream             stream    = new CP.CryptoStream(memstream, transform, mode);

        //Lê cada caracter da string
        foreach (char ch in "Isto é um teste")
        {
            stream.WriteByte((Convert.ToByte(ch)));
        }

        int c;

        //Reposiciona o ponteiro para leitura
        memstream.Position = c = 0; //técnica não trivial, mas válida

        while ((c = memstream.ReadByte()) != -1)
        {
            Console.Write((char)c);
        }

        stream.Close();    //Libera a stream (crypto)
        memstream.Close(); //Libera a stream (mem)
    }
        private void EncryptUsingDotNet(Stream istream, Stream ostream, byte[] iv, bool forEncryption)
        {
            var engine = BlockCipherModel.Engine.Instance <SysSecurity.SymmetricAlgorithm>();
            var mode   =
                (SysSecurity.CipherMode)Enum.Parse(typeof(SysSecurity.CipherMode), BlockCipherModel.Mode.ToString());
            var padding =
                (SysSecurity.PaddingMode)
                Enum.Parse(typeof(SysSecurity.PaddingMode), BlockCipherModel.Padding.ToString());

            engine.Mode    = mode;
            engine.Padding = padding;
            SysSecurity.ICryptoTransform cryptoTranform = forEncryption
                                                              ? engine.CreateEncryptor(PbkdfModel.Key, iv)
                                                              : engine.CreateDecryptor(PbkdfModel.Key, iv);
            SysSecurity.CryptoStreamMode csMode = forEncryption
                                                      ? SysSecurity.CryptoStreamMode.Write
                                                      : SysSecurity.CryptoStreamMode.Read;
            using (
                var encStream = new SysSecurity.CryptoStream(forEncryption ? ostream : istream, cryptoTranform, csMode))
            {
                if (forEncryption)
                {
                    CopyStream(istream, encStream);
                }
                else
                {
                    CopyStream(encStream, ostream);
                }
            }
        }