PK.Key = curkey;
PK.IV = curiv;
PK.KeySize = 64;
MemoryStream OMS = new MemoryStream(Content);
CryptoStream encStream = new CryptoStream(OMS, PK.CreateDecryptor(), CryptoStreamMode.Read);
// Create a StreamReader for reading the stream.
StreamReader sr = new StreamReader(encStream);
// Read the stream as a string.
string val = sr.ReadLine();
确已确定 KEY和 IV是一致的
但是执行的READLINE的时候报错 "无效的数据" 往回查找,上边关于 encStream 有异常:Length 引发了异常等等.微软自己的例子都出错啊!
搜了一下贴,也有几个人出现问题. 但是没找到答案.
是不是我要加解秘的内容要保证长度?(目前测试是10个字符左右.以后可能会加密1-20个字符内容的情况比较多.)解决就给分.
=================向四川的人民问候. 祝他们顺利度过困难时期.
PK.IV = curiv;
PK.KeySize = 64;
MemoryStream OMS = new MemoryStream(Content);
CryptoStream encStream = new CryptoStream(OMS, PK.CreateDecryptor(), CryptoStreamMode.Read);
// Create a StreamReader for reading the stream.
StreamReader sr = new StreamReader(encStream);
// Read the stream as a string.
string val = sr.ReadLine();
确已确定 KEY和 IV是一致的
但是执行的READLINE的时候报错 "无效的数据" 往回查找,上边关于 encStream 有异常:Length 引发了异常等等.微软自己的例子都出错啊!
搜了一下贴,也有几个人出现问题. 但是没找到答案.
是不是我要加解秘的内容要保证长度?(目前测试是10个字符左右.以后可能会加密1-20个字符内容的情况比较多.)解决就给分.
=================向四川的人民问候. 祝他们顺利度过困难时期.
using System.IO;
using System.Text;
using System.Security.Cryptography;public class RijndaelSimple
{
public static string Encrypt(string plainText,
string passPhrase,
string saltValue,
string hashAlgorithm,
int passwordIterations,
string initVector,
int keySize)
{
byte[] initVectorBytes = Encoding.ASCII.GetBytes(initVector);
byte[] saltValueBytes = Encoding.ASCII.GetBytes(saltValue);
byte[] plainTextBytes = Encoding.UTF8.GetBytes(plainText);
PasswordDeriveBytes password = new PasswordDeriveBytes(
passPhrase,
saltValueBytes,
hashAlgorithm,
passwordIterations);
byte[] keyBytes = password.GetBytes(keySize / 8);
// Create uninitialized Rijndael encryption object.
RijndaelManaged symmetricKey = new RijndaelManaged();
symmetricKey.Mode = CipherMode.CBC;
ICryptoTransform encryptor = symmetricKey.CreateEncryptor(
keyBytes,
initVectorBytes);
// Define memory stream which will be used to hold encrypted data.
MemoryStream memoryStream = new MemoryStream();
// Define cryptographic stream (always use Write mode for encryption).
CryptoStream cryptoStream = new CryptoStream(memoryStream,
encryptor,
CryptoStreamMode.Write);
// Start encrypting.
cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length);
// Finish encrypting.
cryptoStream.FlushFinalBlock(); // Convert our encrypted data from a memory stream into a byte array.
byte[] cipherTextBytes = memoryStream.ToArray();
// Close both streams.
memoryStream.Close();
cryptoStream.Close();
// Convert encrypted data into a base64-encoded string.
string cipherText = Convert.ToBase64String(cipherTextBytes);
// Return encrypted string.
return cipherText;
} public static string Decrypt(string cipherText,
string passPhrase,
string saltValue,
string hashAlgorithm,
int passwordIterations,
string initVector,
int keySize)
{
byte[] initVectorBytes = Encoding.ASCII.GetBytes(initVector);
byte[] saltValueBytes = Encoding.ASCII.GetBytes(saltValue);
// Convert our ciphertext into a byte array.
byte[] cipherTextBytes = Convert.FromBase64String(cipherText);
PasswordDeriveBytes password = new PasswordDeriveBytes(
passPhrase,
saltValueBytes,
hashAlgorithm,
passwordIterations);
byte[] keyBytes = password.GetBytes(keySize / 8);
// Create uninitialized Rijndael encryption object.
RijndaelManaged symmetricKey = new RijndaelManaged();
symmetricKey.Mode = CipherMode.CBC;
ICryptoTransform decryptor = symmetricKey.CreateDecryptor(
keyBytes,
initVectorBytes);
// Define memory stream which will be used to hold encrypted data.
MemoryStream memoryStream = new MemoryStream(cipherTextBytes);
// Define cryptographic stream (always use Read mode for encryption).
CryptoStream cryptoStream = new CryptoStream(memoryStream,
decryptor,
CryptoStreamMode.Read); // Since at this point we don't know what the size of decrypted data
// will be, allocate the buffer long enough to hold ciphertext;
// plaintext is never longer than ciphertext.
byte[] plainTextBytes = new byte[cipherTextBytes.Length];
// Start decrypting.
int decryptedByteCount = cryptoStream.Read(plainTextBytes,
0,
plainTextBytes.Length);
// Close both streams.
memoryStream.Close();
cryptoStream.Close();
// Convert decrypted data into a string.
// Let us assume that the original plaintext string was UTF8-encoded.
string plainText = Encoding.UTF8.GetString(plainTextBytes,
0,
decryptedByteCount);
// Return decrypted string.
return plainText;
}
}/// <summary>
/// Illustrates the use of RijndaelSimple class to encrypt and decrypt data.
/// </summary>
public class RijndaelSimpleTest
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
string plainText = "Hello, World!"; // original plaintext
string passPhrase = "Pas5pr@se"; // can be any string
string saltValue = "s@1tValue"; // can be any string
string hashAlgorithm = "SHA1"; // can be "MD5"
int passwordIterations = 2; // can be any number
string initVector = "@1B2c3D4e5F6g7H8"; // must be 16 bytes
int keySize = 256; // can be 192 or 128
Console.WriteLine(String.Format("Plaintext : {0}", plainText)); string cipherText = RijndaelSimple.Encrypt(plainText,
passPhrase,
saltValue,
hashAlgorithm,
passwordIterations,
initVector,
keySize); Console.WriteLine(String.Format("Encrypted : {0}", cipherText));
plainText = RijndaelSimple.Decrypt(cipherText,
passPhrase,
saltValue,
hashAlgorithm,
passwordIterations,
initVector,
keySize); Console.WriteLine(String.Format("Decrypted : {0}", plainText));
}
}
FileMode.OpenOrCreate,FileAccess.Write);DESCryptoServiceProvider cryptic = new DESCryptoServiceProvider();cryptic.Key = ASCIIEncoding.ASCII.GetBytes(“ABCDEFGH”);
cryptic.IV = ASCIIEncoding.ASCII.GetBytes(“ABCDEFGH”);CryptoStream crStream = new CryptoStream(stream,
cryptic.CreateEncryptor(),CryptoStreamMode.Write);
byte[] data = ASCIIEncoding.ASCII.GetBytes(“Hello World!”);crStream.Write(data,0,data.Length);crStream.Close();
stream.Close();FileStream stream = new FileStream(“C:\\test.txt”,
FileMode.Open,FileAccess.Read);DESCryptoServiceProvider cryptic = new DESCryptoServiceProvider();cryptic.Key = ASCIIEncoding.ASCII.GetBytes(“ABCDEFGH”);
cryptic.IV = ASCIIEncoding.ASCII.GetBytes(“ABCDEFGH”);CryptoStream crStream = new CryptoStream(stream,
cryptic.CreateDecryptor(),CryptoStreamMode.Read);StreamReader reader = new StreamReader(crStream);string data = reader.ReadToEnd();reader.Close();
stream.Close();我的代码怎么没错误.....
是长度 还是编码问题?
8个字符吗?
给个例子LZ看看吧。 const string KEY_64 = "WellTech";//是8个字符,64位,加密钥 const string IV_64 = "WellTech";
//字符串加密
public string Encode(string data,string KEY_64,string IV_64)
{
byte[] byKey = ASCIIEncoding.ASCII.GetBytes(KEY_64);
byte[] byIV = ASCIIEncoding.ASCII.GetBytes(IV_64); DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
int i = cryptoProvider.KeySize;
MemoryStream ms = new MemoryStream();
CryptoStream cst = new CryptoStream(ms, cryptoProvider.CreateEncryptor(byKey, byIV), CryptoStreamMode.Write);
StreamWriter sw = new StreamWriter(cst);
sw.Write(data);
sw.Flush();
cst.FlushFinalBlock();
sw.Flush();
return Convert.ToBase64String(ms.GetBuffer(), 0, (int)ms.Length); }
//字符串解密
public string Decode(string data, string KEY_64, string IV_64)
{
byte[] byKey = System.Text.ASCIIEncoding.ASCII.GetBytes(KEY_64);
byte[] byIV = System.Text.ASCIIEncoding.ASCII.GetBytes(IV_64); byte[] byEnc;
try
{
byEnc = Convert.FromBase64String(data);
}
catch
{
return null;
} DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
MemoryStream ms = new MemoryStream(byEnc);
CryptoStream cst = new CryptoStream(ms, cryptoProvider.CreateDecryptor(byKey, byIV), CryptoStreamMode.Read);
StreamReader sr = new StreamReader(cst);
return sr.ReadToEnd();
}
string val = sr.ReadLine();
确已确定 KEY和 IV是一致的
但是执行的READLINE的时候报错 "无效的数据" 往回查找,上边关于 encStream 有异常:Length 引发了异常等等. 微软自己的例子都出错啊!
搜了一下贴,也有几个人出现问题. 但是没找到答案.
private static byte[] Keys ={ 0x12, 0x34, 0x56, 0x78, 0xAB, 0xCD, 0xEF,0x39 };
public string DecryptDES(string decryptString, string decryptKey)
{
try
{
byte[] rgbKey = Encoding.UTF8.GetBytes(decryptKey);
byte[] rgbIV = Keys;
byte[] inputByteArray = Convert.FromBase64String(decryptString);
DESCryptoServiceProvider DCSP = new DESCryptoServiceProvider();
MemoryStream mStream = new MemoryStream();
CryptoStream cStream = new CryptoStream(mStream, DCSP.CreateDecryptor(rgbKey, rgbIV), CryptoStreamMode.Write); cStream.Write(inputByteArray, 0, inputByteArray.Length);
cStream.FlushFinalBlock();
return Encoding.UTF8.GetString(mStream.ToArray());
}
catch
{
return decryptString;
}
}
/// 加解密数据
/// </summary>
/// <param name="key">密钥</param>
/// <param name="source">源数据</param>
/// <param name="cryptKind">加解密类型</param>
/// <returns></returns>
public static string Docrypt(string key, string source, CryptKind cryptKind)
{ if (key.Trim() == null || source.Trim() == null)
{
return "";
}
string Resulte = string.Empty;
byte[] _Key = Encoding.UTF8.GetBytes(Md5Str16(key));
byte[] _Iv = Encoding.UTF8.GetBytes(Md5Str16(Md5Str32(key)));
byte[] Sources = null;
RijndaelManaged rjMananger = new RijndaelManaged();
try
{
switch (cryptKind)
{
//加密
case CryptKind.Encrypt: Sources = Encoding.UTF8.GetBytes(source);
MemoryStream ms1 = new MemoryStream();
CryptoStream cs1 = new CryptoStream(ms1, rjMananger.CreateEncryptor(_Key, _Iv), CryptoStreamMode.Write); cs1.Write(Sources, 0, Sources.Length);
cs1.FlushFinalBlock(); Resulte = Convert.ToBase64String(ms1.ToArray());
break;
//解密
case CryptKind.Decrypt: Sources = Convert.FromBase64String(source);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, rjMananger.CreateDecryptor(_Key, _Iv), CryptoStreamMode.Write); cs.Write(Sources, 0, Sources.Length);
cs.FlushFinalBlock(); Resulte = Encoding.UTF8.GetString(ms.ToArray());
break;
}
}
catch(Exception ex)
{ }
return Resulte; }
在解密方面.
MS的例子是用加密的BYTE[]作源,弄个流,用解码器去读.
而楼上有几位用的却是用解码器写数据.
我晕 真神奇
我去试下这个