zoukankan      html  css  js  c++  java
  • DESCryptoServiceProvider 类

    下面的示例使用 DESCryptoServiceProvider 类将一些数据加密到内存,然后解密数据。

    // This sample demonstrates using a key based on the cryptographic service provider (CSP) version
    // of the Data Encryption Standard (DES)algorithm to encrypt a string to a byte array, and then
    // to decrypt the byte array back to a string.

    using System;
    using System.IO;
    using System.Text;
    using System.Security.Cryptography;

    class CryptoMemoryStream
    {
    // Main method.
        public static void Main()
        {
            // Create a new DES key.
            DESCryptoServiceProvider key = new DESCryptoServiceProvider();

            // Encrypt a string to a byte array.
            byte[] buffer = Encrypt("This is some plaintext!", key);

            // Decrypt the byte array back to a string.
            string plaintext =  Decrypt(buffer, key);

            // Display the plaintext value to the console.
            Console.WriteLine(plaintext);
        }
       
    // Encrypt the string.
        public static byte[] Encrypt(string PlainText, SymmetricAlgorithm key)
        {
            // Create a memory stream.
            MemoryStream ms = new MemoryStream();

            // Create a CryptoStream using the memory stream and the
            // CSP DES key. 
            CryptoStream encStream = new CryptoStream(ms, key.CreateEncryptor(), CryptoStreamMode.Write);

            // Create a StreamWriter to write a string
            // to the stream.
            StreamWriter sw = new StreamWriter(encStream);

            // Write the plaintext to the stream.
            sw.WriteLine(PlainText);

            // Close the StreamWriter and CryptoStream.
            sw.Close();
            encStream.Close();

            // Get an array of bytes that represents
            // the memory stream.
            byte[] buffer = ms.ToArray();

            // Close the memory stream.
            ms.Close();

            // Return the encrypted byte array.
            return buffer;
        }

       // Decrypt the byte array.
        public static string Decrypt(byte[] CypherText, SymmetricAlgorithm key)
        {
            // Create a memory stream to the passed buffer.
            MemoryStream ms = new MemoryStream(CypherText);

            // Create a CryptoStream using the memory stream and the
            // CSP DES key.
            CryptoStream encStream = new CryptoStream(ms, key.CreateDecryptor(), CryptoStreamMode.Read);

            // Create a StreamReader for reading the stream.
            StreamReader sr = new StreamReader(encStream);

            // Read the stream as a string.
            string val = sr.ReadLine();

            // Close the streams.
            sr.Close();
            encStream.Close();
            ms.Close();
               
            return val;
        }
    }

  • 相关阅读:
    【LuoguP4770】[NOI2018] 你的名字
    【LuoguP5171】Earthquake
    【LuoguP3747】[六省联考2017] 相逢是问候
    【LuoguP4916】魔力环
    YOLO2:实时目标检测视频教程,视频演示, Android Demo ,开源教学项目,论文。
    谷歌发布 TensorFlow Lite [官方网站,文档]
    Chinese-Text-Classification,用卷积神经网络基于 Tensorflow 实现的中文文本分类。
    Chinese-Text-Classification:Tensorflow CNN 模型实现的中文文本分类器[不分词版]
    Hinton's paper Dynamic Routing Between Capsules 的 Tensorflow , Keras ,Pytorch实现
    谷歌开发者:看可口可乐公司是怎么玩转TensorFlow的?
  • 原文地址:https://www.cnblogs.com/lzjsky/p/2003636.html
Copyright © 2011-2022 走看看