萬盛學電腦網

 萬盛學電腦網 >> 網絡編程 >> 編程語言綜合 >> C++教程:openssl 實現的加密算法

C++教程:openssl 實現的加密算法

編碼規則:Digest = Base64(SHA1(str1 + "$" + TimeStamp)); Result = URLEncoding(ID + "$" + Base64(3DES(str1 + "$" + TimeStamp + "$" + Digest))).
從編碼規則中我們要使用SHA1、Base64、3DES與URLEncoding 四種加密方法並且來回來加密. 我就不都單獨拿出來貼代碼了,直接貼比較全的代碼.   標簽: 加密 算法 sha1 base64 3DES OpenSSL  

代碼片段(1)

view source   print? 001 /** 002 * Creator: WangBin, 2009-11-26   003 * For encrypt...  004 * I cant't verify those code, What the fuck 0f 3des, Make me always get the different result.Bad thing is the memory, should be careful of those free. 005 * 006 * Need To Notice: When you get the return NULL, means wrong; Remember free memory you get from the return. 007 * How To: 008 * 1.Four parameters: str1, ID, TimeStamp, 3DesKey. 009         3DesKey should be initialied as array,like "unsigned char key[24] ={0x2C, 0x7A, 0x0E, 0x98, 0xF1, 0xE0, 0x76, 0x49, 0x73, 0x15, 0xCD, 0x25, 0xE0, 0xB5, 0x43, 0xCB, 0x0E, 0x80, 0x76, 0x01, 0x7F, 0x23, 0x8A, 0x46};"(I needn't convert them). should not be a string!! 010           011 * Find some memory leaf, Be sure the proccess context is killed! 012 */ 013   014 #include <stdlib.h> 015 #include <string.h> 016 #include <stdio.h> 017 #include <XXX/base64.h> 018 #include <openssl/evp.h> 019 #include <openssl/sha.h> 020 #include <openssl/des.h> 021 #include "encrypt.h" 022 #define MAX_URL_LEN 2048 023 #define DES3_BYTE 8 024 #define DES3_PKCS7 025   026 typedef unsigned char uchar; 027   028 uchar *sha1_encode(uchar *src) 029 { 030         SHA_CTX c; 031         uchar *dest = (uchar *)malloc((SHA_DIGEST_LENGTH + 1)*sizeof(uchar)); 032         memset(dest, 0, SHA_DIGEST_LENGTH + 1); 033         if(!SHA1_Init(&c)) 034         { 035                 free(dest); 036                 return NULL; 037         } 038         SHA1_Update(&c, src, strlen(src)); 039         SHA1_Final(dest,&c); 040
copyright © 萬盛學電腦網 all rights reserved