using System;
using System.Security.Cryptography;
using System.IO;
using System.Text;
using System.Runtime.InteropServices;
using System.EnterpriseServices;
namespace EnCryptography
{
[Guid("87B8C714-C163-4080-95B5-FD4220655910")]
[InterfaceType(ComInterfaceType.InterfaceIsDual)]
public interface EnCryptography
{
string GetEncryptString(string str1);
string GetDecryptString(string str2);
string EncryptString(string Value);
string DecryptString(string Value);
string MD5(string str1);
}
[Transaction(TransactionOption.RequiresNew)]
public class Tools:ServicedComponent,EnCryptography
{
private SymmetricAlgorithm mCSP;
public string GetEncryptString(string str1)
{
mCSP=new DESCryptoServiceProvider();
//密钥设置
mCSP.Key=Convert.FromBase64String("sadeweraer=");
mCSP.IV=Convert.FromBase64String("5646tetrwer=");
return EncryptString(str1);
}
public string GetDecryptString(string str2)
{
mCSP=new DESCryptoServiceProvider();
//密钥设置
mCSP.Key=Convert.FromBase64String("sadeweraer=");
mCSP.IV=Convert.FromBase64String("5646tetrwer=");
return DecryptString(str2);
}
public Tools()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
public string EncryptString(string Value)
{
ICryptoTransform ct;
MemoryStream ms;
CryptoStream cs;
byte[] byt;
ct = mCSP.CreateEncryptor(mCSP.Key, mCSP.IV);
byt = Encoding.UTF8.GetBytes(Value);
ms = new MemoryStream();
cs = new CryptoStream(ms, ct, CryptoStreamMode.Write);
cs.Write(byt, 0, byt.Length);
cs.FlushFinalBlock();
cs.Close();
return Convert.ToBase64String(ms.ToArray());
}
public string DecryptString(string Value)
{
ICryptoTransform ct;
MemoryStream ms;
CryptoStream cs;
byte[] byt;
ct = mCSP.CreateDecryptor(mCSP.Key, mCSP.IV);
byt = Convert.FromBase64String(Value);
ms = new MemoryStream();
cs = new CryptoStream(ms, ct, CryptoStreamMode.Write);
cs.Write(byt, 0, byt.Length);
cs.FlushFinalBlock();
cs.Close();
return Encoding.UTF8.GetString(ms.ToArray());
}
public string MD5(string str1)
{
MD5CryptoServiceProvider md5=new MD5CryptoServiceProvider();
byte[] bytValue=new byte[100];
byte[] bytHash=new byte[100];
//将原始字符串转换成字节数组
bytValue = System.Text.Encoding.UTF8.GetBytes(str1);
//计算散列,并返回一个字节数组
bytHash = md5.ComputeHash(bytValue);
md5.Clear();
//返回散列值的 Base64 编码字符串
return Convert.ToBase64String(bytHash);
}
}
}
以上是我写的一个加密,解密类,想将其编译为com,然后好在vb中使用,能正常注册