openssl EVP of Series 6 --- EVP_Encrypt series of functional programming languages of architecture and examples --- according to openssl doc / crypto / EVP_EncryptInit.pod and doc / ssleay.txt cipher.doc partially translated and own understanding: DragonKing, Mail: Wzhah@263.net,
Published in: http://openssl.126.com
Version: OpenSSL-0.9.7 In the previous two articles, the EVP_ENCRYPT * ... * series function has been described in detail. This chapter will explain the application architecture common to the series of functions and raise several function application examples. [Application Architecture] Generally, the application architecture of the EVP_ENCRYPT * ... * Series function (assuming the encryption algorithm 3DES): 1. Define some necessary variable char key [EVP_MAX_KEY_LENGTH]; char iv [evp_max_iv_length]; evp_cipher_ctx CTX; unsigned char OUT [512 8]; INT OUTL; 2. Assign a value to the variable key and IV, where functions EVP_BYTESTOKEY are used, which generates a key Key and initialization vector IV from the input password, which will be made later. Introduce. If there is something else to set Key and IV, the call is not a must EVP_BYTESTOKEY (EVP_DES_EDE3_CBC, EVP_MD5, NULL, PASSWD, STRLEN (PASSWD), KEY, IV); 3. The initial encryption algorithm structure EVP_CIPHER_CTX EVP_ENCRYPTIT_EX (& CTX, EVP_DES_EDE3_CBC (), NULL, Key, IV); 4. Encryption Operation of Data While (....) {EVP_ENCRYPTUPDATE (CTX, OUT, & OUTL, IN, 512);} Generally, the loop structure is used to process Each cycle encrypted data is 512 bytes, and the ciphertext output to OUT, OUT and INT should be different in memory. 5. End the encryption, output the last 512 byte of data EVP_ENCRYPTFINAL_EX (& CTX, OUT, & OUTL) This function will be encrypted, if the encryption process is incorrect, it is generally checked. Note: Encryption is the same as the above process, but only uses the EVP_DECRYPT * ... * series function.
[Examples] 1. RC5 algorithm to obtain the number of cycles used (round) int nrounds; EVP_CIPHER_CTX_ctrl (ctx, EVP_CTRL_GET_RC5_ROUNDS, 0, & nrounds); 2. RC2 algorithm to obtain the effective key length int key_bits; EVP_CIPHER_CTX_ctrl (ctx, EVP_CTRL_GET_RC2_KEY_BITS, 0, & key_bits); 3. set RC5 algorithm used cycles (round) int nrounds; EVP_CIPHER_CTX_ctrl (ctx, EVP_CTRL_SET_RC5_ROUNDS, nrounds, NULL); 4. set algorithm RC2 effective key length int key_bits; EVP_CIPHER_CTX_ctrl (ctx, EVP_CTRL_SET_RC2_KEY_BITS, key_bits, NULL); 5. Encrypt a string INT DO_CRYPT (CHAR * outbuff [1024]; int outlen, tmplen; // In fact, Key and IV should generally be obtained from other places, at least at least As a demigned char key [] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}; unsigned char}; {1, 2, 3, 4, 5, 6, 7, 8}; char intexT [] = "Some Crypto Text"; EVP_CIPHER_CTX CTX; file * out; evp_cipher_ctx_init (& ctx); EVP_ENCRYPTINIT_EX (& CTX, EVP_BF_CBC (), NULL Key, IV); if (! EVP_ENCRYPTUPDATE (& CTX, Outbuf, & Outlen, Intext, Strlen (Intext))) {/ * error handling * / RET URN 0;} / Note, the output cache parameter that passes to the following function must be aware that the original encrypted output data IF (! EVP_ENCRYPTFINAL_EX (& CTX, Outbuf Outlen, & TMPLEN)) {/ * error handling * / Return 0;} OUTLEN = TMPLEN; EVP_CIPHER_CTX_CLEANUP (& CTX); // Attention, use binary mode to open files when saving to files, because ciphertext data is binary, and cannot use Strlen functions because the ciphertext string is not String OUT = FOPEN (Outfile, "WB") with NULL (0); FWRITE (Outbuf, 1, Outlen, Out); fclose (out); Return 1;} The example encrypted on the previous example Use the application cipher.exe provided by OpenSSL to decrypt, the command is as follows: