openssl 实现SHA1,DES等加密算法

3/8/2017来源:ASP.NET技巧人气:1898

编码规则:

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

/** * Creator: WangBin, 2009-11-26 * For encrypt... * 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. * * Need To Notice: When you get the return NULL, means wrong; Remember free memory you get from the return. * How To: * 1.Four parameters: str1, ID, TimeStamp, 3DesKey. 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!! * Find some memory leaf, Be sure the PRoccess context is killed! */ #include <stdlib.h> #include <string.h> #include <stdio.h> #include <XXX/base64.h> #include <openssl/evp.h> #include <openssl/sha.h> #include <openssl/des.h> #include "encrypt.h" #define MAX_URL_LEN 2048 #define DES3_BYTE 8 #define DES3_PKCS7 typedef unsigned char uchar; uchar *sha1_encode(uchar *src) { SHA_CTX c; uchar *dest = (uchar *)malloc((SHA_DIGEST_LENGTH + 1)*sizeof(uchar)); memset(dest, 0, SHA_DIGEST_LENGTH + 1); if(!SHA1_Init(&c)) { free(dest); return NULL; } SHA1_Update(&c, src, strlen(src)); SHA1_Final(dest,&c); OPENSSL_cleanse(&c,sizeof(c)); return dest; } uchar *inter_string(uchar *s1, uchar *s2, uchar *s3) { uchar *dst, *tmp = NULL; int value; size_t len; if(s3 != NULL) { len = strlen(s1) + strlen(s2) + strlen(s3) + 2; #ifdef DES3_PKCS7 //PKCS7补全法,情goolge.确保3DES加密时是8的倍数 value = DES3_BYTE - len%DES3_BYTE; if(value != 0) { tmp = (uchar *)malloc((value + 1)*sizeof(uchar)); memset(tmp, value, value); memset(tmp + value, 0, 1); } #endif len = (DES3_BYTE - len%DES3_BYTE) + len; dst = (uchar *)malloc((len + 1)*sizeof(uchar)); memset(dst, 0, len + 1); strcpy(dst, s1); strcat(dst, "$"); strcat(dst, s2); strcat(dst, "$"); strcat(dst, s3); if(tmp != NULL) strcat(dst, tmp); free(tmp); //free a pointer to NULL..not a bad thing } else { len = strlen(s1) + strlen(s2) + 1; len = (DES3_BYTE - len%DES3_BYTE) + len; dst = (uchar *)malloc((len + 1)*sizeof(uchar)); memset(dst, 0, len + 1); strcpy(dst, s1); strcat(dst, "$"); strcat(dst, s2); } fprintf(stderr, "inter_string = %s, //////line = %d\n", dst, __LINE__); return dst; } int des_encode(uchar *key, uchar *iv, uchar *in, size_t len, uchar **out, int enc) { int ret, i, num; uchar cbc_out[512]; uchar key1[8], key2[8], key3[8]; des_key_schedule ks,ks1,ks2; des_cblock *iv3; /************ugly to get key easily*****************/ memset(key1, 0, 8); memset(key2, 0, 8); memset(key3, 0, 8); memcpy(key1, key, 8); memcpy(key2, key + 8, 8); memcpy(key3, key + 16, 8); if ((ret = DES_set_key_checked((const_DES_cblock*)&key1, &ks)) != 0) { fprintf(stderr, "Key1 error %d\n",ret); return -1; } if ((ret = DES_set_key_checked((const_DES_cblock*)&key2, &ks1)) != 0) { fprintf(stderr, "Key2 error %d\n",ret); return -1; } if ((ret = DES_set_key_checked((const_DES_cblock*)&key3, &ks2)) != 0) { fprintf(stderr, "Key3 error %d\n",ret); return -1; } iv3 = (des_cblock *)malloc(strlen(iv)*sizeof(uchar)); memset(cbc_out,0,512); memcpy(iv3,iv,strlen(iv)); num = len/16; des_ede3_cbc_encrypt(in,cbc_out,len,ks,ks1,ks2,iv3,enc); //cbc算法 memcpy(*out, cbc_out, len); /* for(i = 0; i < num; i++) des_ede3_cbc_encrypt(&(in[16*i]),&(cbc_out[16*i]),16L,ks,ks1,ks2,iv3,enc); des_ede3_cbc_encrypt(&(in[16*i]),&(cbc_out[16*i]),len - num*16,ks,ks1,ks2,iv3,enc); //16位加密 */ for(i=0 ; i < len ; i++) printf(" %02x",cbc_out[i]); printf("\n"); free(iv3); return 0; } /*====================================================================== I dont't know what about base64+sha1 we use the sha1-array or a new char * from the sha1-array whatever I do the char charges with ugly code =======================================================================*/ uchar *split_byte(uchar *src, size_t len) { int i; uchar tmp, tmp1; uchar *dest = (uchar *)malloc((len + 1)*sizeof(uchar)); memset(dest, 0, len + 1); for(i = 0; i < len/2; i++) sprintf(dest + i*2,"%02x",src[i] & 0x000000ff); fprintf(stderr, "function = %s, ////dest = %s, //////line = %d\n", __FUNCTION__, dest, __LINE__); } uchar *encrypt_JST(uchar *ID, uchar *str1, uchar *TimeStamp, uchar * key, uchar *iv) { int ret, i; size_t len; uchar *sha1, *sha_str, *digest, *digest1, *encrypt; uchar *des3, *src, *url_str, *url; src = inter_string(str1, TimeStamp, NULL); sha1 = sha1_encode(src); if(!sha1) { free(src); return NULL; } len = strlen(sha1); #ifdef CONVERT_T_STR sha_str = split_byte(sha1, len*2); ret = base64_encode_alloc(sha_str, len*2, &digest); #else ret = base64_encode_alloc(sha1, len, &digest); #endif if(!ret) { free(src); free(sha1); #ifdef CONVERT_T_STR free(sha_str); #endif return NULL; encrypt = (uchar *)malloc(len*sizeof(uchar)); memset(encrypt, 0, len); if(des_encode(key, iv, des3, len, &encrypt, DES_ENCRYPT)) { free(src); free(sha1); #ifdef CONVERT_T_STR free(sha_str); #endif free(des3); free(digest); free(encrypt); return NULL; } ret = base64_encode_alloc(encrypt, len, &digest1); if(!ret) { free(src); free(sha1); #ifdef CONVERT_T_STR free(sha_str); #endif free(des3); free(digest); free(encrypt); return NULL; } fprintf(stderr, "digest1= %s, ////////line = %d\n", digest1, __LINE__); url_str = inter_string(ID, digest1, NULL); url = (uchar *)malloc(MAX_URL_LEN * sizeof(uchar)); url_encode(url_str, url, MAX_URL_LEN - 1); fprintf(stderr, "ur = %s, ///////line = %d\n", url, __LINE__); free(src); free(sha1); #ifdef CONVERT_T_STR free(sha_str); #endif free(des3); free(digest); free(encrypt); free(digest1); free(url_str); return url; }