Example of use of symmetric encryption algorithm des common functions in OpenSSL

Source: Internet
Author: User
Tags decrypt

The main consists of 3 files:

1. Cryptotest.h:

#ifndef _cryptotest_h_#define _cryptotest_h_#include <string>using namespace std;typedef enum {general = 0,ECB, Cbc,cfb,ofb,triple_ecb,triple_cbc}crypto_mode;string des_encrypt (const string cleartext, const string key, Crypto_ mode mode); string des_decrypt (const string ciphertext, const string key, Crypto_mode mode); #endif//_cryptotest_h_

2. Destest.cpp:

#include <iostream> #include <string> #include <vector> #include <openssl/des.h> #include " Cryptotest.h "using namespace std;static unsigned char cbc_iv[8] = {' 0 ', ' 1 ', ' A ', ' B ', ' A ', ' B ', ' 9 ', ' 8 '};string des_encr Ypt (const string cleartext, const string key, Crypto_mode MODE) {string Strciphertext;switch (mode) {case General:case ECB: {Des_cblock Keyencrypt;memset (keyencrypt, 0, 8); if (Key.length () <= 8) memcpy (Keyencrypt, Key.c_str (), Key.length ()) ; Else memcpy (Keyencrypt, Key.c_str (), 8);D Es_key_schedule keyschedule;des_set_key_unchecked (&keyencrypt, & Keyschedule); Const_des_cblock inputtext;des_cblock outputtext;vector<unsigned char> vecCiphertext;unsigned char tmp[8];for (int i = 0; i < cleartext.length ()/8; i + +) {memcpy (Inputtext, cleartext.c_str () + i * 8, 8);D es_ecb_  Encrypt (&inputtext, &outputtext, &keyschedule, Des_encrypt); memcpy (TMP, Outputtext, 8); for (int j = 0; J < 8; J + +) Vecciphertext.push_back (Tmp[j]);} if (CLEARTEXT.LEngth ()% 8! = 0) {int tmp1 = Cleartext.length ()/8 * 8;int TMP2 = cleartext.length ()-Tmp1;memset (inputtext, 0, 8); MEMC PY (Inputtext, cleartext.c_str () + TMP1, TMP2);D es_ecb_encrypt (&inputtext, &outputtext, &keyschedule, DES_ ENCRYPT); memcpy (TMP, Outputtext, 8); for (int j = 0; J < 8; j + +) Vecciphertext.push_back (Tmp[j]);} Strciphertext.clear (); Strciphertext.assign (Vecciphertext.begin (), Vecciphertext.end ());} Break;case Cbc:{des_cblock Keyencrypt, Ivec;memset (keyencrypt, 0, 8); if (Key.length () <= 8) memcpy (Keyencrypt, key.c_ STR (), key.length ()); else memcpy (Keyencrypt, Key.c_str (), 8);D Es_key_schedule keyschedule;des_set_key_unchecked ( &keyencrypt, &keyschedule); memcpy (Ivec, Cbc_iv, sizeof (CBC_IV)); int ilength = Cleartext.length ()% 8? (Cleartext.length ()/8 + 1) * 8:cleartext.length (); unsigned char* tmp = new unsigned char[ilength + 16];memset (tmp, 0, Ilength);D es_ncbc_encrypt ((const unsigned char*) CLEARTEXT.C_STR (), TMP, Cleartext.length () +1, &keyschedulE, &ivec, des_encrypt); strciphertext = (char*) tmp;delete [] tmp;} Break;case Cfb:{des_cblock Keyencrypt, Ivec;memset (keyencrypt, 0, 8); if (Key.length () <= 8) memcpy (Keyencrypt, key.c_ STR (), key.length ()); else memcpy (Keyencrypt, Key.c_str (), 8);D Es_key_schedule keyschedule;des_set_key_unchecked ( &keyencrypt, &keyschedule); memcpy (Ivec, Cbc_iv, sizeof (CBC_IV)); unsigned char* outputtext = new unsigned char[ Cleartext.length ()];memset (outputtext, 0, Cleartext.length ()), const unsigned char* tmp = (const unsigned char*) Cleartext.c_str ();D es_cfb_encrypt (TMP, Outputtext, 8, Cleartext.length (), &keyschedule, &ivec, DES_ENCRYPT); Strciphertext = (char*) outputtext;delete [] outputtext;} Break;case Triple_ecb:{des_cblock Ke1, Ke2, Ke3;memset (ke1, 0, 8); memset (Ke2, 0, 8); memset (Ke2, 0, 8); if (Key.length () (=) {memcpy (Ke1, Key.c_str (), 8), memcpy (Ke2, KEY.C_STR () + 8, 8), memcpy (Ke3, key.c_str () +, 8);} else if (key.length () >= () {memcpy (Ke1, Key.c_str (), 8); memcpy (Ke2,KEY.C_STR () + 8, 8); memcpy (Ke3, key.c_str () + +, Key.length ()-16);} else if (key.length () >= 8) {memcpy (Ke1, Key.c_str (), 8), memcpy (Ke2, KEY.C_STR () + 8, Key.length ()-8); memcpy (Ke3, key . C_STR (), 8);} else {memcpy (Ke1, Key.c_str (), Key.length ()), memcpy (Ke2, Key.c_str (), Key.length ()), memcpy (Ke3, Key.c_str (), Key.length ());} Des_key_schedule ks1, Ks2, ks3;des_set_key_unchecked (&ke1, &AMP;KS1);D es_set_key_unchecked (&ke2, &AMP;KS2) ;D es_set_key_unchecked (&ke3, &AMP;KS3); Const_des_cblock Inputtext;des_cblock outputtext;vector<unsigned char> vecciphertext;unsigned Char tmp[8];for (int i = 0; i < cleartext.length ()/8; i + +) {memcpy (Inputtext, Cleart EXT.C_STR () + i * 8, 8);D Es_ecb3_encrypt (&inputtext, &outputtext, &ks1, &ks2, &AMP;KS3, DES_ENCRYPT); memcpy (TMP, Outputtext, 8); for (int j = 0; J < 8; j + +) Vecciphertext.push_back (Tmp[j]);} if (cleartext.length ()% 8! = 0) {int tmp1 = Cleartext.length ()/8 * 8;int TMP2 = cleartext.length ()-Tmp1;memseT (inputtext, 0, 8); memcpy (Inputtext, cleartext.c_str () + TMP1, TMP2);D es_ecb3_encrypt (&inputtext, &outputtext , &ks1, &ks2, &AMP;KS3, Des_encrypt); memcpy (TMP, Outputtext, 8); for (int j = 0; J < 8; j + +) Vecciphertext.push_b ACK (Tmp[j]);} Strciphertext.clear (); Strciphertext.assign (Vecciphertext.begin (), Vecciphertext.end ());} Break;case Triple_cbc:{des_cblock Ke1, Ke2, Ke3, Ivec;memset (ke1, 0, 8); memset (Ke2, 0, 8); memset (Ke2, 0, 8); if (key.length () >= () {memcpy (Ke1, Key.c_str (), 8), memcpy (Ke2, KEY.C_STR () + 8, 8), memcpy (Ke3, key.c_str () +, 8);} else if (key.  Length () >= () {memcpy (Ke1, Key.c_str (), 8); memcpy (Ke2, KEY.C_STR () + 8, 8); memcpy (Ke3, key.c_str () + key.length () -16);} else if (key.length () >= 8) {memcpy (Ke1, Key.c_str (), 8), memcpy (Ke2, KEY.C_STR () + 8, Key.length ()-8); memcpy (Ke3, key . C_STR (), 8);} else {memcpy (Ke1, Key.c_str (), Key.length ()), memcpy (Ke2, Key.c_str (), Key.length ()), memcpy (Ke3, Key.c_str (), Key.length ());} Des_key_schedule KS1, KS2, ks3;des_set_key_unchecked (&ke1, &AMP;KS1);D es_set_key_unchecked (&ke2, &ks2);D es_set_key_unchecked ( &ke3, &AMP;KS3); memcpy (Ivec, Cbc_iv, sizeof (CBC_IV)); int ilength = Cleartext.length ()% 8? (Cleartext.length ()/8 + 1) * 8:cleartext.length (); unsigned char* tmp = new unsigned char[ilength + 16];memset (tmp, 0, Ilength);D es_ede3_cbc_encrypt ((const unsigned char*) CLEARTEXT.C_STR (), TMP, Cleartext.length () +1, &AMP;KS1, &AMP;KS2 , &AMP;KS3, &ivec, des_encrypt); strciphertext = (char*) tmp;delete [] tmp;} break;} return strciphertext;} String Des_decrypt (const string ciphertext, const string key, Crypto_mode mode) {string Strcleartext;switch (mode) {case GE Neral:case Ecb:{des_cblock keyencrypt;memset (keyencrypt, 0, 8); if (Key.length () <= 8) memcpy (Keyencrypt, Key.c_str () , Key.length ()); else memcpy (Keyencrypt, Key.c_str (), 8);D Es_key_schedule keyschedule;des_set_key_unchecked (& Keyencrypt, &keyschedule); Const_des_cblock Inputtext;des_cblock Outputtext;vector<unsigned char> veccleartext;unsigned Char tmp[8];for (int i = 0; i < ciphertext.length ()/8; i + +) {memcpy (Inputtext, C IPHERTEXT.C_STR () + i * 8, 8);D Es_ecb_encrypt (&inputtext, &outputtext, &keyschedule, Des_decrypt); memcpy ( TMP, Outputtext, 8); for (int j = 0; J < 8; j + +) Veccleartext.push_back (Tmp[j]);} if (ciphertext.length ()% 8! = 0) {int tmp1 = Ciphertext.length ()/8 * 8;int TMP2 = ciphertext.length ()-Tmp1;memset (INP Uttext, 0, 8); memcpy (Inputtext, ciphertext.c_str () + TMP1, TMP2);D es_ecb_encrypt (&inputtext, &outputtext, &keyschedule, Des_decrypt) memcpy (TMP, Outputtext, 8); for (int j = 0; J < 8; j + +) Veccleartext.push_back (Tmp[j]);} Strcleartext.clear (); Strcleartext.assign (Veccleartext.begin (), Veccleartext.end ());} Break;case Cbc:{des_cblock Keyencrypt, Ivec;memset (keyencrypt, 0, 8); if (Key.length () <= 8) memcpy (Keyencrypt, key.c_ STR (), key.length ()); else memcpy (Keyencrypt, Key.c_str (), 8);D Es_key_schedule keyschedule;des_set_key_unchecked ( &keYencrypt, &keyschedule); memcpy (Ivec, Cbc_iv, sizeof (CBC_IV)); int ilength = Ciphertext.length ()% 8? (Ciphertext.length ()/8 + 1) * 8:ciphertext.length (); unsigned char* tmp = new unsigned char[ilength];memset (TMP, 0, ILe Ngth);D es_ncbc_encrypt ((const unsigned char*) CIPHERTEXT.C_STR (), TMP, Ciphertext.length () +1, &keyschedule, & Ivec, des_decrypt); strcleartext = (char*) tmp;delete [] tmp;} Break;case Cfb:{des_cblock Keyencrypt, Ivec;memset (keyencrypt, 0, 8); if (Key.length () <= 8) memcpy (Keyencrypt, key.c_ STR (), key.length ()); else memcpy (Keyencrypt, Key.c_str (), 8);D Es_key_schedule keyschedule;des_set_key_unchecked ( &keyencrypt, &keyschedule); memcpy (Ivec, Cbc_iv, sizeof (CBC_IV)); unsigned char* outputtext = new unsigned char[ Ciphertext.length ()];memset (outputtext, 0, Ciphertext.length ()), const unsigned char* tmp = (const unsigned char*) Ciphertext.c_str ();D es_cfb_encrypt (TMP, Outputtext, 8, 32/*ciphertext.length ()-16*/, &keyschedule, &ivec, Des_decrypt); STRCLEartext = (char*) outputtext;delete [] outputtext;} Break;case Triple_ecb:{des_cblock Ke1, Ke2, Ke3;memset (ke1, 0, 8); memset (Ke2, 0, 8); memset (Ke2, 0, 8); if (Key.length () (=) {memcpy (Ke1, Key.c_str (), 8), memcpy (Ke2, KEY.C_STR () + 8, 8), memcpy (Ke3, key.c_str () +, 8);} else if (key.length () >=) {memcpy (Ke1, Key.c_str (), 8), memcpy (Ke2, KEY.C_STR () + 8, 8); memcpy (Ke3, key.c_str () +, Key.length ()-16) ;} else if (key.length () >= 8) {memcpy (Ke1, Key.c_str (), 8), memcpy (Ke2, KEY.C_STR () + 8, Key.length ()-8); memcpy (Ke3, key . C_STR (), 8);} else {memcpy (Ke1, Key.c_str (), Key.length ()), memcpy (Ke2, Key.c_str (), Key.length ()), memcpy (Ke3, Key.c_str (), Key.length ());} Des_key_schedule ks1, Ks2, ks3;des_set_key_unchecked (&ke1, &AMP;KS1);D es_set_key_unchecked (&ke2, &AMP;KS2) ;D es_set_key_unchecked (&ke3, &AMP;KS3); Const_des_cblock Inputtext;des_cblock outputtext;vector<unsigned char> veccleartext;unsigned Char tmp[8];for (int i = 0; i < ciphertext.length ()/8; i + +)) {memcpy (Inputtext, ciphertext.c_str () + i * 8, 8);D Es_ecb3_encrypt (&inputtext, &outputtext, &AMP;KS1, &AMP;KS2 , &AMP;KS3, Des_decrypt) memcpy (TMP, Outputtext, 8); for (int j = 0; J < 8; j + +) Veccleartext.push_back (Tmp[j]);} if (ciphertext.length ()% 8! = 0) {int tmp1 = Ciphertext.length ()/8 * 8;int TMP2 = ciphertext.length ()-Tmp1;memset (INP Uttext, 0, 8); memcpy (Inputtext, ciphertext.c_str () + TMP1, TMP2);D es_ecb3_encrypt (&inputtext, &outputtext, &AMP;KS1, &ks2, &AMP;KS3, Des_decrypt) memcpy (TMP, Outputtext, 8); for (int j = 0; J < 8; j + +) Veccleartext.push_back (Tmp[j]);} Strcleartext.clear (); Strcleartext.assign (Veccleartext.begin (), Veccleartext.end ());} Break;case Triple_cbc:{des_cblock Ke1, Ke2, Ke3, Ivec;memset (ke1, 0, 8); memset (Ke2, 0, 8); memset (Ke2, 0, 8); if (key.length () >= () {memcpy (Ke1, Key.c_str (), 8), memcpy (Ke2, KEY.C_STR () + 8, 8), memcpy (Ke3, key.c_str () +, 8);} else if (key. Length () >=) {memcpy (Ke1, Key.c_str (), 8); memcpy (Ke2, Key.c_str() + 8, 8); memcpy (Ke3, key.c_str () + key.length ()-16);} else if (key.length () >= 8) {memcpy (Ke1, Key.c_str (), 8), memcpy (Ke2, KEY.C_STR () + 8, Key.length ()-8); memcpy (Ke3, key . C_STR (), 8);} else {memcpy (Ke1, Key.c_str (), Key.length ()), memcpy (Ke2, Key.c_str (), Key.length ()), memcpy (Ke3, Key.c_str (), Key.length ());} Des_key_schedule ks1, Ks2, ks3;des_set_key_unchecked (&ke1, &AMP;KS1);D es_set_key_unchecked (&ke2, &AMP;KS2) ;D es_set_key_unchecked (&ke3, &AMP;KS3); memcpy (Ivec, Cbc_iv, sizeof (CBC_IV)); int ilength = Ciphertext.length ()% 8? (Ciphertext.length ()/8 + 1) * 8:ciphertext.length (); unsigned char* tmp = new unsigned char[ilength];memset (TMP, 0, ILe Ngth);D es_ede3_cbc_encrypt ((const unsigned char*) CIPHERTEXT.C_STR (), TMP, Ciphertext.length () +1, &AMP;KS1, &ks2, &AMP;KS3, &ivec, des_decrypt); strcleartext = (char*) tmp;delete [] tmp;} break;} return strcleartext;}

3. Main.cpp:

#include "stdafx.h" #include "cryptotest.h" #include <iostream> #include <string>using namespace std;void Test_des () {String cleartext = "Beijing, China 12345$abcde%[email protected]!!!!"; String ciphertext = ""; string key = "BEIJINGCHINA1234567890ABCDEFGH!!!"; Crypto_mode MODE = Triple_cbc;ciphertext = Des_encrypt (cleartext, Key, MODE); string decrypt = Des_decrypt (ciphertext, key , mode);cout<< "src cleartext:" <<cleartext<<endl;cout<< "genarate ciphertext:" << ciphertext<<endl;cout<< "src ciphertext:" <<ciphertext<<endl;cout<< "genarate Cleartext: "<<decrypt<<endl;if (strcmp (Cleartext.c_str (), decrypt.c_str ()) = = 0) cout<<" DES Crypto OK!!! " <<endl;elsecout<< "DES Crypto Error!!!" <<endl;} int main (int argc, char* argv[]) {test_des ();cout<< "OK!!!" <<endl;return 0;}


Example of use of symmetric encryption algorithm des common functions in OpenSSL

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.