Hi Dr S N Henson, Thanks for your quick response. Please see following two C programs. We really appreciate your help.
--Wanda Zeng-- /* * crypt.c * * encrypt a string * */ #include <stdio.h> #include <string.h> #include <openssl/evp.h> main() { char dev_query[]="query180900"; char s[]="399"; int i; unsigned char outstring1[1024]; unsigned char key[8]; FILE *fd; strcpy(key,(unsigned char*)s); fd=fopen("dev_query","w"); encrypt_string(key,dev_query,outstring1); fprintf(fd,"%s",outstring1);fflush(fd); fclose(fd); printf("Length of outstring1=%d\n",strlen(outstring1)); printf("decrypted dev_query =[%s]\n",outstring1); } int encrypt_string(unsigned char *key,unsigned char *instring, char *outstring) { unsigned char outbuf[1024] = {0}; /* For string termination */ int outlen,tmplen; unsigned char iv[] = {1,2,3,4,5,6,7,8}; EVP_CIPHER_CTX ctx; EVP_CIPHER_CTX_init(&ctx); EVP_EncryptInit(&ctx, EVP_bf_cbc(), key, iv); if(!EVP_EncryptUpdate(&ctx, outbuf, &outlen, instring, strlen(instring))) { return 0; } if(!EVP_EncryptFinal(&ctx, outbuf + outlen, &tmplen)) { return 0; } outlen += tmplen; printf("outlen=%d\n",outlen); EVP_CIPHER_CTX_cleanup(&ctx); strcpy(outstring,outbuf); return 1; } /* ---------------------------------------------------------------- * decrypt.c * * decrypt a encrypted string * */ #include <stdio.h> #include <string.h> #include <openssl/evp.h> unsigned char dev_query[]={0xf5,0xb9,0x6f,0x14,0xd2,0x91,0x44,0x38, 0x06,0x2a,0x99,0xe0,0xff,0xde,0x87,0x05}; main(int argc, char *argv[]) { char decrypted[2048]; unsigned char key[8]; unsigned char instring[1024]; char s[]="399"; FILE *fd; fd=fopen("dev_query2","w"); fprintf(fd,"%s",dev_query); fclose(fd); strcpy(key,(unsigned char*)s); strcpy(instring,dev_query); decrypt_string(key,instring,decrypted); printf("Development:\n"); printf("\tseed=[%s]\n\tinput=[%s]\n\tdecrypted string=[%s]\n",s,dev_query,decrypted); } int decrypt_string(unsigned char *key,unsigned char *instring, char *outstring) { unsigned char buf[1024] = {0}; /* For string termination */ int outlen,tmplen; unsigned char iv[] = {1,2,3,4,5,6,7,8}; EVP_CIPHER_CTX ctx; EVP_CIPHER_CTX_init(&ctx); EVP_DecryptInit(&ctx, EVP_bf_cbc(), key, iv); if(!EVP_DecryptUpdate(&ctx, buf, &outlen, instring, strlen(instring))) { return 0; } printf("ctx->cipher->block_size=%d\n",ctx.cipher->block_size); printf("ctx->buf_len=%d\n",ctx.buf_len); if(!EVP_DecryptFinal(&ctx, buf + outlen, &tmplen)) { return 0; } outlen += tmplen; EVP_CIPHER_CTX_cleanup(&ctx); /* printf("decrypt value =[%s] [%d] [%d]\n\n", buf, key, outlen); */ strcpy(outstring,buf); return 1; } ______________________________________________________________________ OpenSSL Project http://www.openssl.org User Support Mailing List [EMAIL PROTECTED] Automated List Manager [EMAIL PROTECTED]