using System;
using System.IO;
using System.Security;
using System.Security.Cryptography;
using System.Collections.Generic;
using System.Text;namespace ConsoleApplication1
{ public class EncryptionForMe
{
private RijndaelManaged rmCrypto = new RijndaelManaged(); private byte[] key = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08
,0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16 }; private byte[] IV = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08
,0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16 }; public string Encrypt(string strSource)
{
//这里加密的都没问题,问题都在解密那方法里面 byte[] byteSource = Encoding.Unicode.GetBytes(strSource); MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, rmCrypto.CreateEncryptor(key, IV), CryptoStreamMode.Write); //CryptoStream.Write方法把byteSource加密写进ms里.
cs.Write(byteSource, 0, byteSource.Length); string temp = Encoding.Unicode.GetString(ms.GetBuffer(), 0, (int)ms.Length); cs.Close(); return temp;
} public string Decrypt(string strSource)
{ byte[] tempBtye = Encoding.Unicode.GetBytes(strSource); MemoryStream ms = new MemoryStream(tempBtye);
//MSDN: public CryptoStream(Stream stream,ICryptoTransform transform,CryptoStreamMode mode)
CryptoStream cs = new CryptoStream(ms, rmCrypto.CreateDecryptor(key, IV), CryptoStreamMode.Read); //CryptoStream.Read()这个方法怎么用的?
//MSDN: public override int Read(byte[] buffer,int offset,int count) //看说明是把 ms 解压后得到的byte[] 写进 thisByte 那里 可我都不可能知道ms 解压后的实际长度啊?我该怎么才能让它Read进来
byte[] thisByte = new byte[ms.Length]; // <<这试了用byte[] thisByte = new byte[9999];也不行
cs.Read(thisByte, 0, (int)ms.Length); // <<这老报错误 信息是: 填充无效,无法被移除。
cs.Close(); //VS版本:Team System 2008
//.NET版本:2.0 string temp = Encoding.Unicode.GetString(thisByte); return temp;
} } class Program
{
static void Main(string[] args)
{
EncryptionForMe thisEncryption = new EncryptionForMe();
string tempStr = thisEncryption.Encrypt("我要被加密了!呜呜呜");
Console.WriteLine(tempStr);
Console.WriteLine(thisEncryption.Decrypt(tempStr));
}
}
}
using System.IO;
using System.Security;
using System.Security.Cryptography;
using System.Collections.Generic;
using System.Text;namespace ConsoleApplication1
{ public class EncryptionForMe
{
private RijndaelManaged rmCrypto = new RijndaelManaged(); private byte[] key = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08
,0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16 }; private byte[] IV = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08
,0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16 }; public string Encrypt(string strSource)
{
//这里加密的都没问题,问题都在解密那方法里面 byte[] byteSource = Encoding.Unicode.GetBytes(strSource); MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, rmCrypto.CreateEncryptor(key, IV), CryptoStreamMode.Write); //CryptoStream.Write方法把byteSource加密写进ms里.
cs.Write(byteSource, 0, byteSource.Length); string temp = Encoding.Unicode.GetString(ms.GetBuffer(), 0, (int)ms.Length); cs.Close(); return temp;
} public string Decrypt(string strSource)
{ byte[] tempBtye = Encoding.Unicode.GetBytes(strSource); MemoryStream ms = new MemoryStream(tempBtye);
//MSDN: public CryptoStream(Stream stream,ICryptoTransform transform,CryptoStreamMode mode)
CryptoStream cs = new CryptoStream(ms, rmCrypto.CreateDecryptor(key, IV), CryptoStreamMode.Read); //CryptoStream.Read()这个方法怎么用的?
//MSDN: public override int Read(byte[] buffer,int offset,int count) //看说明是把 ms 解压后得到的byte[] 写进 thisByte 那里 可我都不可能知道ms 解压后的实际长度啊?我该怎么才能让它Read进来
byte[] thisByte = new byte[ms.Length]; // <<这试了用byte[] thisByte = new byte[9999];也不行
cs.Read(thisByte, 0, (int)ms.Length); // <<这老报错误 信息是: 填充无效,无法被移除。
cs.Close(); //VS版本:Team System 2008
//.NET版本:2.0 string temp = Encoding.Unicode.GetString(thisByte); return temp;
} } class Program
{
static void Main(string[] args)
{
EncryptionForMe thisEncryption = new EncryptionForMe();
string tempStr = thisEncryption.Encrypt("我要被加密了!呜呜呜");
Console.WriteLine(tempStr);
Console.WriteLine(thisEncryption.Decrypt(tempStr));
}
}
}
我给你发个加密的类 DES加密
//生成随机的密钥和向量
TDCSP.GenerateKey();
TDCSP.GenerateIV(); //在当前的文本框中显示密钥与向量
this.txtKey.Text = Convert.ToBase64String(TDCSP.Key);
this.txtIV.Text = Convert.ToBase64String(TDCSP.IV);
//将密钥与向量赋值给全局变量以储存
this.Key = this.txtKey.Text;
this.IV = this.txtIV.Text;
//使用某种编码方式格式化明文
byte[] tempArray = ASCIIEncoding.ASCII.GetBytes(this.txtMsg1.Text.Trim());//UnicodeEncoding.Unicode.GetBytes(this.txtMsg1.Text.Trim());
//调用3DES加密算法类对象的方法将格式化后的明文数组进行加密
byte[] tempArray2 = TDCSP.CreateEncryptor().TransformFinalBlock(tempArray, 0, tempArray.Length);
//将加密后的数组输出显示
this.txtCphiMsg.Text = Convert.ToBase64String(tempArray2);
}
else
{
//弹出提示信息
MessageBox.Show("您还没有输入需要加密的明文");
//聚焦点至明文输入框
this.txtMsg1.Focus();
return;
}
cs.Write(byteSource, 0, byteSource.Length);// 注意加上这句:
cs.FlushFinalBlock(); // 输出缓冲区中剩余的数据
一样会出错,Index was outside the bounds of the array.
读一位出来都是这个错误,等清醒点再搞
private RijndaelManaged rmCrypto = new RijndaelManaged();
private Encoding TxtEncode= Encoding.UTF8;
private byte[] key = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08
,0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16 };
private byte[] IV = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08
,0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16 };
public string Encrypt(string strSource)
{
byte[] byteSource = TxtEncode.GetBytes(strSource);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, rmCrypto.CreateEncryptor(key, IV), CryptoStreamMode.Write);
cs.Write(byteSource, 0, byteSource.Length);
cs.FlushFinalBlock();
string temp = Convert.ToBase64String(ms.ToArray());
return temp;
}
public string Decrypt(string strSource)
{
byte[] tempBuf = Convert.FromBase64String(strSource);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, rmCrypto.CreateDecryptor(rmCrypto.Key, rmCrypto.IV), CryptoStreamMode.Write);
cs.Write(tempBuf, 0, tempBuf.Length);
cs.FlushFinalBlock();//Error,Padding is invalid and cannot be removed
StreamReader sr = new StreamReader(ms, TxtEncode);
return sr.ReadToEnd();
}
public string Encrypt(string strSource)
{
RijndaelManaged rmCrypto = new RijndaelManaged();
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, rmCrypto.CreateEncryptor(key, IV), CryptoStreamMode.Write);
StreamWriter wr = new StreamWriter(cs);
wr.Write(strSource);
wr.Close();
string temp = Convert.ToBase64String(ms.ToArray());
return temp;
}
public string Decrypt(string strSource)
{
byte[] tempBuf = Convert.FromBase64String(strSource);
MemoryStream ms = new MemoryStream(tempBuf);
CryptoStream cs = new CryptoStream(ms, rmCrypto.CreateDecryptor(key, IV), CryptoStreamMode.Read);
StreamReader sr = new StreamReader(cs);
string ret = sr.ReadToEnd();
sr.Close(); return ret;
}
中的rmCrypto.Key, rmCrypto.IV改为key,IV。 public string Decrypt(string strSource)
{
byte[] tempBuf = Convert.FromBase64String(strSource);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, rmCrypto.CreateDecryptor(key,IV), CryptoStreamMode.Write);
cs.Write(tempBuf, 0, tempBuf.Length);
cs.FlushFinalBlock();//Error,Padding is invalid and cannot be removed
return Encoding.UTF8.GetString(ms.ToArray());
}
public string Decode(string data)
{
if (data.Length <= 0)
{
return "";
}
byte[] byEnc = Convert.FromBase64String(data);
DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
MemoryStream ms = new MemoryStream(byEnc);
CryptoStream cs = new CryptoStream(ms, cryptoProvider.CreateDecryptor(key,IV), CryptoStreamMode.Read);
StreamReader sr = new StreamReader(cst);
return sr.ReadToEnd();
}
我是用Substring() + IndexOf()处理掉'\0'的问题现在基本没问题了!