读书人

加密解密读流有关问题

发布时间: 2012-09-18 16:21:42 作者: rapoo

加密解密读流问题
我用DEC进行文件加密解密,下面是加密解密的方法(加密略)
/// <param name="inFileName">待加密文件的路径</param>
/// <param name="outFileName">待加密后文件的输出路径</param>

public void Encrypt(string inFileName, string outFileName)
/// <param name="inFileName">待解密文件的路径</param>
/// <param name="outFileName">待解密后文件的输出路径</param>
public void Decrypt(string inFileName, string outFileName)
{
try
{
FileStream fin = new FileStream(inFileName, FileMode.Open, FileAccess.Read);
FileStream fout = new FileStream(outFileName, FileMode.OpenOrCreate, FileAccess.Write);
fout.SetLength(0);

byte[] bin = new byte[100];
long rdlen = 0;
long totlen = fin.Length;
int len;
mydes.Key = GetLegalKey();
mydes.IV = GetLegalIV();
ICryptoTransform encrypto = mydes.CreateDecryptor();
CryptoStream cs = new CryptoStream(fout, encrypto, CryptoStreamMode.Write);
while (rdlen < totlen)
{
len = fin.Read(bin, 0, 100);
cs.Write(bin, 0, len);
rdlen = rdlen + len;
}
cs.Close();
fout.Close();
fin.Close();

}
解密后会保存一个文件,然后我用progress打开解密后的PPT。可是我不想创建解密后PPT文件,而是直接去打开。那该怎么办呢。我这里有dso操作ppt方法,求神人帮助

[解决办法]
那你得先确认ppt是否支持内存加载呢
[解决办法]

C# code
#region ========加密========          /// <summary>        /// 加密        /// </summary>        /// <param name="Text"></param>        /// <returns></returns>        public static string Encrypt(string Text)         {            return Encrypt(Text,"litianping");        }        /// <summary>         /// 加密数据         /// </summary>         /// <param name="Text"></param>         /// <param name="sKey"></param>         /// <returns></returns>         public static string Encrypt(string Text,string sKey)         {             DESCryptoServiceProvider des = new DESCryptoServiceProvider();             byte[] inputByteArray;             inputByteArray=Encoding.Default.GetBytes(Text);             des.Key = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));             des.IV = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));             System.IO.MemoryStream ms=new System.IO.MemoryStream();             CryptoStream cs=new CryptoStream(ms,des.CreateEncryptor(),CryptoStreamMode.Write);             cs.Write(inputByteArray,0,inputByteArray.Length);             cs.FlushFinalBlock();             StringBuilder ret=new StringBuilder();             foreach( byte b in ms.ToArray())             {                 ret.AppendFormat("{0:X2}",b);             }             return ret.ToString();         }        /// <summary>         /// 加密数据         /// </summary>         /// <param name="Text"></param>         /// <param name="sKey"></param>         /// <returns></returns>         public static string Encrypt(string Text,out string key,out string iv)        {            key = "";            iv = "";            DESCryptoServiceProvider des = new DESCryptoServiceProvider();            byte[] inputByteArray;            inputByteArray = Encoding.Default.GetBytes(Text);            des.GenerateIV();            des.GenerateKey();            iv =  new UnicodeEncoding().GetString(des.IV);            key = new UnicodeEncoding().GetString(des.Key);            System.IO.MemoryStream ms = new System.IO.MemoryStream();            CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);            cs.Write(inputByteArray, 0, inputByteArray.Length);            cs.FlushFinalBlock();            StringBuilder ret = new StringBuilder();            foreach (byte b in ms.ToArray())            {                ret.AppendFormat("{0:X2}", b);            }            return ret.ToString();        }         #endregion                #region ========解密========             /// <summary>        /// 解密        /// </summary>        /// <param name="Text"></param>        /// <returns></returns>        public static string Decrypt(string Text)         {            return Decrypt(Text,"litianping");        }        /// <summary>         /// 解密数据         /// </summary>         /// <param name="Text"></param>         /// <param name="sKey"></param>         /// <returns></returns>         public static string Decrypt(string Text,string sKey)         {             DESCryptoServiceProvider des = new DESCryptoServiceProvider();             int len;             len=Text.Length/2;             byte[] inputByteArray = new byte[len];             int x,i;             for(x=0;x<len;x++)             {                 i = Convert.ToInt32(Text.Substring(x * 2, 2), 16);                 inputByteArray[x]=(byte)i;             }             des.Key = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));             des.IV = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));             System.IO.MemoryStream ms=new System.IO.MemoryStream();             CryptoStream cs=new CryptoStream(ms,des.CreateDecryptor(),CryptoStreamMode.Write);             cs.Write(inputByteArray,0,inputByteArray.Length);             cs.FlushFinalBlock();             return Encoding.Default.GetString(ms.ToArray());         }        /// <summary>         /// 解密数据         /// </summary>         /// <param name="Text"></param>         /// <param name="sKey"></param>         /// <returns></returns>         public static string Decrypt(string Text, string key, string iv)        {            byte[] keyBuffer = new UnicodeEncoding().GetBytes(key);            byte[] ivBuffer = new UnicodeEncoding().GetBytes(iv);            DESCryptoServiceProvider des = new DESCryptoServiceProvider();            int len;            len = Text.Length / 2;            byte[] inputByteArray = new byte[len];            int x, i;            for (x = 0; x < len; x++)            {                i = Convert.ToInt32(Text.Substring(x * 2, 2), 16);                inputByteArray[x] = (byte)i;            }            des.Key = keyBuffer;            des.IV = ivBuffer;            System.IO.MemoryStream ms = new System.IO.MemoryStream();            CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);            cs.Write(inputByteArray, 0, inputByteArray.Length);            cs.FlushFinalBlock();            return Encoding.Default.GetString(ms.ToArray());        }         #endregion 

读书人网 >C#

热点推荐