首页 > 其他分享 >源代码

源代码

时间:2023-10-20 11:56:48浏览次数:30  
标签:const len EVP 源代码 data tc out

SM3:
echo -n "abc" | ./apps/openssl dgst -SM3
SM2:
./apps/openssl ecparam -list_curves | grep SM2
SM4:
/** 文件名: https://github.com/liuqun/openssl-sm4-demo/blob/cmake/src/main.c */

include <stddef.h>

include <stdio.h>

include <stdlib.h>

include <string.h>

include "openssl/err.h"

include "openssl/evp.h"

/* Before OpenSSL 1.1.1-pre1, we did not have EVP_sm4_ecb() */

if defined(OPENSSL_VERSION_NUMBER) \

&& OPENSSL_VERSION_NUMBER < 0x10101001L

static const EVP_CIPHER (EVP_sm4_ecb)()=EVP_aes_128_ecb;

endif

typedef struct {
const unsigned char *in_data;
size_t in_data_len;
int in_data_is_already_padded;
const unsigned char *in_ivec;
const unsigned char *in_key;
size_t in_key_len;
} test_case_t;

void test_encrypt_with_cipher(const test_case_t *in, const EVP_CIPHER *cipher)
{
unsigned char *out_buf = NULL;
int out_len;
int out_padding_len;
EVP_CIPHER_CTX *ctx;

ctx = EVP_CIPHER_CTX_new();
EVP_EncryptInit_ex(ctx, cipher, NULL, in->in_key, in->in_ivec);

if (in->in_data_is_already_padded)
{
    /* Check whether the input data is already padded.
    And its length must be an integral multiple of the cipher's block size. */
    const size_t bs = EVP_CIPHER_block_size(cipher);
    if (in->in_data_len % bs != 0)
    {
        printf("ERROR-1: data length=%d which is not added yet; block size=%d\n", (int) in->in_data_len, (int) bs);
        /* Warning: Remember to do some clean-ups */
        EVP_CIPHER_CTX_free(ctx);
        return;
    }
    /* Disable the implicit PKCS#7 padding defined in EVP_CIPHER */
    EVP_CIPHER_CTX_set_padding(ctx, 0);
}
out_buf = (unsigned char *) malloc(((in->in_data_len>>4)+1) << 4);
out_len = 0;
EVP_EncryptUpdate(ctx, out_buf, &out_len, in->in_data, in->in_data_len);
if (1)
{
    printf("Debug: out_len=%d\n", out_len);
}
out_padding_len = 0;
EVP_EncryptFinal_ex(ctx, out_buf+out_len, &out_padding_len);
if (1)
{
    printf("Debug: out_padding_len=%d\n", out_padding_len);
}
EVP_CIPHER_CTX_free(ctx);
if (1)
{
    int i;
    int len;
    len = out_len + out_padding_len;
    for (i=0; i<len; i++)
    {
        printf("%02x ", out_buf[i]);
    }
    printf("\n");
}
if (out_buf)
{
    free(out_buf);
    out_buf = NULL;
}

}
void main()
{
int have_sm4 = (OPENSSL_VERSION_NUMBER >= 0x10101001L);
int have_aes = 1;
const unsigned char data[]=
{
0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
};
unsigned char ivec[EVP_MAX_IV_LENGTH]; ///< IV 向量
const unsigned char key1[16] = ///< key_data, 密钥内容, 至少16字节
{
0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
};
test_case_t tc;
tc.in_data = data;
tc.in_data_len = sizeof(data);
tc.in_data_is_already_padded = (tc.in_data_len % 16)==0; // Hard coded 16 as the cipher's block size
tc.in_key = key1;
tc.in_key_len = sizeof(key1);
memset(ivec, 0x00, EVP_MAX_IV_LENGTH);
tc.in_ivec = ivec;

if defined(OPENSSL_NO_SM4)

have_sm4 = 0;

endif

if (have_sm4)
{
    printf("[1]\n");
    printf("Debug: EVP_sm4_ecb() test\n");
    test_encrypt_with_cipher(&tc, EVP_sm4_ecb());
}

if defined(OPENSSL_NO_AES)

have_aes = 0;

endif

if (have_aes)
{
    printf("[2]\n");
    printf("Debug: EVP_aes_128_ecb() test\n");
    test_encrypt_with_cipher(&tc, EVP_aes_128_ecb());
}

}

gcc -Iinclude -c main.c
gcc main.o libcrypto.so -o a.out
export LD_LIBRARY_PATH=pwd
ldd a.out
./a.out
对称算法:
./apps/openssl enc -ciphers

标签:const,len,EVP,源代码,data,tc,out
From: https://www.cnblogs.com/zhouruiya/p/17776750.html

相关文章

  • 如何保护价值上千万的Node.js源代码?
    如何保护价值上千万的Node.js源代码?https://zhuanlan.zhihu.com/p/843864561|0一个强大的JS混淆器。github.com/javascript-obfuscator/javascript-obfuscator2|0一套JS代码安全问题解决方案。www.jshaman.com3|0一个极简的Node.js字节码编译器。github.com/OsamaAbbas/byte......
  • app直播源代码,监听EditText输入框内输入内容的变化
    app直播源代码,监听EditText输入框内输入内容的变化输入框在开发中是一个很常见的控件,可能很多时候我们不仅仅是用来简单的用它来输入内容这么简单,有时候可能还需要拿到甚至是监听输入框里面的内容,然后作出一个合理的逻辑判断,对EditText监听安卓系统提供了TextWatcher来监听输入框......
  • 直播系统源代码,单选按钮和复选框
    直播系统源代码,单选按钮和复选框 <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"  android:layout_width="match_parent"  android:layout_height="match_parent"  android:background="@drawable/background&qu......
  • 直播平台源代码,自定义设置 View 四个角的圆角 以及边框的设置
    直播平台源代码,自定义设置View四个角的圆角以及边框的设置使用贝塞尔曲线 typedefNS_OPTIONS(NSUInteger,UIRectCorner){  UIRectCornerTopLeft   =1<<0,  UIRectCornerTopRight  =1<<1,  UIRectCornerBottomLeft =1<<2,  UIRectCo......
  • 直播平台源代码,bmob_gudongStpeAdd
    直播平台源代码,bmob_gudongStpeAddfunctiononRequest(request,response,modules){ varaccess_token;varstpe=0; if(request.body.stpe){access_token=request.body.access_token;stpe=request.body.stpe;}  if(request.query.stpe){access_token=request.query.a......
  • app直播源代码,android中几种常用的弹框
    app直播源代码,android中几种常用的弹框一、SweetAlertDialog弹框使用该控件需要添加依赖: implementation'com.github.f0ris.sweetalert:library:1.5.1'​下面是具体用法:  newSweetAlertDialog(this,SweetAlertDialog.WARNING_TYPE)        .setTitl......
  • HashMap源代码的阅读
    Createdby徐庆杰,lastmodifiedon八月08,2023从名字上来看,HashMap应该是通过HashCode的方法存储Key值已达到降低检索时间复杂度的目的的HashMap继承了AbstractMap<K,V>抽象类,并且继承了Map<K,V>接口而AbstractMap<K,V>实现了Map<K,V>中的部分方法default关键字在翻找M......
  • 《流畅的Python》高清高质量电子书PDF+源代码
    下载:https://pan.quark.cn/s/02b9697a37d0......
  • 使用requests.get()得到的源代码与浏览器中的不一样
    用requests.get()之后得到的源代码跟浏览器里面的不一样崔庆才的《python3网络爬虫开发实战》在2.3.3节:我们用urllib或requests获取到的是HTML源代码,但是这个网页是用js渲染的,上面两个库不会像浏览器那样继续运行后面请求来的js模块,所以不一样。 ==================Selenium是一个......
  • 最短路径问题 java实现 源代码
    最短路径问题 java实现源代码下载地址:用到的资源文件 文件名 shortPath.propertiesbegin=/u59CB/u53D1/u5730/uFF1Aclear=/u6E05/u9664clearString=/u6E05/u695A/u7ED8/u56FE/u533A/u548C/u6240/u6709/u7684/u6587/u672CdrawLine=/u7ED8/u5236/u8DEF/u5F84end=/u76EE/......