当前位置:   article > 正文

OpenSSL中对称加密算法DES常用函数使用举例_openssl/des.h

openssl/des.h

主要包括3个文件:

  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_
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  1. 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_Encrypt(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);
 
			DES_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);
				DES_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);
				memcpy(inputText, cleartext.c_str() + tmp1, tmp2);
 
				DES_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);
 
			DES_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);
 
			DES_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);
 
			DES_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();
 
			DES_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() >= 24) {
				memcpy(ke1, key.c_str(), 8);
				memcpy(ke2, key.c_str() + 8, 8);
				memcpy(ke3, key.c_str() + 16, 8);
			} else if (key.length() >= 16) {
				memcpy(ke1, key.c_str(), 8);
				memcpy(ke2, key.c_str() + 8, 8);
				memcpy(ke3, key.c_str() + 16, 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, &ks1);
			DES_set_key_unchecked(&ke2, &ks2);
			DES_set_key_unchecked(&ke3, &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, cleartext.c_str() + i * 8, 8);
				DES_ecb3_encrypt(&inputText, &outputText, &ks1, &ks2, &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);
 
				DES_ecb3_encrypt(&inputText, &outputText, &ks1, &ks2, &ks3, 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 TRIPLE_CBC:
		{
			DES_cblock ke1, ke2, ke3, ivec;
			memset(ke1, 0, 8);
			memset(ke2, 0, 8);
			memset(ke2, 0, 8);
 
			if (key.length() >= 24) {
				memcpy(ke1, key.c_str(), 8);
				memcpy(ke2, key.c_str() + 8, 8);
				memcpy(ke3, key.c_str() + 16, 8);
			} else if (key.length() >= 16) {
				memcpy(ke1, key.c_str(), 8);
				memcpy(ke2, key.c_str() + 8, 8);
				memcpy(ke3, key.c_str() + 16, 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, &ks1);
			DES_set_key_unchecked(&ke2, &ks2);
			DES_set_key_unchecked(&ke3, &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);
 
			DES_ede3_cbc_encrypt((const unsigned char*)cleartext.c_str(), tmp, cleartext.length()+1, &ks1, &ks2, &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 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);
 
			DES_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, ciphertext.c_str() + i * 8, 8);
				DES_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(inputText, 0, 8);
				memcpy(inputText, ciphertext.c_str() + tmp1, tmp2);
 
				DES_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);
 
			DES_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, iLength);
 
			DES_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);
 
			DES_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();
 
			DES_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() >= 24) {
				memcpy(ke1, key.c_str(), 8);
				memcpy(ke2, key.c_str() + 8, 8);
				memcpy(ke3, key.c_str() + 16, 8);
			} else if (key.length() >= 16) {
				memcpy(ke1, key.c_str(), 8);
				memcpy(ke2, key.c_str() + 8, 8);
				memcpy(ke3, key.c_str() + 16, 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, &ks1);
			DES_set_key_unchecked(&ke2, &ks2);
			DES_set_key_unchecked(&ke3, &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);
				DES_ecb3_encrypt(&inputText, &outputText, &ks1, &ks2, &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(inputText, 0, 8);
				memcpy(inputText, ciphertext.c_str() + tmp1, tmp2);
 
				DES_ecb3_encrypt(&inputText, &outputText, &ks1, &ks2, &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() >= 24) {
				memcpy(ke1, key.c_str(), 8);
				memcpy(ke2, key.c_str() + 8, 8);
				memcpy(ke3, key.c_str() + 16, 8);
			} else if (key.length() >= 16) {
				memcpy(ke1, key.c_str(), 8);
				memcpy(ke2, key.c_str() + 8, 8);
				memcpy(ke3, key.c_str() + 16, 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, &ks1);
			DES_set_key_unchecked(&ke2, &ks2);
			DES_set_key_unchecked(&ke3, &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, iLength);
 
			DES_ede3_cbc_encrypt((const unsigned char*)ciphertext.c_str(), tmp, ciphertext.length()+1, &ks1, &ks2, &ks3, &ivec, DES_DECRYPT);
 
			strClearText = (char*)tmp;
 
			delete [] tmp;
		}
		break;
	}
 
	return strClearText;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
  • 233
  • 234
  • 235
  • 236
  • 237
  • 238
  • 239
  • 240
  • 241
  • 242
  • 243
  • 244
  • 245
  • 246
  • 247
  • 248
  • 249
  • 250
  • 251
  • 252
  • 253
  • 254
  • 255
  • 256
  • 257
  • 258
  • 259
  • 260
  • 261
  • 262
  • 263
  • 264
  • 265
  • 266
  • 267
  • 268
  • 269
  • 270
  • 271
  • 272
  • 273
  • 274
  • 275
  • 276
  • 277
  • 278
  • 279
  • 280
  • 281
  • 282
  • 283
  • 284
  • 285
  • 286
  • 287
  • 288
  • 289
  • 290
  • 291
  • 292
  • 293
  • 294
  • 295
  • 296
  • 297
  • 298
  • 299
  • 300
  • 301
  • 302
  • 303
  • 304
  • 305
  • 306
  • 307
  • 308
  • 309
  • 310
  • 311
  • 312
  • 313
  • 314
  • 315
  • 316
  • 317
  • 318
  • 319
  • 320
  • 321
  • 322
  • 323
  • 324
  • 325
  • 326
  • 327
  • 328
  • 329
  • 330
  • 331
  • 332
  • 333
  • 334
  • 335
  • 336
  • 337
  • 338
  • 339
  • 340
  • 341
  • 342
  • 343
  • 344
  • 345
  • 346
  • 347
  • 348
  • 349
  • 350
  • 351
  • 352
  • 353
  • 354
  • 355
  • 356
  • 357
  • 358
  • 359
  • 360
  • 361
  • 362
  • 363
  • 364
  • 365
  • 366
  • 367
  • 368
  • 369
  • 370
  • 371
  • 372
  • 373
  • 374
  • 375
  • 376
  • 377
  • 378
  • 379
  • 380
  • 381
  • 382
  • 383
  • 384
  • 385
  • 386
  • 387
  • 388
  • 389
  • 390
  • 391
  • 392
  • 393
  • 394
  • 395
  • 396
  • 397
  • 398
  • 399
  • 400
  • 401
  • 402
  • 403
  • 404
  • 405
  • 406
  • 407
  • 408
  • 409
  • 410
  • 411
  • 412
  • 413
  • 414
  • 415
  • 416
  • 417
  • 418
  • 419
  • 420
  • 421
  • 422
  • 423
  • 424
  • 425
  • 426
  • 427
  • 428
  • 429
  • 430
  • 431
  • 432
  • 433
  1. main.cpp:
#include "stdafx.h"
#include "cryptotest.h"
#include <iostream>
#include <string>
 
using namespace std;
 
void test_DES()
{
	string cleartext = "中国北京12345$abcde%ABCDE@!!!!";
	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;
	else
		cout<<"DES crypto error!!!"<<endl;
}
 
 
int main(int argc, char* argv[])
{
	test_DES();
 
	cout<<"ok!!!"<<endl;
 
	return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38

GitHub: https://github.com/fengbingchun/OpenSSL_Test

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/308392
推荐阅读
相关标签
  

闽ICP备14008679号