rsa加密解密问题,急!

khpcg 2004-08-17 04:27:01
我在一本书上看到一个小的rsa加密和解密程序代码如下:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Security.Cryptography;
using System.Text;
using System.IO;
using System.Xml;


namespace Cryptography1
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>

public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.Button button4;

private System.Windows.Forms.Button Encrypt;
private System.Windows.Forms.Button Decrypt;
private System.Windows.Forms.TextBox txtClearText;
private System.Windows.Forms.TextBox txtCipherText;
private System.Windows.Forms.TextBox txtPubKey;
private System.Windows.Forms.TextBox txtPrvKey;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
static RSACryptoServiceProvider objRSAProvider=new RSACryptoServiceProvider();

public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();

//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}

/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.txtClearText = new System.Windows.Forms.TextBox();
this.txtCipherText = new System.Windows.Forms.TextBox();
this.Encrypt = new System.Windows.Forms.Button();
this.Decrypt = new System.Windows.Forms.Button();
this.label3 = new System.Windows.Forms.Label();
this.label4 = new System.Windows.Forms.Label();
this.txtPubKey = new System.Windows.Forms.TextBox();
this.txtPrvKey = new System.Windows.Forms.TextBox();
this.button3 = new System.Windows.Forms.Button();
this.button4 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// label1
//
this.label1.Location = new System.Drawing.Point(0, 32);
this.label1.Name = "label1";
this.label1.TabIndex = 0;
this.label1.Text = "Clear Text:";
//
// label2
//
this.label2.Location = new System.Drawing.Point(0, 64);
this.label2.Name = "label2";
this.label2.TabIndex = 1;
this.label2.Text = "Cipher Text:";
//
// txtClearText
//
this.txtClearText.Location = new System.Drawing.Point(104, 32);
this.txtClearText.Name = "txtClearText";
this.txtClearText.Size = new System.Drawing.Size(496, 21);
this.txtClearText.TabIndex = 2;
this.txtClearText.Text = "";
//
// txtCipherText
//
this.txtCipherText.Location = new System.Drawing.Point(104, 64);
this.txtCipherText.Multiline = true;
this.txtCipherText.Name = "txtCipherText";
this.txtCipherText.Size = new System.Drawing.Size(496, 56);
this.txtCipherText.TabIndex = 3;
this.txtCipherText.Text = "";
//
// Encrypt
//
this.Encrypt.Location = new System.Drawing.Point(608, 32);
this.Encrypt.Name = "Encrypt";
this.Encrypt.TabIndex = 4;
this.Encrypt.Text = "Encrypt";
this.Encrypt.Click += new System.EventHandler(this.Encrypt_Click);
//
// Decrypt
//
this.Decrypt.Location = new System.Drawing.Point(616, 80);
this.Decrypt.Name = "Decrypt";
this.Decrypt.TabIndex = 5;
this.Decrypt.Text = "Decrypt";
this.Decrypt.Click += new System.EventHandler(this.Decrypt_Click);
//
// label3
//
this.label3.Location = new System.Drawing.Point(0, 128);
this.label3.Name = "label3";
this.label3.TabIndex = 6;
this.label3.Text = "Public Key:";
//
// label4
//
this.label4.Location = new System.Drawing.Point(0, 232);
this.label4.Name = "label4";
this.label4.TabIndex = 7;
this.label4.Text = "Private Key:";
//
// txtPubKey
//
this.txtPubKey.Location = new System.Drawing.Point(0, 152);
this.txtPubKey.Multiline = true;
this.txtPubKey.Name = "txtPubKey";
this.txtPubKey.Size = new System.Drawing.Size(696, 72);
this.txtPubKey.TabIndex = 8;
this.txtPubKey.Text = "";
//
// txtPrvKey
//
this.txtPrvKey.Location = new System.Drawing.Point(0, 256);
this.txtPrvKey.Multiline = true;
this.txtPrvKey.Name = "txtPrvKey";
this.txtPrvKey.Size = new System.Drawing.Size(696, 88);
this.txtPrvKey.TabIndex = 9;
this.txtPrvKey.Text = "";
//
// button3
//
this.button3.Location = new System.Drawing.Point(544, 120);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(144, 23);
this.button3.TabIndex = 10;
this.button3.Text = "Save Public Key As...";
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// button4
//
this.button4.Location = new System.Drawing.Point(536, 232);
this.button4.Name = "button4";
this.button4.Size = new System.Drawing.Size(152, 23);
this.button4.TabIndex = 11;
this.button4.Text = "Save Private Key As...";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(704, 349);
this.Controls.Add(this.button4);
this.Controls.Add(this.button3);
this.Controls.Add(this.txtPrvKey);
this.Controls.Add(this.txtPubKey);
this.Controls.Add(this.label4);
this.Controls.Add(this.label3);
this.Controls.Add(this.Decrypt);
this.Controls.Add(this.Encrypt);
this.Controls.Add(this.txtCipherText);
this.Controls.Add(this.txtClearText);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void Encrypt_Click(object sender, System.EventArgs e)
{
if(txtClearText.Text.Trim()!="")
{
RSACryptoServiceProvider rsaProvider=new RSACryptoServiceProvider();

UnicodeEncoding utf8=new UnicodeEncoding();
byte[] clearText=utf8.GetBytes(txtClearText.Text.Trim());

txtCipherText.Text=Convert.ToBase64String(rsaProvider.Encrypt(clearText,false));

txtPrvKey.Text=rsaProvider.ToXmlString(true);

txtPubKey.Text=rsaProvider.ToXmlString(false);
}

}

private void Decrypt_Click(object sender, System.EventArgs e)
{
if(txtCipherText.Text.Trim()!="")
{

byte[] bCipherText=Convert.FromBase64String(txtCipherText.Text.Trim());
byte[] CypherTextBArray;
CypherTextBArray =objRSAProvider.
(bCipherText,false);
string strValue=ASCIIEncoding.ASCII.GetString(CypherTextBArray);
//string strValue=(new UnicodeEncoding()).GetString(CypherTextBArray);
//string strValue=UnicodeEncoding.Unicode.GetString(objRSAProvider.Decrypt(bCipherText,false));

MessageBox.Show(this,strValue,"Decrypted value",MessageBoxButtons.OK,MessageBoxIcon.Information);
}

}

但是每次解密时执行到string strValue=ASCIIEncoding.ASCII.GetString(CypherTextBArray);
这一句就报错:不正确数据
请各位大侠指点
...全文
128 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
khpcg 2004-08-19
  • 打赏
  • 举报
回复
用ASP.NET自带的不需要买什么类包吗
直接调用那些类行吗?
我这个程序是抄的书上的
但是编译时在解密的时候报错
khpcg 2004-08-18
  • 打赏
  • 举报
回复
急啊
mainone 2004-08-17
  • 打赏
  • 举报
回复
http://community.csdn.net/Expert/topic/3279/3279664.xml?temp=.6025507
khpcg 2004-08-17
  • 打赏
  • 举报
回复
在线等啊
canoe_eyes 2004-08-17
  • 打赏
  • 举报
回复
UP
【课程介绍】     课程目标:             - 有状态登录和无状态登录的区别             - 常见的非对称加密算法和非对称的加密方式             - 老版本只使用jwt进行加密的弊端             - 授权中心的授权流程             - 如何整合网关组件实现jwt安全验证             - 理解什么是公钥什么是私钥      - 深刻理解授权流程什么是有状态? 有状态服务,即服务端需要记录每次会话的客户端信息,从而识别客户端身份,根据用户身份进行请求的处理,典型的设计如tomcat中的session。例如登录:用户登录后,我们把登录者的信息保存在服务端session中,并且给用户一个cookie值,记录对应的session。然后下次请求,用户携带cookie值来,我们就能识别到对应session,从而找到用户的信息。缺点是什么?- 服务端保存大量数据,增加服务端压力- 服务端保存用户状态,无法进行水平扩展- 客户端请求依赖服务端,多次请求必须访问同一台服务器。什么是无状态? 微服务集群中的每个服务,对外提供的都是Rest风格的接口。而Rest风格的一个最重要的规范就是:服务的无状态性,即:- 服务端不保存任何客户端请求者信息- 客户端的每次请求必须具备自描述信息,通过这些信息识别客户端身份带来的好处是什么呢?- 客户端请求不依赖服务端的信息,任何多次请求不需要必须访问到同一台服务- 服务端的集群和状态对客户端透明- 服务端可以任意的迁移和伸缩- 减小服务端存储压力

110,545

社区成员

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

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

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