111,126
社区成员
发帖
与我相关
我的任务
分享//俺把加密放在类中。然后在点击Button后出错提示:“WindowsFormsApplication1.Class.DES”的类型初始值设定项引发异常。
using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;
namespace WindowsFormsApplication1.Class
{
class DES
{
private static byte[] cipherbytes = new byte[1];
private static CryptoStream CStream;
private static byte[] finalbytes;
private static MemoryStream MStream = new MemoryStream();
private static byte[] plainbytes = Encoding.UTF8.GetBytes(plainText);
private static string plainText = "";
public static byte[] JM(string JMPuls_Text, byte[] Mykey)
{
SymmetricAlgorithm algorithm = Rijndael.Create();
algorithm.Key = Mykey;
plainText = JMPuls_Text;
algorithm.Mode = CipherMode.ECB;
algorithm.Padding = PaddingMode.Zeros;
byte[] datas = Encoding.UTF8.GetBytes(plainText);
MStream = new MemoryStream();
CStream = new CryptoStream(MStream, algorithm.CreateEncryptor(), CryptoStreamMode.Write);
CStream.Write(datas, 0, datas.Length);
CStream.Close();
cipherbytes = MStream.ToArray();
MStream.Close();
return cipherbytes;
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using WindowsFormsApplication1.Class;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
//DES DES_OBJ = new DES();
public Form1()
{
InitializeComponent();
}
private byte[] mykey = { 1, 2, 1, 2, 1, 2, 2, 1, 2, 1, 1, 2, 2, 2, 1, 2, 1, 2, 1, 2, 2, 1, 2, 2, 1, 2, 1, 2, 1, 2, 1, 2 };
private void button1_Click(object sender, EventArgs e)
{
byte[] ds = DES.JM(this.textBox1.Text,mykey);
this.textBox2.Text = Convert.ToBase64String(ds);
}
}
}
class DES
{
private static byte[] cipherbytes = new byte[1];
private static CryptoStream CStream;
private static byte[] finalbytes;
private static MemoryStream MStream = new MemoryStream();
private static string plainText = ""; //要先给plainText赋值。
private static byte[] plainbytes = Encoding.UTF8.GetBytes(plainText);
public static byte[] JM(string JMPuls_Text, byte[] Mykey)
{
SymmetricAlgorithm algorithm = Rijndael.Create();
algorithm.Key = Mykey;
plainText = JMPuls_Text;
algorithm.Mode = CipherMode.ECB;
algorithm.Padding = PaddingMode.Zeros;
byte[] datas = Encoding.UTF8.GetBytes(plainText);
MStream = new MemoryStream();
CStream = new CryptoStream(MStream, algorithm.CreateEncryptor(), CryptoStreamMode.Write);
CStream.Write(datas, 0, datas.Length);
CStream.Close();
cipherbytes = MStream.ToArray();
MStream.Close();
return cipherbytes;
}
}
//另外在说明下,编译是OK了,就不知道还差什么,蛮棘手的