OpenSSL des encryption and decryption

Source: Internet
Author: User
Tags decrypt openssl openssl des openssl library


Recently in the collation of data encryption and decryption, integration of C/s,. NET, Java three platform data encryption and decryption materials, so that three platform invoke related functions to decrypt the results are consistent and compatible.

PS: The final collation of information may be issued in the form of electronic PDF, if it is beneficial to you, please pay attention to.


C + + is directly using the OpenSSL library Crypte module for data encryption and decryption, the following describes the Openssl/crypt call des for encryption and decryption process. The code is easy to understand and is directly attached to the code:

#include <stdio.h> #include "evp.h" #define BREAK_ERROR (msg) {\ fprintf (stderr, "ERROR break [%s]\n", msg); \

Break;\} #define CIPHER_INFO (e) {\ fprintf (stderr, "key_len:[%d]", Evp_cipher_ctx_key_length (e)); \ fprintf (stderr, "Iv_len: [%d ] ", Evp_cipher_ctx_iv_length (e)); \ fprintf (stderr," mode:[%d] ", Evp_cipher_ctx_mode (e)); \ fprintf (stderr," flag:[%d ")

\ n ", Evp_cipher_ctx_flags (e)); \} int Test_ecb_des (char *buf1,char *buf2,char *buf3,int *len1,int *len2,int *len3,unsigned char *key) {int ret = 0,tmple
    N 

    *len2 = *len3 = Tmplen = 0;
    /* Encrypt * * Evp_cipher_ctx CTX;
    Evp_cipher_ctx_init (&AMP;CTX);
    EVP_ENCRYPTINIT_EX (&AMP;CTX,EVP_DES_ECB (), null,key,null);
    Cipher_info (&AMP;CTX); do{if (!
    	Evp_encryptupdate (&AMP;CTX,BUF2,LEN2,BUF1,*LEN1)) {break_error ("encryptupdate"); } if (!
    	EVP_ENCRYPTFINAL_EX (&ctx,buf2+ (*len2), &tmplen)) {break_error ("encryptfinal");
    }}while (0);
    *len2 = *len2 + Tmplen; EVp_cipher_ctx_cleanup (&AMP;CTX);

    fprintf (stderr, "encrypt:len1:[%d] len2:[%d]\n", *len1,*len2);
    /* Decrypt * * Evp_cipher_ctx ctx_d;
    Evp_cipher_ctx_init (&ctx_d);
    EVP_DECRYPTINIT_EX (&AMP;CTX_D,EVP_DES_ECB (), null,key,null); do{if (!
    	Evp_decryptupdate (&ctx_d,buf3,len3,buf2,*len2)) {break_error ("decryptupdate"); } if (!
	Evp_decryptfinal (&ctx_d,buf3+ (*LEN3), &tmplen)) {break_error ("decryptfinal");
    }}while (0);
    *len3 = *len3 + Tmplen;
    Evp_cipher_ctx_cleanup (&ctx_d);
    fprintf (stderr, "encrypt:len2:[%d] len3:[%d]\n", *LEN2,*LEN3);
    /* Compare the data */if (memcmp (buf1,buf3,*len1) = = 0) {fprintf (stderr, "%s success\n", __func__);
    }else{fprintf (stderr, "%s failed\n", __func__);	
return 0;
    #define BUF_SIZE 102402 int main (int Argc,char * argv) {int i,len1,len2,len3;
    unsigned char key_8[] = {0,1,2,3,4,5,6,7};
    unsigned char iv_8[] = {7,6,5,4,3,2,1,0}; char * buf1 = (char *) malloc (buf_size+ 1);
    char * buf2 = (char *) malloc (buf_size + 1);
    char * buf3 = (char *) malloc (buf_size + 1);
    Len1 = Len2 = Len3 = Buf_size;
    for (i = 0;i< buf_size;i++) {buf1[i] = i%256;

    } test_ecb_des (Buf1,buf2,buf3,&len1,&len2,&len3,key_8);
return 0; I/////////////////////////////////////////////////////////////////// One thing I have to say is that des has been deciphered, and of course the cost of deciphering it is not low, and not so simple. If you require a higher level of data security, you can choose to use 3DES or AES, and so on.


The above code is run under the &NBSP;OPENSSL-0.9.8ZD version and passed the test. Not sure if you can run it on all versions of OpenSSL. In use is please test.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.