OpenSSL加解密
http://www.caole.net/diary/des.html
Table of Contents
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加解密的更多相关文章
- curses-键盘编码-openssl加解密【转】
本文转载自;https://zhuanlan.zhihu.com/p/26164115 1.1 键盘编码 按键过程:当用户按下某个键时, 1.键盘会检测到这个动作,并通过键盘控制器把扫描码(scan ...
- Openssl 加解密文件
使用openssl 的命令行进行文件的加密与解密过程,主要有两种方式: openssl 指定加密/解密算法加密 openssl 指定公钥/私钥文件加密 openssl 指定加密/解密算法加密 To E ...
- php OpenSSL 加解密
2018-1-6 17:10:19 星期六 $data = '123456'; $openssl_method = 'AES-256-CBC'; $openssl_iv_length = openss ...
- openssl在多平台和多语言之间进行RSA加解密注意事项
首先说一下平台和语言: 系统平台为CentOS6.3,RSA加解密时使用NOPADDING进行填充 1)使用C/C++调用系统自带的openssl 2)Android4.2模拟器,第三方openssl ...
- PHP加密解密方法,使用openssl加密解密
/** * des 加密算法 */ function do_mencrypt($input, $key) { if (!function_exists("mcrypt_module_open ...
- PHP 基础篇 - PHP 中 DES 加解密详解
一.简介 DES 是对称性加密里面常见一种,全称为 Data Encryption Standard,即数据加密标准,是一种使用密钥加密的块算法.密钥长度是64位(bit),超过位数密钥被忽略.所谓对 ...
- .net core中使用openssl的公钥私钥进行加解密
这篇博文分享的是 C#中使用OpenSSL的公钥加密/私钥解密 一文中的解决方法在 .net core 中的改进.之前的博文针对的是 .NET Framework ,加解密用的是 RSACryptoS ...
- openssl - rsa加解密例程
原文链接: http://www.cnblogs.com/cswuyg/p/3187462.html openssl是可以很方便加密解密的库,可以使用它来对需要在网络中传输的数据加密.可以使用非对称加 ...
- openssl enc 加解密
国内私募机构九鼎控股打造APP,来就送 20元现金领取地址:http://jdb.jiudingcapital.com/phone.html内部邀请码:C8E245J (不写邀请码,没有现金送)国内私 ...
随机推荐
- Fragment初步了解
fragment 1.fragment解释: 从英文上来说fragment是碎片和片段的意思,这解释的是相当到位的,因为android中的fragment就像是碎片嵌在了Activity当中的,为构造 ...
- 4.接口隔离原则(Interface Segregation Principle)
1.定义 客户端不应该依赖它不需要的接口: 一个类对另一个类的依赖应该建立在最小的接口上. 2.定义解读 定义包含三层含义: 一个类对另一个类的依赖应该建立在最小的接口上: 一个接口代表一个角色,不应 ...
- ASP.NET 最佳DataGrid读取单元格的值
采用asp:BoundColumn列 前台: <asp:BoundColumn DataField="UserID" HeaderText="工号"> ...
- 用一个I/O口控制1个三色指示灯, 2个单色指示灯
http://www.baiheee.com/Documents/081207/081207184434.htm http://www.baiheee.com/Documents/081207/081 ...
- 大话数据结构—平衡二叉树(AVL树)
平衡二叉树(Self-Balancing Binary Search Tree/Height-Balanced Binary Search Tree),是一种二叉排序树,当中每个节点的左子树和右子树的 ...
- 关于SPA及RPA
SPA Request-------->Callback (page onload,execute <script>)-------->call func RPA URL re ...
- oc-21-class对象
/** 什么是类对象(Class对象)? 类在内存当中也是以对象形式进行存储的. 1.类对象的类型:Class类型 2.如何创建类对象: 1)Class 类对象名 = [类名 class]; 2)类名 ...
- TLS握手优化详解
随着 HTTP/2 的逐渐普及,以及国内网络环境越来越糟糕(运营商劫持和篡改),HTTPS 已经开始成为主流.HTTPS 在 TCP 和 HTTP 之间增加了 TLS(Transport Laye ...
- 读懂IL代码就这么简单
原文地址:http://www.cnblogs.com/zery/p/3366175.html 一前言 感谢 @冰麟轻武 指出文章的错误之处,现已更正 对于IL代码没了解之前总感觉很神奇,初一看完全不 ...
- 代码片段--Makefile之大型工程项目子目录Makefile的一种通用写法
转载:http://blog.csdn.net/mo_hui123456/article/details/8929615 管理Linux环境下的C/C++大型项目,如果有一个智能的Build Syst ...