| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1567 人关注过本帖
标题:[求助]加密解密问题???????
只看楼主 加入收藏
liyachi
Rank: 1
等 级:新手上路
帖 子:70
专家分:0
注 册:2006-10-31
收藏
 问题点数:0 回复次数:2 
[求助]加密解密问题???????

Rijndael crypt=Rijndael.Create();

public void btn_jiami_Click(object sender,System.EventArgs e)
{
//加密
// ICryptoTransform transform=crypt.CreateEncryptor();
// FileStream fs=new FileStream("H:\\testfile.txt",FileMode.Create);
// CryptoStream cs=new CryptoStream(fs,transform,CryptoStreamMode.Write);
// StreamWriter sw=new StreamWriter(cs);
// sw.Write(this.txt_Text.Text);
// sw.Flush();
// cs.FlushFinalBlock();
// sw.Close();
// this.txt_Text.Text="";

FileStream fs=new FileStream("H:\\test.txt",FileMode.Create);
ICryptoTransform transformEncode=new ToBase64Transform();
CryptoStream csEncode=new CryptoStream(fs,transformEncode,CryptoStreamMode.Write);
ICryptoTransform transformEncrypt=crypt.CreateEncryptor();
CryptoStream csEncrypt=new CryptoStream(csEncode,transformEncrypt,CryptoStreamMode.Write);
StreamWriter w=new StreamWriter(csEncrypt);
w.Write(this.txt_Text.Text);
w.Flush();
csEncrypt.FlushFinalBlock();
w.Close();
this.txt_Text.Text="";
}
public void btn_jiemi_Click(object sender,System.EventArgs e)
{
//解密
// ICryptoTransform transform=crypt.CreateDecryptor();
// FileStream fs=new FileStream("H:\\testfile.txt",FileMode.Open);
// CryptoStream cs=new CryptoStream(fs,transform,CryptoStreamMode.Read);
// StreamReader sr=new StreamReader(cs);
// string text=sr.ReadToEnd();
// sr.Close();
// this.txt_Text.Text=text;

FileStream fs=new FileStream("H:\\test.txt",FileMode.Open);
ICryptoTransform transformDecode=new FromBase64Transform();
CryptoStream csDecode=new CryptoStream(fs,transformDecode,CryptoStreamMode.Read);
ICryptoTransform transformDecrypt=crypt.CreateDecryptor();
CryptoStream csDecrypt=new CryptoStream(csDecode,transformDecrypt,CryptoStreamMode.Read);
StreamReader r=new StreamReader(csDecrypt);
string text=r.ReadToEnd();
this.txt_Text.Text=""+text+"";
}

出现以下问题,是那里出错了,麻烦各位了

PKCS7 填充无效,无法被移除。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。

异常详细信息: System.Security.Cryptography.CryptographicException: PKCS7 填充无效,无法被移除。

源错误:


行 70: CryptoStream csDecrypt=new CryptoStream(csDecode,transformDecrypt,CryptoStreamMode.Read);
行 71: StreamReader r=new StreamReader(csDecrypt);
行 72: string text=r.ReadToEnd();
行 73: this.txt_Text.Text=""+text+"";
行 74: }

搜索更多相关主题的帖子: 解密 
2007-06-01 10:40
skyland84
Rank: 2
等 级:新手上路
威 望:4
帖 子:544
专家分:0
注 册:2006-10-9
收藏
得分:0 

我现在懒得看代码了!
不看看你使用加密时 是否对称处理了!

我昨天就搞错了这点 就一直出不来!

自己调试下吧~!


决定人生~
2007-06-01 10:55
rainic
Rank: 6Rank: 6
等 级:贵宾
威 望:27
帖 子:2367
专家分:0
注 册:2005-8-9
收藏
得分:0 

我写过VB的,2003的

Imports System.Security.Cryptography
Imports System.IO
Imports System.Text

Public Class Secret
Protected des As DESCryptoServiceProvider
Protected IsDesInit = False 'DES是否已经初始化

Protected Sub DESInit() 'DES初始化
des = New DESCryptoServiceProvider
Dim strKey As String
Dim keypath As String = Application.StartupPath & "\DesKey"
If File.Exists(keypath) Then
Dim sr As StreamReader = File.OpenText(keypath)
strKey = sr.ReadLine()
sr.Close()
End If
If strKey Is Nothing Then
strKey = "RainChan"
ElseIf strKey.Length <> 8 Then
strKey = "RainChan"
End If
des.Key = ASCIIEncoding.Default.GetBytes(strKey)
des.IV = des.Key
IsDesInit = True
End Sub

Public Function Encrypt(ByVal originaltext As String) As String
If IsDesInit = False Then
DESInit()
End If
Dim inputByteArray() As Byte
inputByteArray = Encoding.Default.GetBytes(originaltext)
Dim ms As New System.IO.MemoryStream
Dim cs As New CryptoStream(ms, des.CreateEncryptor, CryptoStreamMode.Write)
cs.Write(inputByteArray, 0, inputByteArray.Length)
cs.FlushFinalBlock()
cs.Close()
Dim sb As New StringBuilder
For Each b As Byte In ms.ToArray()
sb.AppendFormat("{0:X2}", b)
Next
ms.Close()
Return sb.ToString()
End Function

Public Function Decrypt(ByVal ciphertext As String) As String
If IsDesInit = False Then
DESInit()
End If
Dim len As Integer
len = ciphertext.Length / 2 - 1
Dim inputByteArray(len) As Byte
Dim x, i As Integer
For x = 0 To len
i = Convert.ToInt32(ciphertext.Substring(x * 2, 2), 16)
inputByteArray(x) = CType(i, Byte)
Next
Dim ms As New System.IO.MemoryStream
Dim cs As New CryptoStream(ms, des.CreateDecryptor, CryptoStreamMode.Write)
cs.Write(inputByteArray, 0, inputByteArray.Length)
cs.FlushFinalBlock()
cs.Close()
Dim originaltext As String
originaltext = Encoding.Default.GetString(ms.ToArray)
ms.Close()
Return originaltext
End Function

Public Function FileEncrypt(ByVal inFile As String, ByVal outFile As String) As Boolean
Return file_en_de(inFile, outFile, "en")
End Function

Public Function FileDecrypt(ByVal inFile As String, ByVal outFile As String) As Boolean
Return file_en_de(inFile, outFile, "de")
End Function

Protected Function file_en_de(ByVal inFile As String, ByVal outFile As String, ByVal mode As String) As Boolean
If IsDesInit = False Then
DESInit()
End If
Dim ok As Boolean = True
Dim fin As New FileStream(inFile, FileMode.Open, FileAccess.Read)
Dim fout As New FileStream(outFile, FileMode.OpenOrCreate, FileAccess.Write)
fout.SetLength(0)
'Create variables to help with read and write.
Dim bin(4096) As Byte 'This is intermediate storage for the encryption.
Dim rdlen As Long = 0 'This is the total number of bytes written.
Dim totlen As Long = fin.Length 'Total length of the input file.
Dim len As Integer 'This is the number of bytes to be written at a time.
Try
Dim encStream As CryptoStream
If mode = "en" Then
encStream = New CryptoStream(fout, des.CreateEncryptor, CryptoStreamMode.Write)
Else
encStream = New CryptoStream(fout, des.CreateDecryptor, CryptoStreamMode.Write)
End If
'Read from the input file, then encrypt(decrypt) and write to the output file.
While rdlen < totlen
len = fin.Read(bin, 0, 4096)
encStream.Write(bin, 0, len)
rdlen = Convert.ToInt32(rdlen + len / des.BlockSize * des.BlockSize)
End While
encStream.Close()
fout.Close()
Catch ex As Exception
ok = False
fout.Close()
File.Delete(outFile)
Finally
fin.Close()
End Try
Return ok
End Function

End Class


2007-06-01 11:38
快速回复:[求助]加密解密问题???????
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.019978 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved