第一步下载capicom.dll
http://files.cnblogs.com/files/chen110xi/DLL.7z
第二步注册capicom.dll至SysWow64
第三步VS中设置
1.添加com,capicom.dll的参考
2.COM 元件需注意是否內嵌 Interop 型別,设置为false(只有.NET 4.0+需要)
3.专案是否设置为x86
第四步开发
http://files.cnblogs.com/files/chen110xi/KeyC.7z
C#
/// <summary> /// 解密 /// </summary> /// <param name="EncryptMsg">需要解密的字串</param> /// <param name="DecryptKey">金鑰</param> /// <returns></returns> /// <remarks></remarks> public static string DoDecryptCommand(string EncryptMsg,string DecryptKey) { string strRetrun = string.Empty; // Dim Contents CAPICOM.EncryptedData EncryptedData = new CAPICOM.EncryptedData(); //dynamic EncryptedData = null; // Create the EncryptedData object. //EncryptedData = Interaction.CreateObject("CAPICOM.EncryptedData"); // Set decryption password. EncryptedData.SetSecret(DecryptKey); // Load the encrypted message. // LoadFile FileName, Message // Now decrypt it. EncryptedData.Decrypt(EncryptMsg); //DoDecryptCommand = EncryptedData.Content strRetrun = EncryptedData.Content; // Free resources. EncryptedData = null; return strRetrun; } // End DoDecryptCommand /// <summary> /// 加密 /// </summary> /// <param name="Content">要加密的字串</param> /// <param name="Algorithm">預設3</param> /// <param name="KeyLength">預設0</param> /// <param name="Password">金鑰</param> /// <returns></returns> /// <remarks></remarks> public static string DoEncryptCommand(string Content, CAPICOM.CAPICOM_ENCRYPTION_ALGORITHM Algorithm, CAPICOM.CAPICOM_ENCRYPTION_KEY_LENGTH KeyLength, string Password) { string functionReturnValue = ""; CAPICOM.EncryptedData EncryptedData = new CAPICOM.EncryptedData(); // Set algorithm, key size, and encryption password. EncryptedData.Algorithm.Name = Algorithm; EncryptedData.Algorithm.KeyLength = KeyLength; EncryptedData.SetSecret(Password); // Now encrypt it. EncryptedData.Content = Content; functionReturnValue = EncryptedData.Encrypt(); // Free resources. EncryptedData = null; return functionReturnValue; }
VB
Imports CAPICOM Module Decrypt 'Public Fire_Up, Login_OK, Pass1 As String ''' <summary> ''' 解密 ''' </summary> ''' <param name="EncryptMsg">密文</param> ''' <param name="DecryptKey">密匙</param> ''' <returns>明文</returns> ''' <remarks></remarks> Public Function DoDecryptCommand(ByVal EncryptMsg, ByVal DecryptKey, Optional ByVal Algorithm = 0, Optional ByVal KeyLength = 0) 'Dim Contents 'Dim EncryptedData '' Create the EncryptedData object. 'EncryptedData = CreateObject("CAPICOM.EncryptedData") '' Set decryption password. 'EncryptedData.Algorithm.Name = Algorithm 'EncryptedData.Algorithm.KeyLength = KeyLength 'EncryptedData.SetSecret(DecryptKey) '' Load the encrypted message. '' LoadFile FileName, Message '' Now decrypt it. 'EncryptedData.Decrypt(EncryptMsg) 'Nelson Mark 'Contents = Trim(EncryptedData.Content) 'Nelson Mark 'Pass1 = Contents 'EncryptedData = Nothing 'Return Contents 'Nelson Mark Dim encryptedData As New CAPICOM.EncryptedData encryptedData.Algorithm.Name = 0 encryptedData.Algorithm.KeyLength = 0 encryptedData.SetSecret(DecryptKey, CAPICOM_SECRET_TYPE.CAPICOM_SECRET_PASSWORD) encryptedData.Decrypt(EncryptMsg) Return encryptedData.Content End Function 'Const CAPICOM_ENCRYPTION_ALGORITHM_RC2 = 0 'Const CAPICOM_ENCRYPTION_ALGORITHM_RC4 = 1 'Const CAPICOM_ENCRYPTION_ALGORITHM_DES = 2 'Const CAPICOM_ENCRYPTION_ALGORITHM_3DES = 3 'Const CAPICOM_ENCRYPTION_ALGORITHM_AES = 4 'Const CAPICOM_ENCRYPTION_KEY_LENGTH_MAXIMUM = 0 'Const CAPICOM_ENCRYPTION_KEY_LENGTH_40_BITS = 1 'Const CAPICOM_ENCRYPTION_KEY_LENGTH_56_BITS = 2 'Const CAPICOM_ENCRYPTION_KEY_LENGTH_128_BITS = 3 'Const CAPICOM_ENCRYPTION_KEY_LENGTH_192_BITS = 4 'Const CAPICOM_ENCRYPTION_KEY_LENGTH_256_BITS = 5 ''' <summary> ''' 加密 ''' </summary> ''' <param name="Content">明文</param> ''' <param name="DecryptKey">密匙</param> ''' <param name="Algorithm">加密類型:0:RSA RC2;1:RSA RC4;2:DES;3:3DES</param> ''' <param name="KeyLength">密匙長度:0:CAPICOM_KEY_LENGTH_MAXIMUM;1:CAPICOM_KEY_LENGTH_40_BITS(40-bit);2:56-bit;3:128-bit;5:256;</param> ''' <returns>密文</returns> ''' <remarks></remarks> Function DoEncryptCommand(ByVal Content, ByVal DecryptKey, Optional ByVal Algorithm = 0, Optional ByVal KeyLength = 0) 'Dim EncryptedData '' Create the EncryptedData object. 'EncryptedData = CreateObject("CAPICOM.EncryptedData") '' Set algorithm, key size, and encryption password. 'EncryptedData.Algorithm.Name = Algorithm 'EncryptedData.Algorithm.KeyLength = KeyLength 'EncryptedData.SetSecret(DecryptKey) '' Now encrypt it. 'EncryptedData.Content = Content 'DoEncryptCommand = EncryptedData.Encrypt '' Free resources. 'EncryptedData = Nothing Dim encryptedData = New EncryptedDataClass() encryptedData.Content = Content encryptedData.Algorithm.Name = 0 encryptedData.Algorithm.KeyLength = 0 encryptedData.SetSecret(DecryptKey, CAPICOM_SECRET_TYPE.CAPICOM_SECRET_PASSWORD) Return encryptedData.Encrypt(CAPICOM_ENCODING_TYPE.CAPICOM_ENCODE_BASE64) 'Dim functionReturnValue = "" 'Dim EncryptedData = New CAPICOM.EncryptedData() 'EncryptedData.Algorithm.Name = Algorithm 'EncryptedData.Algorithm.KeyLength = KeyLength 'EncryptedData.SetSecret(DecryptKey) 'EncryptedData.Content = Content 'functionReturnValue = EncryptedData.Encrypt() 'Return functionReturnValue End Function ' End DoEncryptCommand End Module