回 帖 发 新 帖 刷新版面

主题:有關加密

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Security.Cryptography;
using System.IO;
using System.Text ;
using System.Diagnostics;

UTF8Encoding utf8Encoding = new UTF8Encoding();
            string aa=textBox1.Text;
            byte[] inputByteArray = utf8Encoding.GetBytes(aa.ToCharArray());

            // 方式一:调用默认的DES实现方法DES_CSP.
            DES des = DES.Create();
            // 方式二:直接使用DES_CSP()实现DES的实体
            //DES_CSP DES = new DES_CSP();

            // 初始化DES加密的密钥和一个随机的、8比特的初始化向量(IV)
            Byte[] key = {0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef};
            Byte[] IV = {0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef};
            des.Key = key;
            des.IV = IV;
 建立加密流這裡報錯(SymmetricStreamEncryptor.CryptoMemoryStream 這二個類型和namespace找不到,)請問這是哪個空間的,或少了什麼引用
            SymmetricStreamEncryptor sse = des.CreateEncryptor();

            // 使用CryptoMemoryStream方法获取加密过程的输出
            CryptoMemoryStream cms = new CryptoMemoryStream();

            // 将SymmetricStreamEncryptor流中的加密数据输出到CryptoMemoryStream中
            sse.SetSink(cms);

            // 加密完毕,将结果输出到控制台
            sse.Write(inputByteArray);
            sse.CloseStream();

            // 获取加密数据
            byte[] encryptedData = cms.Data;

回复列表 (共1个回复)

沙发

不清楚

我的是这样

public void DES(string InputName, string OutputName, string Key)
        {
            string FileName = InputName;
            string OutputFileName = OutputName;
            string MyKey = Key;
            byte[] MyDESIV ={ 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
            byte[] MyDESKey ={ 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
            if (MyKey.Length == 6)
            {
                MyDESKey = new byte[]{(byte)MyKey[0],(byte)MyKey[1],(byte)MyKey[2],(byte)

MyKey[3],
                    (byte)MyKey[4],(byte)MyKey[5],0x07,0x08};
            }
            if (MyKey.Length == 7)
            {
                MyDESKey = new byte[]{(byte)MyKey[0],(byte)MyKey[1],(byte)MyKey[2],(byte)

MyKey[3],
                    (byte)MyKey[4],(byte)MyKey[5],(byte)MyKey[6],0x07};
            }
            if (MyKey.Length >= 8)
            {
                MyDESKey = new byte[]{(byte)MyKey[0],(byte)MyKey[1],(byte)MyKey[2],(byte)

MyKey[3],
                    (byte)MyKey[4],(byte)MyKey[5],(byte)MyKey[6],(byte)MyKey[7]};
            }
            FileStream MyInFileStream = new FileStream(FileName, FileMode.OpenOrCreate, 

FileAccess.Read);
            FileStream MyOutFileStream = new FileStream(OutputFileName, 

FileMode.OpenOrCreate, FileAccess.Write);
            MyOutFileStream.SetLength(0);
            byte[] InsertData = new byte[100];
            int CompletedLength = 0;
            long InFileSize = MyInFileStream.Length;            
            DES MyDES = new DESCryptoServiceProvider();
            CryptoStream EncryptStream = new CryptoStream(MyOutFileStream, 

MyDES.CreateEncryptor(MyDESKey, MyDESIV), CryptoStreamMode.Write);
            while (CompletedLength < InFileSize)
            {
                int Length = MyInFileStream.Read(InsertData, 0, 100);
                EncryptStream.Write(InsertData, 0, Length);
                CompletedLength = CompletedLength + Length;
                //progressBar2.PerformStep();
            }
            EncryptStream.Close();
            MyOutFileStream.Close();
            MyInFileStream.Close();

我来回复

您尚未登录,请登录后再回复。点此登录或注册