加密后进行解密,出现问题
被加密文件在解密后出现在文件头的那一部分没有被解密或解密错误,
请问我的代码什么地方出现问题??? public class SecrecyManager
{
/// <summary>
/// 加密文件路径
/// </summary>
private static string path;
/// <summary>
/// 加密对象
/// </summary>
private static DESCryptoServiceProvider desc; static SecrecyManager( )
{
desc = new DESCryptoServiceProvider();
path = string.Empty;
//初始化
InitDESC();
} /// <summary>
/// 初始化向量及密钥
/// </summary>
private static void InitDESC( )
{
desc.GenerateIV();
desc.Key = new byte[] { 12,23,45,67,89,48,93,03};
} /// <summary>
/// 读取文件内容
/// </summary>
/// <exception cref=" FileNOtFoundException">文件没有被发现或不可访问</exception>
/// <returns></returns>
private static byte[] ReadFile( )
{
FileStream fs = null;
byte[] tmp = null;
long readCount = 0; try
{
fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.None);
tmp = new byte[fs.Length];
readCount = fs.Read(tmp, 0, tmp.Length); return tmp;
}
finally
{
if (fs != null)
fs.Dispose();
}
} #region 加/解密
/// <summary>
/// 加密文件
/// </summary>
/// <param name="path">文件路径</param>
/// <returns>加密后文件字节</returns>
public static byte[] Encrypt( string path )
{
SecrecyManager.path = path;
MemoryStream ms = new MemoryStream();
CryptoStream cs = null;
byte[] fileData = null;
try
{
//读取文件数据
fileData = ReadFile( );
//生成加密流,加密数据
cs = new CryptoStream(ms, desc.CreateEncryptor(), CryptoStreamMode.Write);
cs.Write(fileData, 0, fileData.Length);
cs.Close(); return ms.ToArray();
}
finally
{
if (cs != null)
cs.Dispose();
}
} /// <summary>
/// 解密文件
/// </summary>
/// <param name="enCrypto">加密文件字节</param>
/// <exception cref=" CryptographicException">解密数据错误</exception>
/// <returns>解密后文件字节</returns>
public static byte[] Decrypt( byte[] enCrypto )
{
MemoryStream ms = new MemoryStream(enCrypto);
CryptoStream cs = null;
byte[] tmp = null;
byte[] ret = null;
int readCount = 0;
int totalCount = 0; try
{
cs = new CryptoStream(ms, desc.CreateDecryptor(), CryptoStreamMode.Read);
ret = new byte[enCrypto.Length];
do
{
readCount = cs.Read(ret,readCount, ret.Length - totalCount);
totalCount += readCount;
} while (readCount != 0); cs.Close();
//新建一数据,获取实际的数据
tmp = new byte[totalCount];
Array.ConstrainedCopy(ret, 0, tmp, 0, totalCount); return tmp;
}
catch (CryptographicException ex)
{
throw ex;
}
finally
{
if (cs != null)
cs.Dispose();
}
}
}
被加密文件在解密后出现在文件头的那一部分没有被解密或解密错误,
请问我的代码什么地方出现问题??? public class SecrecyManager
{
/// <summary>
/// 加密文件路径
/// </summary>
private static string path;
/// <summary>
/// 加密对象
/// </summary>
private static DESCryptoServiceProvider desc; static SecrecyManager( )
{
desc = new DESCryptoServiceProvider();
path = string.Empty;
//初始化
InitDESC();
} /// <summary>
/// 初始化向量及密钥
/// </summary>
private static void InitDESC( )
{
desc.GenerateIV();
desc.Key = new byte[] { 12,23,45,67,89,48,93,03};
} /// <summary>
/// 读取文件内容
/// </summary>
/// <exception cref=" FileNOtFoundException">文件没有被发现或不可访问</exception>
/// <returns></returns>
private static byte[] ReadFile( )
{
FileStream fs = null;
byte[] tmp = null;
long readCount = 0; try
{
fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.None);
tmp = new byte[fs.Length];
readCount = fs.Read(tmp, 0, tmp.Length); return tmp;
}
finally
{
if (fs != null)
fs.Dispose();
}
} #region 加/解密
/// <summary>
/// 加密文件
/// </summary>
/// <param name="path">文件路径</param>
/// <returns>加密后文件字节</returns>
public static byte[] Encrypt( string path )
{
SecrecyManager.path = path;
MemoryStream ms = new MemoryStream();
CryptoStream cs = null;
byte[] fileData = null;
try
{
//读取文件数据
fileData = ReadFile( );
//生成加密流,加密数据
cs = new CryptoStream(ms, desc.CreateEncryptor(), CryptoStreamMode.Write);
cs.Write(fileData, 0, fileData.Length);
cs.Close(); return ms.ToArray();
}
finally
{
if (cs != null)
cs.Dispose();
}
} /// <summary>
/// 解密文件
/// </summary>
/// <param name="enCrypto">加密文件字节</param>
/// <exception cref=" CryptographicException">解密数据错误</exception>
/// <returns>解密后文件字节</returns>
public static byte[] Decrypt( byte[] enCrypto )
{
MemoryStream ms = new MemoryStream(enCrypto);
CryptoStream cs = null;
byte[] tmp = null;
byte[] ret = null;
int readCount = 0;
int totalCount = 0; try
{
cs = new CryptoStream(ms, desc.CreateDecryptor(), CryptoStreamMode.Read);
ret = new byte[enCrypto.Length];
do
{
readCount = cs.Read(ret,readCount, ret.Length - totalCount);
totalCount += readCount;
} while (readCount != 0); cs.Close();
//新建一数据,获取实际的数据
tmp = new byte[totalCount];
Array.ConstrainedCopy(ret, 0, tmp, 0, totalCount); return tmp;
}
catch (CryptographicException ex)
{
throw ex;
}
finally
{
if (cs != null)
cs.Dispose();
}
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货