首页新闻找找看学习计划

C#序列化与反序列化

0
悬赏园豆:20 [已解决问题] 解决于 2013-10-29 16:55

VS创建一个项目A,生成一个序列化的加密文件**.lic,然后,创建项目B,反序列化读出A项目生成的加密文件**.lic并解密,这时候,提示错误:无法找到程序集,是A项目中的。

代码:A项目:

#region DES加密
        /// <summary>
        /// DES加密
        /// </summary>
        /// <param name="EncryptKey">加密密钥</param>
        /// <param name="EncryptString">需要加密的字符串</param>
        /// <returns></returns>
        public string Encrypt(string EncryptKey, string EncryptString)
        {
            try
            {
                int number = 0;
                SymmetricAlgorithm sa = DES.Create();
                try
                {
                    sa.Key = InitKey(EncryptKey);
                }
                catch { return "密钥生成失败!"; }
                sa.IV = new byte[] { 148, 255, 64, 249, 29, 140, 50, 48 };
                sa.Mode = CipherMode.CBC;
                sa.Padding = PaddingMode.Zeros;
                BinaryFormatter bf = new BinaryFormatter();
                byte[] plainBytes;
                Message msg = new Message();
                msg.Num = number;
                msg.MessageBody = EncryptString;
                using (MemoryStream stream = new MemoryStream())
                {
                    bf.Serialize(stream, msg);
                    plainBytes = stream.ToArray();
                }
                MemoryStream ms = new MemoryStream();
                CryptoStream cs = new CryptoStream(ms, sa.CreateEncryptor(), CryptoStreamMode.Write);
                cs.Write(plainBytes, 0, plainBytes.Length);
                cs.Close();
                byte[] cipherBytes = ms.ToArray();
                StringBuilder sb = new StringBuilder();
                for (Int32 i = 0; i < cipherBytes.Length; i++)
                {
                    if (i == cipherBytes.Length - 1)
                        sb.Append(string.Format("{0:X2}", cipherBytes[i]));
                    else
                        sb.Append(string.Format("{0:X2}-", cipherBytes[i]));
                }
                ms.Close();
                return sb.ToString();
            }
            catch (Exception ex) { return "加密失败,失败原因:" + ex.Message; }
        }
        #endregion

 #region 序列化
        //[Serializable]
        public class Message
        {
            private string _messageBody;
            private int _num;
            public string MessageBody
            {
                get { return _messageBody; }
                set { _messageBody = value; }
            }
            public int Num
            {
                get { return _num; }
                set { _num = value; }
            }
        }
        #endregion

#region 生成KEY的函数
        /// <summary>
        /// 生成KEY的函数
        /// </summary>
        /// <param name="strKey"></param>
        /// <returns></returns>
        private byte[] InitKey(String strKey)
        {
            byte[] bp = new byte[strKey.Length];
            byte[] m_Key = new byte[8];
            ASCIIEncoding aEnc = new ASCIIEncoding();
            aEnc.GetBytes(strKey, 0, strKey.Length, bp, 0);
            SHA1CryptoServiceProvider sha = new SHA1CryptoServiceProvider();
            byte[] bpHash = sha.ComputeHash(bp);
            for (int i = 0; i < 8; i++)
            {
                m_Key[i] = bpHash[i];
            }
            return m_Key;
        }
        #endregion

 

B项目:

 

 

        #region DES解密         /// <summary>         /// DES解密         /// </summary>         /// <param name="EncryptKey">解密密钥</param>         /// <param name="EncryptString">需要解密的密文</param>         /// <returns></returns>         public string Decrypt(string EncryptKey, string EncryptString)         {             try             {                 SymmetricAlgorithm sa = DES.Create();                 try                 {                     sa.Key = InitKey(EncryptKey);                 }                 catch { return "生成解密密钥失败!"; }                 sa.IV = new byte[] { 148, 255, 64, 249, 29, 140, 50, 48 };                 sa.Mode = CipherMode.CBC;                 sa.Padding = PaddingMode.Zeros;                 byte[] plainbytes;                 string[] EncryptStrings = EncryptString.Split('-');                 byte[] bts = new byte[EncryptStrings.Length];                 for (int i = 0; i < EncryptStrings.Length; i++)                 {                     int value = Convert.ToInt32(EncryptStrings[i], 16);                     bts[i] = (byte)value;                 }

                using (MemoryStream ms = new MemoryStream(bts))                 {                     using (CryptoStream cs = new CryptoStream(ms, sa.CreateDecryptor(),                         CryptoStreamMode.Read))                     {                         plainbytes = new byte[bts.Length];                         cs.Read(plainbytes, 0, bts.Length);                     }                 }                 Message recoveredMessage;                 using (MemoryStream stream = new MemoryStream(plainbytes, false))                 {                     BinaryFormatter bf = new BinaryFormatter();                     recoveredMessage = (Message)bf.Deserialize(stream);                 }                 return recoveredMessage.MessageBody;             }             catch (Exception ex) { return "解密失败,失败原因:" + ex.Message; }         }         #endregion        

 

        #region 序列化         [Serializable]         public class Message         {             private string _messageBody;             private int _num;             public string MessageBody             {                 get { return _messageBody; }                 set { _messageBody = value; }             }             public int Num             {                 get { return _num; }                 set { _num = value; }             }         }         #endregion

        #region 生成KEY的函数         /// <summary>         /// 生成KEY的函数         /// </summary>         /// <param name="strKey"></param>         /// <returns></returns>         private byte[] InitKey(String strKey)         {             byte[] bp = new byte[strKey.Length];             byte[] m_Key = new byte[8];             ASCIIEncoding aEnc = new ASCIIEncoding();             aEnc.GetBytes(strKey, 0, strKey.Length, bp, 0);             SHA1CryptoServiceProvider sha = new SHA1CryptoServiceProvider();             byte[] bpHash = sha.ComputeHash(bp);             for (int i = 0; i < 8; i++)             {                 m_Key[i] = bpHash[i];             }             return m_Key;         }         #endregion

有机物的主页 有机物 | 初学一级 | 园豆:15
提问于:2013-10-29 12:50
< >
分享
最佳答案
0

两个项目的类名一样,但他们是不同的类,通常情况下是这样的

项目A的类是ProjectA.Message

项目B的类是ProjectB.Message

你使用ProjectA的Message类序列化在字符串后,先别加密,你看看里面产生的字符串就知道了。

如果要处理你这种情况,通常一个简单的方法是创建一人项目C,里面放置Message类。

项目A引用项目C,项目B也引用项目C,序列化与反序列化都是同一个类,这样就没有问题了。

收获园豆:10
爱编程的大叔 | 高人七级 |园豆:29862 | 2013-10-29 15:29
其他回答(3)
0

这个和加解密没什么关系吧。你看看是不是别的地方的问题?

幻天芒 | 园豆:36522 (高人七级) | 2013-10-29 13:00
0

这个是必须的。Message序列化二进制对象再转换成string之后,反序列化过程需要加载对应的程序集。

把这个对象转换成json的格式 再反序列化吧。

 

````` | 园豆:14268 (专家六级) | 2013-10-29 13:08

这个解决了,将序列化的程序集A导入到B项目中引用,使用同一个程序集就可以了。

支持(0) 反对(0) 有机物 | 园豆:15 (初学一级) | 2013-10-29 13:17

@有机物: 嗯。这样也可以解决的。

支持(0) 反对(0) ````` | 园豆:14268 (专家六级) | 2013-10-29 13:22

@imfunny: 结贴放分的时候搞错了,本来是把分给你的,给错了,不好意思哈!

支持(0) 反对(0) 有机物 | 园豆:15 (初学一级) | 2013-10-29 16:56

@有机物: 嗯呢。没事情哈。

支持(0) 反对(0) ````` | 园豆:14268 (专家六级) | 2013-10-29 17:01
0

缺少引用吧?是不是哪个程序集没有引用啊

收获园豆:10
wolfy | 园豆:2636 (老鸟四级) | 2013-10-29 13:08
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册