您现在的位置: 万盛学电脑网 >> 程序编程 >> 网络编程 >> 编程语言综合 >> 正文

C++教程:openssl 实现的加密算法

作者:佚名    责任编辑:admin    更新时间:2022-06-22

编码规则:Digest = Base64(SHA1(str1 + "$" + TimeStamp)); Result = URLEncoding(ID + "$" + Base64(3DES(str1 + "$" + TimeStamp + "$" + Digest))).
从编码规则中我们要使用SHA1、Base64、3DES与URLEncoding 四种加密方法并且来回来加密. 我就不都单独拿出来贴代码了,直接贴比较全的代码.   标签: 加密 算法 sha1 base64 3DES OpenSSL  

代码片段(1)

[代码] [C/C++/Objective-C]代码

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