http://www.caole.net/diary/des.html

OpenSSL - DES

 

Summary

This library contains a fast implementation of the DES encryption algorithm.

There are two phases to the use of DES encryption.

  • The first is the generation of a DESkeyschedule from a key,
  • The second is the actual encryption. A DES key is of type DEScblock. This type is consists of 8 bytes with odd parity. The least significant bit in each byte is the parity bit. The key schedule is an expanded form of the key; it is used to speed the encryption process.

DES使用的例子

/**//*
gcc -o des-basic des-basic.c -lcrypto
*/
#include <stdio.h>
#include <openssl/des.h> int main(int argc,char **argv)
{
DES_cblock key;
/**//* DES_random_key(&key); */ /**//* generate a random key */
DES_string_to_key("pass", &key); DES_key_schedule schedule;
DES_set_key_checked(&key, &schedule); const_DES_cblock input = "hehehe";
DES_cblock output; printf("cleartext:%s ", input); DES_ecb_encrypt(&input, &output, &schedule, DES_ENCRYPT);
printf("Encrypted! "); printf("ciphertext:");
int i;
for (i = 0; i < sizeof(input); i++)
printf("%02x", output[i]);
printf(" "); DES_ecb_encrypt(&output, &input, &schedule, DES_DECRYPT);
printf("Decrypted! ");
printf("cleartext:%s ", input); return 0;
}

另一个带注释的例子

// DES encrypt with OpenSSL support
// g++ crypt.cc -lssl -o crypt #include <openssl/des.h>;
#include <iostream>; int main(int argc, char** argv)
{
try
{
std::string keystring("this is my key");// 初始化key(密钥)
DES_cblock key[1]; //具体是什么?不知道也一样可以用
DES_key_schedule key_schedule; //字面意思是密码表 if (NULL != argv[1])
keystring.assign(argv[1]);//如果命令行指定了key,那么就用指定的 // 生成一个 key
DES_string_to_key(keystring.c_str(), key);
if (DES_set_key_checked(key, &key_schedule) != 0)
throw "DES_set_key_schedule"; unsigned char input[] = "this is a text being encrypted by openssl";//需要加密的字符串
size_t len = (sizeof(input)+7)/8 * 8;
unsigned char *output = new unsigned char[len+1];
//加密以后的内容怎么分配内存,照上面这两步走就是了
//错了就干掉贴代码的人:mrgreen:
DES_cblock ivec;
//照这个搞就是了,用别人的代码不一定要知道所由的细节是为什么 // 加密
memset((char*)&ivec, 0, sizeof(ivec));//ivec清0
DES_ncbc_encrypt(input, output, sizeof(input), &key_schedule, &ivec, 1);//这里就进行加密了
std::cout << input << std::endl; //输出加密以后的内容
for (int i = 0; i < len; ++i)
printf("%02x", output[i]);
std::cout << std::endl; // 解密
memset((char*)&ivec, 0, sizeof(ivec));
DES_ncbc_encrypt(output, input, len, &key_schedule, &ivec, 0);
std::cout << input << std::endl; delete[] output;
}
catch(const char* errstr) //对抛出异常的处理
{
std::cerr << errstr << std::endl;
return EXIT_FAILURE;
} return EXIT_SUCCESS;

另一段Code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <openssl/des.h> #define LEN_OF_KEY 24 int main(void)
{
char tmp_str[512];
int tmp_len; int docontinue = 1;
unsigned char *data = "530160";
int data_len;
int data_rest;
unsigned char ch; unsigned char *src = NULL;
unsigned char *dst = NULL;
int len;
unsigned char tmp[8];
unsigned char in[8];
unsigned char out[8]; char *k = "12345678901234567890";
int key_len;
unsigned char key[LEN_OF_KEY];
unsigned char block_key[9];
DES_key_schedule ks,ks2,ks3; key_len = strlen(k);
memcpy(key, k, key_len);
memset(key + key_len, 0x00, LEN_OF_KEY - key_len); data_len = strlen(data);
data_rest = data_len % 8;
len = data_len + (8 - data_rest);
ch = 8 - data_rest; src = malloc(len);
dst = malloc(len);
if (NULL == src || NULL == dst)
{
docontinue = 0;
}
if (docontinue)
{
int count;
int i, j; memset(src, 0, len);
memcpy(src, data, data_len);
memset(src + data_len, ch, 8 - data_rest); memset(block_key, 0, sizeof(block_key));
memcpy(block_key, key + 0, 8);
DES_set_key_unchecked((const_DES_cblock*)block_key, &ks);
memcpy(block_key, key + 8, 8);
DES_set_key_unchecked((const_DES_cblock*)block_key, &ks2);
memcpy(block_key, key + 16, 8);
DES_set_key_unchecked((const_DES_cblock*)block_key, &ks3); printf("before encrypt:");
for (i = 0; i < len; i++)
{
printf("0x%.2X ", *(src + i));
}
printf("\n"); count = len / 8;
for (i = 0; i < count; i++)
{
memset(tmp, 0, 8);
memset(in, 0, 8);
memset(out, 0, 8);
memcpy(tmp, src + 8 * i, 8); DES_ecb3_encrypt((const_DES_cblock*)&tmp, (DES_cblock*)&in, &ks, &ks2, &ks3, DES_ENCRYPT); for(j = 0; j < sizeof(in); j++){
printf("%02x", in[j]);
}
printf("\n"); DES_ecb3_encrypt((const_DES_cblock*)&in, (DES_cblock*)&out, &ks, &ks2, &ks3, DES_DECRYPT); memcpy(dst + 8 * i, out, 8);
} printf("after decrypt :");
for (i = 0; i < len; i++)
{
printf("0x%.2X ", *(dst + i));
} printf("[%s]\n", dst);
} if (NULL != src)
{
free(src);
src = NULL;
}
if (NULL != dst)
{
free(dst);
dst = NULL;
} return 0;
}

Author: Le Cao

Date: 2010-10-12 18:19:41 CST

HTML generated by org-mode TAG=7.01g in emacs 23

OpenSSL加解密的更多相关文章

  1. curses-键盘编码-openssl加解密【转】

    本文转载自;https://zhuanlan.zhihu.com/p/26164115 1.1 键盘编码 按键过程:当用户按下某个键时, 1.键盘会检测到这个动作,并通过键盘控制器把扫描码(scan ...

  2. Openssl 加解密文件

    使用openssl 的命令行进行文件的加密与解密过程,主要有两种方式: openssl 指定加密/解密算法加密 openssl 指定公钥/私钥文件加密 openssl 指定加密/解密算法加密 To E ...

  3. php OpenSSL 加解密

    2018-1-6 17:10:19 星期六 $data = '123456'; $openssl_method = 'AES-256-CBC'; $openssl_iv_length = openss ...

  4. openssl在多平台和多语言之间进行RSA加解密注意事项

    首先说一下平台和语言: 系统平台为CentOS6.3,RSA加解密时使用NOPADDING进行填充 1)使用C/C++调用系统自带的openssl 2)Android4.2模拟器,第三方openssl ...

  5. PHP加密解密方法,使用openssl加密解密

    /** * des 加密算法 */ function do_mencrypt($input, $key) { if (!function_exists("mcrypt_module_open ...

  6. PHP 基础篇 - PHP 中 DES 加解密详解

    一.简介 DES 是对称性加密里面常见一种,全称为 Data Encryption Standard,即数据加密标准,是一种使用密钥加密的块算法.密钥长度是64位(bit),超过位数密钥被忽略.所谓对 ...

  7. .net core中使用openssl的公钥私钥进行加解密

    这篇博文分享的是 C#中使用OpenSSL的公钥加密/私钥解密 一文中的解决方法在 .net core 中的改进.之前的博文针对的是 .NET Framework ,加解密用的是 RSACryptoS ...

  8. openssl - rsa加解密例程

    原文链接: http://www.cnblogs.com/cswuyg/p/3187462.html openssl是可以很方便加密解密的库,可以使用它来对需要在网络中传输的数据加密.可以使用非对称加 ...

  9. openssl enc 加解密

    国内私募机构九鼎控股打造APP,来就送 20元现金领取地址:http://jdb.jiudingcapital.com/phone.html内部邀请码:C8E245J (不写邀请码,没有现金送)国内私 ...

随机推荐

  1. IE下判断IE版本的语句...[if lte IE 6]……[endif]

    <!--[if lte IE 6]> <![endif]--> IE6及其以下版本可见 <!--[if lte IE 7]> <![endif]--> ...

  2. Unity3d:megaFierstext(翻书效果插件)

    附件中是一款翻书效果插件,由于附件上传大小限制,在下载完后,需要在megaFierstext_BHYF\Assets\Resources\Textures下添加图片精灵并修改属性为Texture,即可 ...

  3. C++学习之const整理总结

    1什么是const? (const类型)常类型是指使用类型修饰符const说明的类型,常类型的变量或对象的值是不能被更新的.(但可以偷梁换柱进行更新) 2为什么引入const? const 推出的初始 ...

  4. erlang自定义数据类型

    Erlang系统自带的基础数据类型有:atom.tuple.list.binary.pid.float.number.port.reference.record. 用户可以通过通过命令type来自定义 ...

  5. 用Java实现菱形的打印输出

    import java.util.Scanner; public class dengyao2 { public dengyao2() { super(); } public static void ...

  6. Oracle数据库多语言文字存储解决方案

    一.关于字符集 字符集(也称字元集,Character Set)就是字符编码表(codepage),一个字符不论英文.中文.韩文等在计算机系统内存或硬盘中通过二进制的字节(Byte)保存,这个二进制的 ...

  7. CSS布局经验谈

    1.盒子模型 CSS最具特色也是最本质的可以浓缩成盒子模型. 整个页面可以通过大盒子套小盒子,盒子挨着盒子放,摆成一个页面即可. 盒子即所谓的块元素,只有块元素才有宽和高,有了宽和高才能使盒子挨着盒子 ...

  8. Codeforces Round #336 (Div. 2) C. Chain Reaction set维护dp

    C. Chain Reaction 题目连接: http://www.codeforces.com/contest/608/problem/C Description There are n beac ...

  9. .net MVC 碰到的问题

    1:问:回车会默认会触发页面从左边至右,从上到下索引位置第一的按钮事件.如何取消? 答:在不需要触发按钮事件的按钮中加一个属性:UseSubmitBehavior="false" ...

  10. Java IO之File

    FILE类是用来实现获取文件.文件夹的类库工具,File并不是像类名所表示的那样仅仅是用来表示文件.它还能够用来表示文件夹. 所以能够用File来获取一个文件夹下的全部文件,甚至是文件夹中的文件. 一 ...