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. Delphi Data Types

    http://docwiki.embarcadero.com/RADStudio/XE6/en/Delphi_Data_Types Integer Data Types Type Descriptio ...

  2. uva539 The Settlers of Catan

    The Settlers of Catan Within Settlers of Catan, the 1995 German game of the year, players attempt to ...

  3. 【转】larbin主要代码说明

    转自:http://blog.csdn.net/s030702614/article/details/5683928 1. 主函数: int main (int argc, char *argv[]) ...

  4. 【M27】要求或者禁止对象产生于heap之中

    1.要求对象只能产生于heap之中,该怎么办? 栈上的对象肯定调用构造方法和析构方法(离开作用域的时候),因此,要求对象只能产生于heap之中,也就是禁止栈上产生对象,解决办法有两种:将所有的构造方法 ...

  5. [Jobdu] 题目1527:首尾相连数组的最大子数组和

    题目描述: 给定一个由N个整数元素组成的数组arr,数组中有正数也有负数,这个数组不是一般的数组,其首尾是相连的.数组中一个或多个连续元素可以组成一个子数组,其中存在这样的子数组arr[i],…arr ...

  6. VMWare虚拟机网络的三种工作模式

    VMWare提供了三种工作模式: 1.bridged(桥接模式) 在这种模式下,VMWare虚拟出来的操作系统就像是局域网中的一台独立的主机,它可以访问网内任何一台机器.在桥接模式下,需要手工为虚拟系 ...

  7. Android-Volley详解

    一.Volley简介和特点 1. 简介: 并发.效率.性能 高要求 Volley(齐射,迸发) Volley是Google2013发布的Android平台上的网络通信库 2. Volley特点: 通信 ...

  8. iOS开发——UI高级OC篇&自定义控件之调整按钮中子控件(图片和文字)的位置

    自定义控件之调整按钮中子控件(图片和文字)的位置 其实还有一种是在storyBoard中实现的,只需要设置对应空间的左右间距: 这里实现前面两种自定义的方式 一:imageRectForContent ...

  9. ios开发——实用技术篇&数据保存于恢复

    数据保存于恢复 用户操作(输入数据)之后,应用程序退出并且终止之后,当用户再次打开应用的时候还是保持原来的状态 一:在storyBoard中设置恢复标志符 二:在AppDalegate中代理方法 -( ...

  10. ASP.NET项目从VS2008迁移至VS2010或2012编译不过的问题

    这次将一个VS2008编写的网站迁移至VS2010或者2012下都编译不通过,统统报相同的错误:缺少System.Linq引用,缺少System.Xml.Linq引用,但是明明有引用啊,引用DLL我都 ...