C# 填充无效,无法删除。在cryptoStream.FlushFinalBlock()上;在Decrypt方法中,我得到了这个异常 使用系统; 使用System.Collections.Generic; 使用系统组件模型; 使用系统数据; 使用系统图; 使用System.Linq; 使用系统文本; 使用System.Windows.Forms; 使用System.IO; 使用系统线程; Net系统; 使用System.Net.Sockets; 使用System.Security.Cryptography; 命名空间GUISimpleTCPClient { 公共部分类GUISimpleTCPClient:表单 { TcpClient client=null; 网络流; 流阅读器; 流作者; 公共GUISimpleTCPClient() { 初始化组件(); } 私有void Connect\u单击(对象发送方,事件参数e) { 尝试 { client=newtcpclient(ipAddress.Text,Convert.ToInt32(port.Text)); listBox1.Items.Add(“连接到服务器”); stream=client.GetStream(); 读卡器=新的流读卡器(流); writer=新的流writer(流); 字符串msg=reader.ReadLine(); listBox1.Items.Add(“接收表单服务器:”); listBox1.Items.Add(msg); } 捕获(例外情况除外) { MessageBox.Show(例如Message); } } 私有无效发送\单击(对象发送者,事件参数e) { 字符串msg=M.Text; Symmetrialgorithm Symmetrialgorithm=Symmetrialgorithm.Create(); byte[]data=Encoding.UTF8.GetBytes(“密码”); symmetricAlgorithm.Key=GetLegalKey(“密码”); symmetricAlgorithm.IV=getv(); 字节[]密码=加密(对称算法,数据); System.Text.asciencoding enc=新的System.Text.asciencoding(); msg=enc.GetString(密码); //System.Text.Encoding Encoding=新的System.Text.Encoding(); System.Text.asciencoding denc=新的System.Text.asciencoding(); Byte[]dec_data=denc.GetBytes(msg); 字符串解码=解密(对称算法,dec_数据); 尝试 { writer.WriteLine(msg); writer.Flush(); msg=reader.ReadLine(); 如果(消息长度!=0) listBox1.Items.Add(解码); } 捕获(例外情况除外) { MessageBox.Show(例如Message); } } 私有静态字节[]加密(对称算法对称算法, 字节[]数据) { //FileStream outStream=File.OpenWrite(outPath); ICryptoTransform transform=symmetricAlgorithm.CreateEncryptor(); MemoryStream t=新的MemoryStream(); 加密流加密流= 新的加密流(t,transform,CryptoStreamMode.Write); 字节[]infle=数据; //ReadAllBytes(inPath); cryptoStream.Write(数据,0,数据长度); cryptoStream.FlushFinalBlock(); cryptoStream.Close(); 返回t.ToArray(); } 私有静态字符串解密(对称算法对称算法, 字节[]密码) { ICryptoTransform transform=symmetricAlgorithm.CreateDecryptor(); //Stream inStream=File.OpenRead(inPath); MemoryStream inStream=新的MemoryStream(); 加密流加密流= 新的加密流(流内、转换、加密流模式.Write); 字节[]缓冲区=新字节[100]; cryptoStream.Write(密码,0,密码长度); cryptoStream.FlushFinalBlock(); //Console.WriteLine(); 返回Encoding.UTF8.GetString(inStream.ToArray()); //int length=cryptoStream.Read(buffer,0,buffer.length); //Stream outStream=File.OpenWrite(outPath); /*而(长度>0) { 输出流写入(缓冲区,0,长度); 长度=cryptoStream.Read(缓冲区,0,缓冲区.length); }*/ //流内关闭(); //exptream.Close(); } 专用字节[]GetLegalKey(字符串键) { Symmetrialgorithm Symmetrialgorithm=Symmetrialgorithm.Create(); 字符串sTemp=键; 对称算法生成基(); 字节[]bytTemp=对称算法键; int KeyLength=字节长度; 如果(sTemp.Length>KeyLength) sTemp=sTemp.Substring(0,KeyLength); 否则如果(sTemp.LengthIVLength) sTemp=sTemp.子字符串(0,IVLength); 否则,如果(sTemp.Length

C# 填充无效,无法删除。在cryptoStream.FlushFinalBlock()上;在Decrypt方法中,我得到了这个异常 使用系统; 使用System.Collections.Generic; 使用系统组件模型; 使用系统数据; 使用系统图; 使用System.Linq; 使用系统文本; 使用System.Windows.Forms; 使用System.IO; 使用系统线程; Net系统; 使用System.Net.Sockets; 使用System.Security.Cryptography; 命名空间GUISimpleTCPClient { 公共部分类GUISimpleTCPClient:表单 { TcpClient client=null; 网络流; 流阅读器; 流作者; 公共GUISimpleTCPClient() { 初始化组件(); } 私有void Connect\u单击(对象发送方,事件参数e) { 尝试 { client=newtcpclient(ipAddress.Text,Convert.ToInt32(port.Text)); listBox1.Items.Add(“连接到服务器”); stream=client.GetStream(); 读卡器=新的流读卡器(流); writer=新的流writer(流); 字符串msg=reader.ReadLine(); listBox1.Items.Add(“接收表单服务器:”); listBox1.Items.Add(msg); } 捕获(例外情况除外) { MessageBox.Show(例如Message); } } 私有无效发送\单击(对象发送者,事件参数e) { 字符串msg=M.Text; Symmetrialgorithm Symmetrialgorithm=Symmetrialgorithm.Create(); byte[]data=Encoding.UTF8.GetBytes(“密码”); symmetricAlgorithm.Key=GetLegalKey(“密码”); symmetricAlgorithm.IV=getv(); 字节[]密码=加密(对称算法,数据); System.Text.asciencoding enc=新的System.Text.asciencoding(); msg=enc.GetString(密码); //System.Text.Encoding Encoding=新的System.Text.Encoding(); System.Text.asciencoding denc=新的System.Text.asciencoding(); Byte[]dec_data=denc.GetBytes(msg); 字符串解码=解密(对称算法,dec_数据); 尝试 { writer.WriteLine(msg); writer.Flush(); msg=reader.ReadLine(); 如果(消息长度!=0) listBox1.Items.Add(解码); } 捕获(例外情况除外) { MessageBox.Show(例如Message); } } 私有静态字节[]加密(对称算法对称算法, 字节[]数据) { //FileStream outStream=File.OpenWrite(outPath); ICryptoTransform transform=symmetricAlgorithm.CreateEncryptor(); MemoryStream t=新的MemoryStream(); 加密流加密流= 新的加密流(t,transform,CryptoStreamMode.Write); 字节[]infle=数据; //ReadAllBytes(inPath); cryptoStream.Write(数据,0,数据长度); cryptoStream.FlushFinalBlock(); cryptoStream.Close(); 返回t.ToArray(); } 私有静态字符串解密(对称算法对称算法, 字节[]密码) { ICryptoTransform transform=symmetricAlgorithm.CreateDecryptor(); //Stream inStream=File.OpenRead(inPath); MemoryStream inStream=新的MemoryStream(); 加密流加密流= 新的加密流(流内、转换、加密流模式.Write); 字节[]缓冲区=新字节[100]; cryptoStream.Write(密码,0,密码长度); cryptoStream.FlushFinalBlock(); //Console.WriteLine(); 返回Encoding.UTF8.GetString(inStream.ToArray()); //int length=cryptoStream.Read(buffer,0,buffer.length); //Stream outStream=File.OpenWrite(outPath); /*而(长度>0) { 输出流写入(缓冲区,0,长度); 长度=cryptoStream.Read(缓冲区,0,缓冲区.length); }*/ //流内关闭(); //exptream.Close(); } 专用字节[]GetLegalKey(字符串键) { Symmetrialgorithm Symmetrialgorithm=Symmetrialgorithm.Create(); 字符串sTemp=键; 对称算法生成基(); 字节[]bytTemp=对称算法键; int KeyLength=字节长度; 如果(sTemp.Length>KeyLength) sTemp=sTemp.Substring(0,KeyLength); 否则如果(sTemp.LengthIVLength) sTemp=sTemp.子字符串(0,IVLength); 否则,如果(sTemp.Length,c#,C#,这就是问题所在: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.IO; using System.Threading; using System.Net

这就是问题所在:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Threading;
using System.Net;
using System.Net.Sockets;
using System.Security.Cryptography;

namespace GUISimpleTCPClient
{

    public partial class GUISimpleTCPClient : Form
    {

        TcpClient client=null;
        NetworkStream stream;
        StreamReader reader;
        StreamWriter writer;
        public GUISimpleTCPClient()
        {
            InitializeComponent();
        }

        private void Connect_Click(object sender, EventArgs e)
        {
            try
            {
                client=new TcpClient(ipAddress.Text,Convert.ToInt32(port.Text));
                listBox1.Items.Add("Connected to Server");
                stream = client.GetStream();
                reader = new StreamReader(stream);
                writer = new StreamWriter(stream);
                string msg = reader.ReadLine();
                listBox1.Items.Add("Recieved form server:");
                listBox1.Items.Add(msg);
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);
            }

        }

        private void send_Click(object sender, EventArgs e)
        {

            string msg = M.Text;
            SymmetricAlgorithm symmetricAlgorithm = SymmetricAlgorithm.Create();
            byte[] data = Encoding.UTF8.GetBytes("password");


            symmetricAlgorithm.Key = GetLegalKey("PASSWORD");
            symmetricAlgorithm.IV = GetLegalIV();
            byte[] cipher = Encrypt(symmetricAlgorithm, data);
            System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
            msg = enc.GetString(cipher);

           // System.Text.Encoding encoding=new System.Text.Encoding() ;
            System.Text.ASCIIEncoding denc = new System.Text.ASCIIEncoding();
           Byte[] dec_data = denc.GetBytes(msg);
            string decode = Decrypt(symmetricAlgorithm, dec_data);

            try
            {

                writer.WriteLine(msg);
                writer.Flush();
                msg = reader.ReadLine();
                if (msg.Length!=0)
                listBox1.Items.Add(decode);
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);
            }
        }

        private static byte[] Encrypt(SymmetricAlgorithm symmetricAlgorithm,

             byte[] data)
        {

            //   FileStream outStream = File.OpenWrite(outPath);



            ICryptoTransform transform = symmetricAlgorithm.CreateEncryptor();

            MemoryStream t = new MemoryStream();

            CryptoStream cryptoStream =

                new CryptoStream(t, transform, CryptoStreamMode.Write);



            Byte[] inFile = data;
            // File.ReadAllBytes(inPath);



            cryptoStream.Write(data, 0, data.Length);
            cryptoStream.FlushFinalBlock();
            cryptoStream.Close();
            return t.ToArray();

        }



        private static string Decrypt(SymmetricAlgorithm symmetricAlgorithm,

            byte[] cipher)
        {

            ICryptoTransform transform = symmetricAlgorithm.CreateDecryptor();



            // Stream inStream = File.OpenRead(inPath);
            MemoryStream inStream = new MemoryStream();

            CryptoStream cryptoStream =

                new CryptoStream(inStream, transform, CryptoStreamMode.Write);



            Byte[] buffer = new Byte[100];
            cryptoStream.Write(cipher, 0, cipher.Length);
            cryptoStream.FlushFinalBlock();
            //Console.WriteLine();
            return Encoding.UTF8.GetString(inStream.ToArray());


            // int length = cryptoStream.Read(buffer, 0, buffer.Length);



            //  Stream outStream = File.OpenWrite(outPath);

            /* while (length > 0)

             {

                 outStream.Write(buffer, 0, length);

                 length = cryptoStream.Read(buffer, 0, buffer.Length);

             }*/


            //inStream.Close();

            // outStream.Close();

        }

        private byte[] GetLegalKey(string Key)
        {
            SymmetricAlgorithm symmetricAlgorithm = SymmetricAlgorithm.Create();
            string sTemp = Key;
            symmetricAlgorithm.GenerateKey();
            byte[] bytTemp = symmetricAlgorithm.Key;
            int KeyLength = bytTemp.Length;

            if (sTemp.Length > KeyLength)
                sTemp = sTemp.Substring(0, KeyLength);
            else if (sTemp.Length < KeyLength)
                sTemp = sTemp.PadRight(KeyLength, ' ');

            return ASCIIEncoding.ASCII.GetBytes(sTemp);
        }

        private byte[] GetLegalIV()
        {
            string initialIV = "12345678";
            SymmetricAlgorithm symmetricAlgorithm = SymmetricAlgorithm.Create();
            // The initial string of IV may be modified with any data you like
            string sTemp = initialIV;
            symmetricAlgorithm.GenerateIV();
            byte[] bytTemp = symmetricAlgorithm.IV;
            int IVLength = bytTemp.Length;

            if (sTemp.Length > IVLength)
                sTemp = sTemp.Substring(0, IVLength);
            else if (sTemp.Length < IVLength)
                sTemp = sTemp.PadRight(IVLength, ' ');

            return ASCIIEncoding.ASCII.GetBytes(sTemp);
        }
    }
}
永远不要这样做。

加密的数据不是ASCII编码的字符串,所以不要这样对待它。请改用base64。因此,这个片段应该是:

byte[] cipher = Encrypt(symmetricAlgorithm, data);
System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
msg = enc.GetString(cipher);
byte[] cipher = Encrypt(symmetricAlgorithm, data);    
msg = Convert.ToBase64String(cipher);
反之亦然:

byte[] cipher = Encrypt(symmetricAlgorithm, data);
System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
msg = enc.GetString(cipher);
byte[] cipher = Encrypt(symmetricAlgorithm, data);    
msg = Convert.ToBase64String(cipher);
永远不要将不透明的二进制数据视为真正的编码文本。这就像试图加载一个MP3文件,就好像它是一个JPG。不是-你会丢失数据。

你能缩短这个吗