-
-
[旧帖] c# 加密函数 转C/c++ 0.00雪花
-
发表于: 2012-11-29 14:58 4031
-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | private static byte[] Encrypt(string key, string plianText) { SymmetricAlgorithm Aesalg = SymmetricAlgorithm.Create( "Rijndael" ); HashAlgorithm MD5alg = HashAlgorithm.Create( "MD5" ); Aesalg.BlockSize = 128; Aesalg.Padding = PaddingMode.PKCS7; Aesalg.Mode = CipherMode.CFB; Aesalg.IV = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 }; Aesalg.Key = MD5alg.ComputeHash(Encoding.ASCII.GetBytes(key)); ICryptoTransform cryptor = Aesalg.CreateEncryptor(); MemoryStream ms = new MemoryStream(); CryptoStream cs = new CryptoStream(ms, cryptor, CryptoStreamMode.Write); byte[] inBuffer = Encoding.ASCII.GetBytes(plianText); cs.Write(inBuffer, 0, inBuffer.Length); cs.FlushFinalBlock(); return ms.ToArray(); } |
以上是一段c#加密代码,求c/c++下用CryptoAPI的实现。以下是我用c/c++的实现代码,但是加密的结果不一样,求熟悉的人给于指点。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 | #include "stdafx.h" #include <STDLIB.H> #include <Windows.h> #include <WinCrypt.h> #pragma comment(lib,"crypt32") #define KEYLENGTH 0x00800000 #define ENCRYPT_BLOCK_SIZE 16 BYTE IV[16] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15}; #define MS_ENH_RSA_AES_PROV "Microsoft Enhanced RSA and AES Cryptographic Provider (Prototype)" // 加密数据 char* Entrypt(char* sql,char key[16]) { HCRYPTPROV hCryptProv; HCRYPTKEY hKey; HCRYPTHASH hHash; PBYTE pbBuffer; DWORD dwBlockLen; DWORD dwBufferLen; DWORD dwCount = 0; // 以下获得一个CSP句柄 CryptAcquireContext( &hCryptProv, NULL, //NULL 表示使用默认密钥容器,默认密钥容器名为用户登陆名 MS_ENH_RSA_AES_PROV, PROV_RSA_AES, CRYPT_VERIFYCONTEXT); //-------------------------------------------------------------------- // 创建一个会话密钥(session key) // 会话密钥也叫对称密钥,用于对称加密算法。 // (注: 一个Session是指从调用函数CryptAcquireContext到调用函数 // CryptReleaseContext 期间的阶段。会话密钥只能存在于一个会话过程) if (CryptCreateHash(hCryptProv,CALG_MD5,0,0,&hHash)) { // 用输入的密码产生一个散列 if (CryptHashData(hHash,(BYTE*)key,10,0)) { // 通过散列生成会话密钥 CryptDeriveKey(hCryptProv,CALG_AES_128,hHash,KEYLENGTH,&hKey); // 删除散列表 CryptDestroyHash(hHash); hHash = NULL; BOOL b1 = CryptSetKeyParam(hKey,KP_IV,IV,0); DWORD dwMode = CRYPT_MODE_CFB; b1 = CryptSetKeyParam(hKey,KP_MODE,(const BYTE*)&dwMode,0); dwMode = PKCS5_PADDING; b1 = CryptSetKeyParam(hKey,KP_PADDING,(const BYTE*)&dwMode,0); // 因为加密算法是按ENCRYPT_BLOCK_SIZE 大小的块加密的,所以被加密的 // 数据长度必须是ENCRYPT_BLOCK_SIZE 的整数倍。下面计算一次加密的 // 数据长度。 int sqllen = strlen(sql); dwBlockLen = sqllen - sqllen % ENCRYPT_BLOCK_SIZE; if ((sqllen % ENCRYPT_BLOCK_SIZE) > 0) dwBufferLen = dwBlockLen + ENCRYPT_BLOCK_SIZE; pbBuffer = (BYTE *)malloc(dwBufferLen); ZeroMemory(pbBuffer,dwBufferLen); strcpy((char*)pbBuffer,sql); dwCount = sqllen; if (!CryptEncrypt(hKey,0,TRUE,0,pbBuffer,&dwCount,dwBufferLen)) { free (pbBuffer); pbBuffer = (BYTE*)malloc(dwCount); ZeroMemory(pbBuffer,dwCount); strcpy((char*)pbBuffer,sql); dwCount = strlen(sql); CryptEncrypt(hKey,0,TRUE,0,pbBuffer,&dwCount,dwBufferLen); } } } CryptReleaseContext(hCryptProv,0); DWORD cchString = 0; CryptBinaryToString(pbBuffer,dwCount,CRYPT_STRING_BASE64,NULL,&cchString); char* pResult = (char*)malloc(cchString); CryptBinaryToString(pbBuffer,dwCount,CRYPT_STRING_BASE64,pResult,&cchString); free (pbBuffer); return pResult; } int main(int argc, char* argv[]) { char* plianText = "i love this game." ; char* pEncryText = Entrypt(plianText, "1234567890" ); printf ( "%s" ,pEncryText); return 0; } |
赞赏
他的文章
赞赏
雪币:
留言: