OpenSSL测试-SM4
OpenSSL测试-SM4
任务详情
-
在openEuler(推荐)或Ubuntu或Windows(不推荐)中完成下面任务
-
使用OpenSSL的命令对你的8位学号(数字)进行加密解密,密钥的前8个字节为你的8位学号,提交过程截图(5')
-
使用OpenSSL编程对对"你的8位学号(数字)"进行加密解密,提交代码和运行结果截图。(10’)
-
使用OpenSSL编程对内容为"所有同学的8位学号(数字)"的文件进行加密解密,密钥要包含你的8位学号,提交代码和运行结果截图。(选做(10’))
产生一个装有20201205的文件 a1.txt ,然后计算这个文件的SHA256摘要。等一下用OpenSSL加密产生 b.txt 再对 b.txt 解密产生c.txt 。计算 c.txt 的SHA256摘要,应该和 a1.txt 的一样。
接着,加密 a1.txt ,使用SM4算法。加密后输出文件 b.txt 到当前目录下。
$ openssl enc -in a1.txt -out b.txt -e -sm4-ctr -pbkdf2 -k 123123
![]
$ sha256sum a1.txt b.txt
![]
然后是解密,除了输入文件和输出文件需要修改之外,参数 -e 换成 -d 就是解密过程了,其余的参数跟加密的时候一样。
$ openssl enc -in b.txt -out c.txt -d -sm4-ctr -pbkdf2 -k 120201205
1
看下产生的文件的SHA256摘要,可以发现 a.txt 和 c.txt 摘要跟原来的一样。
第二个:使用OpenSSL编程对"你的8位学号(数字)"进行加密解密
进行了学号的输入:
#include <openssl/rsa.h>
#include <openssl/pem.h>
#include <openssl/err.h>
#include <openssl/evp.h>
#include <openssl/ec.h>
int main(int argc, char *argv[])
{
unsigned char sm4_en[512],sm4_de[512];
int sm4enStrLen,sm4deStrLen;
unsigned char source[7]={0x31,0x33,0x34,0x33,0x45,0x46,0x43};
unsigned char keyStr[16]={0x15,0x67,0x28,0xe1,0x5f,0x9a,0xfc,0x01,0xd4,0xb6,0x1b,0x4e,0x44,0x5d,0xbb,0x26};
sm4enStrLen=my_sm4encrpt(keyStr,source,7,sm4_en);
printf("sm4enStrLen:%d\n",sm4enStrLen);
for(int i=0;i<sm4enStrLen;++i)
{
printf("0x%x",sm4_en[i]);
}
printf("\n");
sm4deStrLen=dencryptStr(keyStr,sm4_en,sm4enStrLen,sm4_de);
printf("sm4deStrLen:%d\n",sm4deStrLen);
for(int i=0;i<7;++i)
{
printf("0x%x",sm4_de[i]);
}
printf("\n");
}
int my_sm4encrpt(unsigned char * keyStr,unsigned char * surbuf,int surlen,unsigned char * enbuf)
{
unsigned char *out_buf = enbuf;
int out_len;
int out_padding_len;
int i;
unsigned char *iv;
EVP_CIPHER_CTX *ctx;
ctx = EVP_CIPHER_CTX_new();
EVP_EncryptInit(ctx, EVP_sm4_ecb(), keyStr, iv);
if( 0 == surlen % 16 )
{
EVP_CIPHER_CTX_set_padding( ctx, 0 );
}
out_len = 0;
EVP_EncryptUpdate(ctx, out_buf, &out_len, surbuf, surlen);
out_padding_len = 0;
EVP_EncryptFinal(ctx, out_buf + out_len, &out_padding_len);
EVP_CIPHER_CTX_free(ctx);
return out_len + out_padding_len;
}
int dencryptStr(unsigned char * sm4PriKey, unsigned char *cEnStr, int cEnstrlen, unsigned char *deStr)
{
unsigned char *iv;
EVP_CIPHER_CTX *ctx;
int len;
int temlen;
int deStrLen;
if (!(ctx = EVP_CIPHER_CTX_new())) {
printf("EVP_CIPHER_CTX_new failed");
}
if (1 != EVP_DecryptInit(ctx, EVP_sm4_ecb(), sm4PriKey, iv)) {
printf("EVP_DecryptInit_ex failed");
}
if (1 != EVP_DecryptUpdate(ctx, deStr, &len, cEnStr, cEnstrlen)) {
printf("EVP_DecryptUpdate failed");
}
if( 0 == len % 16 )
{
EVP_CIPHER_CTX_set_padding( ctx, 0 );
len += 16;
}
if( !EVP_DecryptFinal( ctx, deStr + len, &temlen ) )
{
printf("EVP_DecryptFinal failed");
return EXIT_FAILURE;
}
deStrLen=len+temlen;
printf("解密数据:%d\n",deStrLen);
for (int i = 0;i < deStrLen;i++) {
printf("0x%02x ",*(deStr + i));
}
printf("\n");
EVP_CIPHER_CTX_free(ctx);
return deStrLen;
}
运行结果:
第三个:使用OpenSSL编程对内容为"所有同学的8位学号(数字)"的文件进行加密解密,密钥要包含你的8位学号
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通