$key = '1234567890123456';
$iv = '1234567890123456'; $enc = openssl_encrypt("hello wolrd!", 'aes-256-cbc', $key, OPENSSL_RAW_DATA, $iv);
$decrypted = openssl_decrypt($enc, 'aes-256-cbc', $key, OPENSSL_RAW_DATA, $iv); echo bin2hex($enc)."\r\n";
echo $decrypted;

algo_aes.h

#ifndef ALGO_AES_H
#define ALGO_AES_H int encrypt(unsigned char *plaintext, int plaintext_len, unsigned char *key,
unsigned char *iv, unsigned char *ciphertext); int decrypt(unsigned char *ciphertext, int ciphertext_len, unsigned char *key,
unsigned char *iv, unsigned char *plaintext); #endif

algo_aes.cpp

#include <stdlib.h>
#include <stdio.h>
//#include "algo_aes.h"
#include <openssl/evp.h>
#pragma comment(lib, "libeay32.lib")
void handleErrors(void)
{
//ERR_print_errors_fp(stderr);
abort();
} int encrypt(unsigned char *plaintext, int plaintext_len, unsigned char *key,
unsigned char *iv, unsigned char *ciphertext)
{
EVP_CIPHER_CTX *ctx; int len; int ciphertext_len; /* Create and initialise the context */
if (!(ctx = EVP_CIPHER_CTX_new())) handleErrors(); /* Initialise the encryption operation. IMPORTANT - ensure you use a key
* and IV size appropriate for your cipher
* In this example we are using 256 bit AES (i.e. a 256 bit key). The
* IV size for *most* modes is the same as the block size. For AES this
* is 128 bits */
if (1 != EVP_EncryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, key, iv))
handleErrors(); /* Provide the message to be encrypted, and obtain the encrypted output.
* EVP_EncryptUpdate can be called multiple times if necessary
*/
if (1 != EVP_EncryptUpdate(ctx, ciphertext, &len, plaintext, plaintext_len))
handleErrors();
ciphertext_len = len; /* Finalise the encryption. Further ciphertext bytes may be written at
* this stage.
*/
if (1 != EVP_EncryptFinal_ex(ctx, ciphertext + len, &len)) handleErrors();
ciphertext_len += len; /* Clean up */
EVP_CIPHER_CTX_free(ctx); return ciphertext_len;
} int decrypt(unsigned char *ciphertext, int ciphertext_len, unsigned char *key,
unsigned char *iv, unsigned char *plaintext)
{
EVP_CIPHER_CTX *ctx; int len; int plaintext_len; /* Create and initialise the context */
if (!(ctx = EVP_CIPHER_CTX_new())) handleErrors(); /* Initialise the decryption operation. IMPORTANT - ensure you use a key
* and IV size appropriate for your cipher
* In this example we are using 256 bit AES (i.e. a 256 bit key). The
* IV size for *most* modes is the same as the block size. For AES this
* is 128 bits */
if (1 != EVP_DecryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, key, iv))
handleErrors(); /* Provide the message to be decrypted, and obtain the plaintext output.
* EVP_DecryptUpdate can be called multiple times if necessary
*/
if (1 != EVP_DecryptUpdate(ctx, plaintext, &len, ciphertext, ciphertext_len))
handleErrors();
plaintext_len = len; /* Finalise the decryption. Further plaintext bytes may be written at
* this stage.
*/
if (1 != EVP_DecryptFinal_ex(ctx, plaintext + len, &len)) handleErrors();
plaintext_len += len; /* Clean up */
EVP_CIPHER_CTX_free(ctx); return plaintext_len;
} //#include "algo_aes.h"
#include <stdio.h>
#include <string.h>
//#include <openssl/evp.h> int main(int arc, char *argv[])
{
/* Set up the key and iv. Do I need to say to not hard code these in a
* real application? :-)
*/ /* A 256 bit key */
unsigned char *key = (unsigned char*)"1234567890123456"; /* A 128 bit IV */
unsigned char *iv = (unsigned char*)"1234567890123456"; /* Message to be encrypted */
unsigned char *plaintext = (unsigned char*)"hello wolrd!"; /* Buffer for ciphertext. Ensure the buffer is long enough for the
* ciphertext which may be longer than the plaintext, dependant on the
* algorithm and mode
*/
unsigned char ciphertext[64]; /* Buffer for the decrypted text */
unsigned char decryptedtext[64]; int decryptedtext_len, ciphertext_len; /* Initialise the library */
/* ERR_load_crypto_strings();
OpenSSL_add_all_algorithms();
OPENSSL_config(NULL);*/ printf("Plaintext is:\n%s\n", plaintext); /* Encrypt the plaintext */
ciphertext_len = encrypt(plaintext, strlen((const char*)plaintext), key, iv,
ciphertext); /* Do something useful with the ciphertext here */
printf("Ciphertext is %d bytes long:\n", ciphertext_len);
BIO_dump_fp(stdout, (const char*)ciphertext, ciphertext_len); /* Decrypt the ciphertext */
decryptedtext_len = decrypt(ciphertext, ciphertext_len, key, iv,
decryptedtext); /* Add a NULL terminator. We are expecting printable text */
decryptedtext[decryptedtext_len] = '\0'; /* Show the decrypted text */
printf("Decrypted text is:\n");
printf("%s\n", decryptedtext); /* Clean up */
EVP_cleanup();
//ERR_free_strings();
system("pause"); return 0;
}

php 与 c++ openssl 加密通信的更多相关文章

  1. 开源项目SMSS发开指南(四)——SSL/TLS加密通信详解

    本文将详细介绍如何在Java端.C++端和NodeJs端实现基于SSL/TLS的加密通信,重点分析Java端利用SocketChannel和SSLEngine从握手到数据发送/接收的完整过程.本文也涵 ...

  2. Security基础(三):OpenSSL及证书服务、邮件TLS/SSL加密通信

    一.OpenSSL及证书服务 目标: 本案例要求熟悉OpenSSL工具的基本使用,完成以下任务操作: 使用OpenSSL加密/解密文件 搭建企业自有的CA服务器,为颁发数字证书提供基础环境 方案: 使 ...

  3. SSL及其加密通信过程

    SSL及其加密通信过程 什么是SSL SSL英文全称Secure Socket Layer,安全套接层,是一种为网络通信提供安全以及数据完整性的安全协议,它在传输层对网络进行加密.它主要是分为两层: ...

  4. Python3+ssl实现加密通信

    一.说明 1. python标准库ssl可实现加密通信 2. ssl库底层使用openssl,做了面向对像化改造和简化,但还是可以明显看出openssl的痕迹 3. 本文先给出python实现的soc ...

  5. Filebeat与Logstash配置SSL加密通信

    为了保证应用日志数据的传输安全,我们可以使用SSL相互身份验证来保护Filebeat和Logstash之间的连接. 这可以确保Filebeat仅将加密数据发送到受信任的Logstash服务器,并确保L ...

  6. 开源项目SMSS发开指南(五)——SSL/TLS加密通信详解(下)

    继上一篇介绍如何在多种语言之间使用SSL加密通信,今天我们关注Java端的证书创建以及支持SSL的NioSocket服务端开发.完整源码 一.创建keystore文件 网上大多数是通过jdk命令创建秘 ...

  7. OpenSSL & 加密解密

    OpenSSL&加密解密(思维导图) 1. 网络通信概述 传输层协议 进程间通信 监听端口 SSL 裸套接字 2. 加密和解密 2.1 加密的方式 对称加密 公钥加密 单向加密 认证加密 2. ...

  8. PHP的OpenSSL加密扩展学习(一):对称加密

    我们已经学过不少 PHP 中加密扩展相关的内容了.而今天开始,我们要学习的则是重点中的重点,那就是 OpenSSL 加密扩展的使用.为什么说它是重点中的重点呢?一是 OpenSSL 是目前 PHP 甚 ...

  9. PHP的openssl加密扩展使用小结

    h2:first-child, body>h1:first-child, body>h1:first-child+h2, body>h3:first-child, body>h ...

随机推荐

  1. Hadoop入门必须知道的简单知识

    Hadoop入门知识 Hadoop构成 Hadoop由4个主要构成部分: 1) 基础核心:提供基础的通用的功能 2) HDFS:分布式存储 3) MapReduce:分布式计算 4) YARN:资源分 ...

  2. 一直深深困扰我的问题——hadoop 重启集群后,之前运行的job运行状态都丢失了

    努力之后总是存在回报的,我花了三天时间终于找到了问题所在: 配置文件yarn-site.xml: <property>        <name>yarn.resourcema ...

  3. android开发笔记(一)Android studio 输入法

    以前都是用的时候查资料做些增添即可,现在下决心系统学习下. 首先发现developer.Android.com在开发工具上开始推出了 Android Studio了,不过他自己没有sdk manage ...

  4. python中yield使用

    16.yield使用   列表推导与生成器表达式   当我们创建了一个列表的时候,就创建了一个可以迭代的对象: >>> squares=[n*n for n in range(3)] ...

  5. mac配置python自然语言处理环境

    一.nltk安装 Ⅰ.工具安装步骤 1.根据python版本从 https://pypi.python.org/pypi/setuptools 下载对应版本的setuptools.然后,在终端下运行, ...

  6. git学习------>如何修改git已提交的记录中的Author和Email?

    一.背景 最近搭建好GitLab后,准备陆陆续续的将之前在SVN仓库中保存的代码迁移到GitLab上,昨天顺利将三个Android组件的代码迁移到GitLab后,其他同事发现迁移是成功了,但是pull ...

  7. 【云安全与同态加密_调研分析(4)】云计算安全领域主要研究成果——By Me

    下表列举了在云安全问题研究表现突出的ICT公司和研究机构以及其在云计算安全方面主要研究成果: ◆ICT公司和研究机构(云计算安全领域主要研究成果)◆ ◆机构名称◆ ◆机构类别◆ ◆主要研究成果◆ ◆备 ...

  8. 安装odoo过程中出现的问题

    一 centos6.5 1. simplejson error:module not found fix: easy_install simplejson 2. python version erro ...

  9. odoo学习:创建新数据库及修改数据库内容

    1.切换到odoo用户 su - odoo -s /bin/bash 2. 创建新数据库 createdb v8dev 3. 初始化数据库,并配置odoo数据模式 chmod +x odoo: odo ...

  10. linux的虚拟机时间和物理机时间同步

    1.查看和设置物理机硬件时间 查看硬件时间# hwclock --show或# clock --show 设置硬件时间# hwclock --set --date=”01/14/19 10:19″(月 ...