OpenSSL测试-SM4

发布时间 2023-04-12 09:46:10作者: 西宁西

OpenSSL测试-SM4
任务详情

  1. 在openEuler(推荐)或Ubuntu或Windows(不推荐)中完成下面任务

  2. 使用OpenSSL的命令对你的8位学号(数字)进行加密解密,密钥的前8个字节为你的8位学号,提交过程截图(5')

  3. 使用OpenSSL编程对对"你的8位学号(数字)"进行加密解密,提交代码和运行结果截图。(10’)

  4. 使用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位学号