实习日志21

本文介绍了如何在WebStorm中使用SM3和SM4加密算法,包括编码、解码操作,以及如何将这些算法适配到活字格环境,提供加密和解密函数,并展示了加密和解密的具体实现方法,包括HMAC和CBC模式。
摘要由CSDN通过智能技术生成

1.找了很多sm3、sm4加密算法相关的库

1.1.sm4算法编码和解码可以在webstorm中直接执行

1.2.封装的真好,活字格里用不了一点

1.3.把sm3和sm4加密算法源码读透后剥离出来加入到活字格的资源管理器中即可使用

把调用函数改为活字格支持的普通函数

2.sm3加密改法

2.1.改函数


// module.exports = function (input, options) {
//   input = typeof input === 'string' ? utf8ToArray(input) : Array.prototype.slice.call(input)
//
//   if (options) {
//     const mode = options.mode || 'hmac'
//     if (mode !== 'hmac') throw new Error('invalid mode')
//
//     let key = options.key
//     if (!key) throw new Error('invalid key')
//
//     key = typeof key === 'string' ? hexToArray(key) : Array.prototype.slice.call(key)
//     return ArrayToHex(hmac(input, key))
//   }
//
//   return ArrayToHex(sm3(input))
// }
// sm3加密
function sm3Encrypt(input, options) {
  input = typeof input === 'string' ? utf8ToArray(input) : Array.prototype.slice.call(input)

  if (options) {
    const mode = options.mode || 'hmac'
    if (mode !== 'hmac') throw new Error('invalid mode')

    let key = options.key
    if (!key) throw new Error('invalid key')

    key = typeof key === 'string' ? hexToArray(key) : Array.prototype.slice.call(key)
    return ArrayToHex(hmac(input, key))
  }

  return ArrayToHex(sm3(input))
}

2.2.调用

/**
 * SM3 算法加密
 *
 * @param {string} paramStr - 待加密字符串
 * @returns {string} - 返回加密后,固定长度=32的16进制字符串
 */
function encodeSM3(paramStr) {
    try {
        const resultHexString = sm3Encrypt(paramStr);
        return resultHexString;
    } catch (error) {
        console.error('Encryption error:', error);
        return '';
    }
}

3.sm4加密、解密改法

3.1.改函数

/**
 * sm4加密
 */
function sm4Encrypt(inArray, key, options) {
  return sm4(inArray, key, 1, options)
}

/**
 * sm4解密
 */
function sm4Decrypt(inArray, key, options) {
  return sm4(inArray, key, 0, options)
}

3.2.文件有俩

3.3.调用

加密

/**
 * SM4 算法加密
 *
 * @param {string} paramStr - 待加密字符串
 * @returns {string} - 返回加密后,固定长度=32的16进制字符串
 */
function encodeSM4(paramStr) {

    /* 密钥,32位十六进制数字 */
    // const key_sm4 = '密钥';
    // const plaintext = `keqingrong@outlook.com`;
    // const key_sm4 = token; //密钥key == 授权值token
    // const key_sm4 = '密钥'; //密钥key == 授权值token
    // console.log('key:' + key_sm4);

    // 默认 ECB 模式
    const encrypted = sm4Encrypt(paramStr, key_sm4);
    // const decrypted = sm4Decrypt(encrypted, key_sm4);
    // // console.log(encrypted); 
    // // console.log(decrypted === plaintext); // true

    // // CBC 模式
    // /* 初始化向量,32位十六进制数字 */
    // const iv = '密钥';
    // const iv = token;
    // //加密
    // const encrypted2 = sm4.encrypt(plaintext, key, {
    //     iv,
    //     mode: 'cbc'
    // });
    // //解密
    // const decrypted2 = sm4.decrypt(encrypted2, key, {
    //     iv,
    //     mode: 'cbc'
    // });
    // // console.log(encrypted2); // 加密后
    // // console.log(decrypted2 === plaintext); // true
    return encrypted;
}

解密:

注:此处还加了个base64解密

/**
 * data解密解密
 *
 * @param data 加密且编码的data
 * @returns {string} 返回json格式的data数据
 */
function decodeData(data) {
    // sm4解密
    const sm4Data = decodeSM4(data);
    // console.log('sm4Data: ' + sm4Data)

    //base64解码
    // 判断数据是否是有效的Base64字符串
    if (isValidBase64(sm4Data)) {
        //是有效的Base64字符串
        //解密
        const jsonData = Base64.decode(sm4Data);
        // console.log('jsonData:', jsonData);
        return jsonData;
    } else {
        console.error('Invalid Base64 string:', sm4Data);
    }
}

  • 6
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值