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
两个项目的类名一样,但他们是不同的类,通常情况下是这样的
项目A的类是ProjectA.Message
项目B的类是ProjectB.Message
你使用ProjectA的Message类序列化在字符串后,先别加密,你看看里面产生的字符串就知道了。
如果要处理你这种情况,通常一个简单的方法是创建一人项目C,里面放置Message类。
项目A引用项目C,项目B也引用项目C,序列化与反序列化都是同一个类,这样就没有问题了。
这个和加解密没什么关系吧。你看看是不是别的地方的问题?
缺少引用吧?是不是哪个程序集没有引用啊