棘手的问题-“WindowsFormsApplication1.Class.DES”的类型初始值设定项引发异常。

sjf_kk 2009-05-01 02:10:57
//俺把加密放在类中。然后在点击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);
}
}
}

...全文
127 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
真相重于对错 2009-05-01
  • 打赏
  • 举报
回复
上面两句,颠倒位置,要不然怎么初始化
真相重于对错 2009-05-01
  • 打赏
  • 举报
回复
private static byte[] plainbytes = Encoding.UTF8.GetBytes(plainText);
private static string plainText = "";
sjf_kk 2009-05-01
  • 打赏
  • 举报
回复
7、8楼可以说清楚点吗?还是不明白?

[Quote=引用 9 楼 jingshuaizh 的回复:]
是不是应该加个 public

public class DES

[/Quote]

回9楼,不是加个public的问题。

期待高人牛人~ ^_^
蓝海D鱼 2009-05-01
  • 打赏
  • 举报
回复
是不是应该加个 public

public class DES
Garnett_KG 2009-05-01
  • 打赏
  • 举报
回复
语句顺序有问题。

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;
}
}


我姓区不姓区 2009-05-01
  • 打赏
  • 举报
回复
通常都是由于静态成员引起的,参考:
http://www.cnblogs.com/kirinboy/archive/2007/11/07/951903.html
mykelly6 2009-05-01
  • 打赏
  • 举报
回复
最好自己断点了调试下吧,肯定是DES类里面哪个对象或者变量没有给初值~
sjf_kk 2009-05-01
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 mjjzg 的回复:]
差了个using System.Security;的命名空间吧
[/Quote]

应该不是吧!

//另外在说明下,编译是OK了,就不知道还差什么,蛮棘手的
mjjzg 2009-05-01
  • 打赏
  • 举报
回复
差了个using System.Security;的命名空间吧
feifeiyiwen 2009-05-01
  • 打赏
  • 举报
回复
同意楼上的观点
mykelly6 2009-05-01
  • 打赏
  • 举报
回复
MemoryStream初始化的时候好像要设定他的容量的。
也就是说你new的时候可以不指定参数,但是还是要补上capacity这个属性的设置的
chyyyin 2009-05-01
  • 打赏
  • 举报
回复
异常报在什么地方?!

111,126

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧