| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 790 人关注过本帖
标题:大家帮忙看看我错在哪里啦
只看楼主 加入收藏
dreamhouse
Rank: 1
等 级:新手上路
帖 子:107
专家分:7
注 册:2011-12-16
结帖率:59.46%
收藏
已结贴  问题点数:2 回复次数:3 
大家帮忙看看我错在哪里啦
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using

namespace ConsoleApplication11
{
    class Program
    {
        static void Main(string[] args)
        {
             public class MYDES{
        public static System.Security.Cryptography.DES mydes = new DESCryptoServiceProvider();
        private static string m_key = "";
        private static string m_iv = "";

        #region 解密文件 返回解密出的字符串 string Decode(string inFileName, string key, string iv)
        public static string Decode(string inFileName, string key, string iv)
        {
            m_key = key;
            m_iv = iv;
            string result = "";
            try
            {
                mydes.IV = MYDES.GetLegalIV();
                mydes.Key = MYDES.GetLegalKey();
                MemoryStream mStream = new MemoryStream();
                ICryptoTransform encrypto = mydes.CreateDecryptor();
                CryptoStream encStream = new CryptoStream(mStream, encrypto, CryptoStreamMode.Write);
                byte[] btFile = File.ReadAllBytes(inFileName);
                encStream.Write(btFile, 0, btFile.Length);
                encStream.FlushFinalBlock();
                result = Encoding.Default.GetString(mStream.ToArray());
                encStream.Close();
                mStream.Close();

            }catch(Exception ex)
            {
                return "解密失败";
            }
            return result;
        }
        #endregion

        #region 解密文件 解密后生成新的文件  返回bool指示解密成功失败情况 bool Decode(string inFileName, string outFileName, string key, string iv)
        public static bool Decode(string inFileName, string outFileName, string key, string iv)
        {
            m_key = key;
            m_iv = iv;
            try
            {
                if (outFileName.Trim().Length == 0)
                    outFileName = inFileName + ".decry";
                FileStream fout = new FileStream(outFileName, FileMode.OpenOrCreate, FileAccess.Write);
                mydes.IV = MYDES.GetLegalIV();
                mydes.Key = MYDES.GetLegalKey();
                ICryptoTransform encrypto = mydes.CreateDecryptor();
                CryptoStream encStream = new CryptoStream(fout, encrypto, CryptoStreamMode.Write);
                byte[] btFile = File.ReadAllBytes(inFileName);
                encStream.Write(btFile, 0, btFile.Length);
                encStream.FlushFinalBlock();
                encStream.Close();
                fout.Close();
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
        #endregion

        #region 加密文件 返回加密后的字符串  string Encode(string inFileName, string key, string iv)
        public static string Encode(string inFileName, string key, string iv)
        {
            m_key = key;
            m_iv = iv;
            string result = "";
            try
            {
                mydes.IV = MYDES.GetLegalIV();
                mydes.Key = MYDES.GetLegalKey();
                MemoryStream mStream = new MemoryStream();
                ICryptoTransform encrypto = mydes.CreateEncryptor();
                CryptoStream encStream = new CryptoStream(mStream, encrypto, CryptoStreamMode.Write);
                byte[] btFile = File.ReadAllBytes(inFileName);
                encStream.Write(btFile, 0, btFile.Length);
                encStream.FlushFinalBlock();
                result = Convert.ToBase64String(mStream.ToArray());
                encStream.Close();
                mStream.Close();
            }catch(Exception ex)
            {
                return "加密失败!";
            }
            return result;
        }
        #endregion

        #region 加密文件 加密后生成新的文件  返回bool指示加密成功失败情况 bool Encode(string inFileName, string outFileName, string key, string iv)
        public static bool Encode(string inFileName, string outFileName, string key, string iv)
        {
            m_key = key;
            m_iv = iv;
            try
            {
                if (outFileName.Trim().Length == 0)
                    outFileName = inFileName + ".enc";
                FileStream fout = new FileStream(outFileName, FileMode.OpenOrCreate, FileAccess.Write);
                mydes.IV = MYDES.GetLegalIV();
                mydes.Key = MYDES.GetLegalKey();
                ICryptoTransform encrypto = mydes.CreateEncryptor();
                CryptoStream encStream = new CryptoStream(fout, encrypto, CryptoStreamMode.Write);
                byte[] btFile = File.ReadAllBytes(inFileName);
                encStream.Write(btFile, 0, btFile.Length);
                encStream.FlushFinalBlock();
                encStream.Close();
                fout.Close();
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
        #endregion

        #region 解密字符串 返回解密后的字符串 string Decrypt(string src, string key, string iv)
        public static string Decrypt(string src, string key, string iv)
        {
            m_key = key;
            m_iv = iv;
            string result ="";
            try
            {
                mydes.IV = MYDES.GetLegalIV();
                mydes.Key = MYDES.GetLegalKey();
                byte[] btFile = Convert.FromBase64String(src);
                MemoryStream mStream = new MemoryStream();
                ICryptoTransform encrypto = mydes.CreateDecryptor();
                CryptoStream encStream = new CryptoStream(mStream, encrypto, CryptoStreamMode.Write);
                encStream.Write(btFile, 0, btFile.Length);
                encStream.FlushFinalBlock();
                result = Encoding.Default.GetString(mStream.ToArray());
                encStream.Close();
                mStream.Close();
            }catch(Exception ex)
            {
                return "解密失败";
            }
            return result;
        }
        #endregion

        #region 解密字符串 生成解密文件 返回bool值 bool Decrypt(string src, string outFileName, string key, string iv)
        public static bool Decrypt(string src, string outFileName, string key, string iv)
        {
            m_key = key;
            m_iv = iv;
            try
            {
                FileStream fout = new FileStream(outFileName, FileMode.OpenOrCreate, FileAccess.Write);
                mydes.IV = MYDES.GetLegalIV();
                mydes.Key = MYDES.GetLegalKey();
                ICryptoTransform encrypto = mydes.CreateDecryptor();
                CryptoStream encStream = new CryptoStream(fout, encrypto, CryptoStreamMode.Write);
                byte[] btFile = Convert.FromBase64String(src);
                encStream.Write(btFile, 0, btFile.Length);
                encStream.FlushFinalBlock();
                encStream.Close();
                fout.Close();
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
        #endregion

        #region 加密字符串 返回加密后的字符串 string Encrypt(string src, string key, string iv)
        public static string Encrypt(string src, string key, string iv)
        {
            m_key = key;
            m_iv = iv;
            string result ="";
            try
            {
                mydes.IV = MYDES.GetLegalIV();
                mydes.Key = MYDES.GetLegalKey();
                byte[] btFile = Encoding.Default.GetBytes(src);
                MemoryStream mStream = new MemoryStream();
                ICryptoTransform encrypto = mydes.CreateEncryptor();
                CryptoStream encStream = new CryptoStream(mStream, encrypto, CryptoStreamMode.Write);
                encStream.Write(btFile, 0, btFile.Length);
                encStream.FlushFinalBlock();
                result = Convert.ToBase64String(mStream.ToArray());
                encStream.Close();
                mStream.Close();
            }
            catch (Exception ex)
            {
                return "";
            }
            return result;
        }
        #endregion

        #region 加密字符串 生成加密文件 返回bool值 bool Encrypt(string src, string outFileName, string key, string iv)
        public static bool Encrypt(string src, string outFileName, string key, string iv)
        {
            m_key = key;
            m_iv = iv;
            try
            {
                FileStream fout = new FileStream(outFileName, FileMode.OpenOrCreate, FileAccess.Write);
                mydes.IV = MYDES.GetLegalIV();
                mydes.Key = MYDES.GetLegalKey();
                ICryptoTransform encrypto = mydes.CreateEncryptor();
                CryptoStream encStream = new CryptoStream(fout, encrypto, CryptoStreamMode.Write);
                byte[] btFile = Encoding.Default.GetBytes(src);
                encStream.Write(btFile, 0, btFile.Length);
                encStream.FlushFinalBlock();
                encStream.Close();
                fout.Close();
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
        #endregion




        private static byte[] GetLegalKey()
        {
            string sTemp = m_key;
            mydes.GenerateKey();
            byte[] bytTemp = mydes.Key;
            int KeyLength = bytTemp.Length;
            if (sTemp.Length > KeyLength)
                sTemp = sTemp.Substring(0, KeyLength);
            else if (sTemp.Length < KeyLength)
                sTemp = sTemp.PadRight(KeyLength, ' ');
            return ASCIIEncoding.ASCII.GetBytes(sTemp);
        }

        private static byte[] GetLegalIV()
        {
            string sTemp = m_iv;
            mydes.GenerateIV();
            byte[] bytTemp = mydes.IV;
            int IVLength = bytTemp.Length;
            if (sTemp.Length > IVLength)
                sTemp = sTemp.Substring(0, IVLength);
            else if (sTemp.Length < IVLength)
                sTemp = sTemp.PadRight(IVLength, ' ');
            return ASCIIEncoding.ASCII.GetBytes(sTemp);
        }

    }
}
}
搜索更多相关主题的帖子: private public 
2014-01-10 10:01
wangnannan
Rank: 18Rank: 18Rank: 18Rank: 18Rank: 18
等 级:贵宾
威 望:87
帖 子:2546
专家分:9359
注 册:2007-11-3
收藏
得分:1 
你自己在VS里编译一下看看不就知道了 或者哪个方法出问题了 你断点调试下 这种干看代码 判断对错 貌似没有谁有这个耐心

出来混,谁不都要拼命的嘛。 。拼不赢?那就看谁倒霉了。 。有机会也要看谁下手快,快的就能赢,慢。 。狗屎你都抢不到。 。还说什么拼命?
2014-01-10 14:22
有容就大
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:东土大唐
等 级:版主
威 望:74
帖 子:9048
专家分:14309
注 册:2011-11-11
收藏
得分:1 
是啊 先自己找问题 描述清楚啦

梅尚程荀
马谭杨奚







                                                       
2014-01-11 00:44
dreamhouse
Rank: 1
等 级:新手上路
帖 子:107
专家分:7
注 册:2011-12-16
收藏
得分:0 
回复 3楼 有容就大
我用vs2008的

vvvvvvvvvv
2014-01-14 16:28
快速回复:大家帮忙看看我错在哪里啦
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.018283 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved