1月 03

研究一下DES对称加密的算法

C#实现如下

    public class DESEncryptHelper
    {

        /// <summary>
        /// DES对称加密的Key
        /// 固定8位
        /// </summary>
        private static string Key = "duRdp3kJ";

        //// <summary> 
        /// DES加密 
        /// </summary> 
        /// <param name="encryptString"></param> 
        /// <returns></returns> 
        public static string DesEncrypt(string encryptString)
        {
            byte[] keyBytes = Encoding.UTF8.GetBytes(Key.Substring(0, 8));
            byte[] keyIV = keyBytes;
            byte[] inputByteArray = Encoding.UTF8.GetBytes(encryptString);
            DESCryptoServiceProvider provider = new DESCryptoServiceProvider();
            MemoryStream mStream = new MemoryStream();
            CryptoStream cStream = new CryptoStream(mStream, provider.CreateEncryptor(keyBytes, keyIV), CryptoStreamMode.Write);
            cStream.Write(inputByteArray, 0, inputByteArray.Length);
            cStream.FlushFinalBlock();
            return Convert.ToBase64String(mStream.ToArray());
        }

        /// <summary> 
        /// DES解密 
        /// </summary> 
        /// <param name="decryptString"></param> 
        /// <returns></returns> 
        public static string DesDecrypt(string decryptString)
        {
            byte[] keyBytes = Encoding.UTF8.GetBytes(Key.Substring(0, 8));
            byte[] keyIV = keyBytes;
            byte[] inputByteArray = Convert.FromBase64String(decryptString);
            DESCryptoServiceProvider provider = new DESCryptoServiceProvider();
            MemoryStream mStream = new MemoryStream();
            CryptoStream cStream = new CryptoStream(mStream, provider.CreateDecryptor(keyBytes, keyIV), CryptoStreamMode.Write);
            cStream.Write(inputByteArray, 0, inputByteArray.Length);
            cStream.FlushFinalBlock();
            return Encoding.UTF8.GetString(mStream.ToArray());
        }
    }

 

       [DllImport("Lib.dll", EntryPoint = "DES_Encrypt",  CallingConvention = CallingConvention.Cdecl)]
        public extern static void DES_Encrypt(StringBuilder sz_encrypt);

        [DllImport("Lib.dll", EntryPoint = "DES_Decrypt", CallingConvention = CallingConvention.Cdecl)]
        public extern static void DES_Decrypt(StringBuilder sz_encrypt);
// Lib.cpp : Defines the exported functions for the DLL application.
//

#include "stdafx.h"
#include "Lib.h"

#include "des.h"
#define STR_LEN		1024*8

typedef unsigned char byte;


extern "C" __declspec(dllexport) void DES_Encrypt(char*  sz_encrypt)
{
	char keyString[9]; 
	strcpy(keyString, "12345678");
	des_context ctx;
	des_set_key(&ctx, (unsigned char*)keyString);



	char srcString[STR_LEN], destString[STR_LEN];
	memset(srcString, 0, STR_LEN);
	memset(destString, 0, STR_LEN);

	//int SrcLen = 4;

	int SrcLen = strlen(sz_encrypt);

	if (SrcLen % 8 != 0)
		SrcLen = ((SrcLen / 8) + 1) * 8;


	strcpy(srcString, sz_encrypt);




	int len = strlen(srcString);
	for (int i = 0; i<len; i += 8){
		unsigned char tmp[9] = { 0 };
		des_encrypt(&ctx, (unsigned char*)&srcString[i], tmp);
		memcpy(&destString[i], tmp, 8);
	}
	char outString[STR_LEN * 2];
	memset(outString, 0, STR_LEN * 2);


	for (int i = 0; i < SrcLen; i++){
		wsprintf(&outString[i * 2], "%02X", *(byte*)(destString + i));
	}
	 


	strcpy(sz_encrypt, outString);
}

extern "C" __declspec(dllexport) void DES_Decrypt(char*  sz_encrypt)
{

	int destLen = strlen(sz_encrypt);

	char keyString[9];
	strcpy(keyString, "12345678");



	char srcString[STR_LEN], destString[STR_LEN];
	char outString[STR_LEN];

	memset(srcString, 0, STR_LEN);

	memset(destString, 0, STR_LEN);
	strcpy(destString, sz_encrypt);


	memset(outString, 0, STR_LEN);
	BYTE bTmp;
	for (int i = 0; i<destLen / 2; i++){
		bTmp = destString[i * 2];
		if (bTmp>0x39)
			bTmp = bTmp - 'A' + 10;
		else
			bTmp -= 0x30;
		outString[i] = bTmp << 4;

		bTmp = destString[i * 2 + 1];
		if (bTmp>0x39)
			bTmp = bTmp - 'A' + 10;
		else
			bTmp -= 0x30;
		outString[i] += bTmp;
	}

	des_context ctx;
	des_set_key(&ctx, (unsigned char*)keyString);
	for (int i = 0; i<destLen / 2; i += 8){
		unsigned char tmp[9] = { 0 };
		des_decrypt(&ctx, (unsigned char*)&outString[i], tmp);
		memcpy(&srcString[i], tmp, 8);
	}



	strcpy(sz_encrypt, srcString);
}

written by ocean \\ tags: ,

1月 01

新年的鞭炮声刚过,我还在苦逼的Coding,不过依然自得其乐。

这篇记录一下C#调用VC++的DLL里的方法

其实VC++完全一窍不通呀,不过为了新的项目,我决定拼了。顺祝自己好运

首先,建立VC++的项目,选择Win32的DLL项目

在Source Files里添加OceanTest.cpp

#include "stdafx.h"

extern "C" __declspec(dllexport) int Add(int a, int b)
{
	return a + b;
}

extern "C" __declspec(dllexport) int Add2(int a, int b)
{
	return a + b + 2;
}

里面定义了两个方法,编译之后产生一个XXX.DLL 

建立C#工程,类型任意,测试用的winform

讲刚才生成的XXX.DLL复制到bin的debug目录下

调用如下

        private void button1_Click(object sender, EventArgs e)
        {

            int c = Add(5, 7);
            int cc = Add2(5, 7);

        }


        [DllImport("XXX.dll", EntryPoint = "Add", ExactSpelling = false, CallingConvention = CallingConvention.Cdecl)]
        public static extern int Add(int a, int b);


        [DllImport("XXX.dll", EntryPoint = "Add2", ExactSpelling = false, CallingConvention = CallingConvention.Cdecl)]
        public static extern int Add2(int a, int b);

 

完美!!! 

补充一下字符串型的调用,这个和int的不太一样

C++ Code

extern "C" __declspec(dllexport) void DES_Encrypt(char*  sz_encrypt)
{ 
	strcpy(sz_encrypt, "ccccccc"); 
}

C#中定义

        [DllImport("xxx.dll", EntryPoint = "DES_Encrypt",  CallingConvention = CallingConvention.Cdecl)]
        public extern static void DES_Encrypt(StringBuilder sz_encrypt);
    }

C#调用

            var output = new StringBuilder();
            DES_Encrypt(output);

written by ocean \\ tags: