我先是按照网上的一些范例中规中矩的改了一个,大概就是从文件流中读出一段数据,然
后直接对这段数据整体加密或者解密,然后再写入新文件,成功。
见这个链接的方法6:http://www.cnblogs.com/zengxiangzhan/archive/2010/01/30/1659687.html但后来我自己设计了一个。就是把加解密专门做成一个单独的模块,对一个任意长的字符串
进行加解密,对这个字符串可以一段一段的处理。而这个字符串又是文件流中的一部分。
但这样就不对的,结果错误,特点为解密结果中夹杂着正确的结果和乱码。我摸着石头试着从很多角度修改,但就是不行
谁帮我分析一下?当然大的思路不能变,起码方法二思路没问题吧。我把整个可执行的代码附上。
using System;
using System.IO;
using System.Security.Cryptography;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace DecryptFile
{
class DES2StepEncryptDecrypt
{
static void Main()
{
Console.WriteLine("Encrypt and decrypt files....\n"); string rawFilePath;
int index;
string encryptedFilePath;
string decryptFilePath;
string temp;
string DESKey;
string DESVector; DESKey = "jieqichunai";
DESVector = "h54r34eoiwuro34uiu3u5o43u5oi3u4h5k43h5";
rawFilePath = @"Test1.txt";
index = rawFilePath.LastIndexOf('.');
temp = rawFilePath.Substring(0, index);
encryptedFilePath = temp + ".encrypt";
EncryptFileWithDESFS(rawFilePath, encryptedFilePath, DESKey, DESVector);
decryptFilePath = temp + "_Decrypt.txt";
DecryptFileWithDESFS(encryptedFilePath, decryptFilePath, DESKey, DESVector);
}
private static void EncryptFileWithDESFS(String inName, String outName, string DESKey, string DESVector)
{
FileStream reader = new FileStream(inName, FileMode.Open, FileAccess.Read);
FileStream writer = new FileStream(outName, FileMode.OpenOrCreate, FileAccess.Write); int BlockLength = 20;
byte[] bin = new byte[BlockLength]; //This is intermediate storage for the encryption.
int rdlen = 0; //This is the total number of bytes written.
long totlen = reader.Length; //This is the total length of the input file.
int len; //This is the number of bytes to be written at a time. //Read from the input file, then encrypt and write to the output file.
while (rdlen < totlen)
{
bin = new byte[100]; len = reader.Read(bin, 0, BlockLength); byte[] outData = EncryptDESByte(bin, DESKey); writer.Write(outData, 0, outData.Length); rdlen = rdlen + len;
} reader.Close();
writer.Close();
} private static void DecryptFileWithDESFS(String inName, String outName, string DESKey, string DESVector)
{
FileStream reader = new FileStream(inName, FileMode.Open, FileAccess.Read);
FileStream writer = new FileStream(outName, FileMode.OpenOrCreate, FileAccess.Write); int BlockLength = 20;
byte[] bin = new byte[BlockLength]; //This is intermediate storage for the encryption.
int rdlen = 0; //This is the total number of bytes written.
long totlen = reader.Length; //This is the total length of the input file.
int len; //This is the number of bytes to be written at a time. //Read from the input file, then encrypt and write to the output file.
while (rdlen < totlen)
{
len = reader.Read(bin, 0, BlockLength); byte[] outData = DecryptDESByte(bin, DESKey); writer.Write(outData, 0, outData.Length); rdlen = rdlen + BlockLength;
} reader.Close();
writer.Close();
} //默认密钥向量 private static byte[] Keys = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
public static byte[] EncryptDESByte(byte[] encryptString, string encryptKey)
{ try
{ byte[] rgbKey = new byte[8];
Array.Copy(Encoding.UTF8.GetBytes(encryptKey.PadRight(rgbKey.Length)), rgbKey, rgbKey.Length); byte[] rgbIV = Keys; byte[] inputByteArray = encryptString.ToArray(); DESCryptoServiceProvider dCSP = new DESCryptoServiceProvider(); MemoryStream mStream = new MemoryStream(); CryptoStream cStream = new CryptoStream(mStream, dCSP.CreateEncryptor(rgbKey, rgbIV), CryptoStreamMode.Write); cStream.Write(inputByteArray, 0, inputByteArray.Length); cStream.FlushFinalBlock(); return mStream.ToArray(); } catch
{
return encryptString;
} }
/// <summary> /// DES解密字符串 /// </summary> /// <param name="decryptString">待解密的字符串</param> /// <param name="decryptKey">解密密钥,要求为8位,和加密密钥相同</param> /// <returns>解密成功返回解密后的字符串,失败返源串</returns> public static byte[] DecryptDESByte(byte[] decryptString, string decryptKey)
{ try
{
byte[] rgbKey = Encoding.UTF8.GetBytes(decryptKey.PadRight(8).Substring(0,8)); byte[] rgbIV = Keys; byte[] inputByteArray = decryptString.ToArray(); 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 mStream.ToArray(); } catch
{
return decryptString;
} }
}
}
后直接对这段数据整体加密或者解密,然后再写入新文件,成功。
见这个链接的方法6:http://www.cnblogs.com/zengxiangzhan/archive/2010/01/30/1659687.html但后来我自己设计了一个。就是把加解密专门做成一个单独的模块,对一个任意长的字符串
进行加解密,对这个字符串可以一段一段的处理。而这个字符串又是文件流中的一部分。
但这样就不对的,结果错误,特点为解密结果中夹杂着正确的结果和乱码。我摸着石头试着从很多角度修改,但就是不行
谁帮我分析一下?当然大的思路不能变,起码方法二思路没问题吧。我把整个可执行的代码附上。
using System;
using System.IO;
using System.Security.Cryptography;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace DecryptFile
{
class DES2StepEncryptDecrypt
{
static void Main()
{
Console.WriteLine("Encrypt and decrypt files....\n"); string rawFilePath;
int index;
string encryptedFilePath;
string decryptFilePath;
string temp;
string DESKey;
string DESVector; DESKey = "jieqichunai";
DESVector = "h54r34eoiwuro34uiu3u5o43u5oi3u4h5k43h5";
rawFilePath = @"Test1.txt";
index = rawFilePath.LastIndexOf('.');
temp = rawFilePath.Substring(0, index);
encryptedFilePath = temp + ".encrypt";
EncryptFileWithDESFS(rawFilePath, encryptedFilePath, DESKey, DESVector);
decryptFilePath = temp + "_Decrypt.txt";
DecryptFileWithDESFS(encryptedFilePath, decryptFilePath, DESKey, DESVector);
}
private static void EncryptFileWithDESFS(String inName, String outName, string DESKey, string DESVector)
{
FileStream reader = new FileStream(inName, FileMode.Open, FileAccess.Read);
FileStream writer = new FileStream(outName, FileMode.OpenOrCreate, FileAccess.Write); int BlockLength = 20;
byte[] bin = new byte[BlockLength]; //This is intermediate storage for the encryption.
int rdlen = 0; //This is the total number of bytes written.
long totlen = reader.Length; //This is the total length of the input file.
int len; //This is the number of bytes to be written at a time. //Read from the input file, then encrypt and write to the output file.
while (rdlen < totlen)
{
bin = new byte[100]; len = reader.Read(bin, 0, BlockLength); byte[] outData = EncryptDESByte(bin, DESKey); writer.Write(outData, 0, outData.Length); rdlen = rdlen + len;
} reader.Close();
writer.Close();
} private static void DecryptFileWithDESFS(String inName, String outName, string DESKey, string DESVector)
{
FileStream reader = new FileStream(inName, FileMode.Open, FileAccess.Read);
FileStream writer = new FileStream(outName, FileMode.OpenOrCreate, FileAccess.Write); int BlockLength = 20;
byte[] bin = new byte[BlockLength]; //This is intermediate storage for the encryption.
int rdlen = 0; //This is the total number of bytes written.
long totlen = reader.Length; //This is the total length of the input file.
int len; //This is the number of bytes to be written at a time. //Read from the input file, then encrypt and write to the output file.
while (rdlen < totlen)
{
len = reader.Read(bin, 0, BlockLength); byte[] outData = DecryptDESByte(bin, DESKey); writer.Write(outData, 0, outData.Length); rdlen = rdlen + BlockLength;
} reader.Close();
writer.Close();
} //默认密钥向量 private static byte[] Keys = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
public static byte[] EncryptDESByte(byte[] encryptString, string encryptKey)
{ try
{ byte[] rgbKey = new byte[8];
Array.Copy(Encoding.UTF8.GetBytes(encryptKey.PadRight(rgbKey.Length)), rgbKey, rgbKey.Length); byte[] rgbIV = Keys; byte[] inputByteArray = encryptString.ToArray(); DESCryptoServiceProvider dCSP = new DESCryptoServiceProvider(); MemoryStream mStream = new MemoryStream(); CryptoStream cStream = new CryptoStream(mStream, dCSP.CreateEncryptor(rgbKey, rgbIV), CryptoStreamMode.Write); cStream.Write(inputByteArray, 0, inputByteArray.Length); cStream.FlushFinalBlock(); return mStream.ToArray(); } catch
{
return encryptString;
} }
/// <summary> /// DES解密字符串 /// </summary> /// <param name="decryptString">待解密的字符串</param> /// <param name="decryptKey">解密密钥,要求为8位,和加密密钥相同</param> /// <returns>解密成功返回解密后的字符串,失败返源串</returns> public static byte[] DecryptDESByte(byte[] decryptString, string decryptKey)
{ try
{
byte[] rgbKey = Encoding.UTF8.GetBytes(decryptKey.PadRight(8).Substring(0,8)); byte[] rgbIV = Keys; byte[] inputByteArray = decryptString.ToArray(); 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 mStream.ToArray(); } catch
{
return decryptString;
} }
}
}
其实我已经说了我想了很多可能性,也调试了不少时间了,毕竟我对C#的加解密模式理解很表面,一些分析可能都无法切中要害,需要有丰富经验的人启发一下。
我相信很多人也会遇到类似的问题(解密不完全,虽然不一定和我这个代码相似,但可能有一样的根本原因)可以说出来供我参考嘛,我贴出来只是怕有人觉得需要我文字叙述不清而已,具体现象可以自己去随便运行一下看看,并不是说就要找人帮忙改代码。