商用密码标准实现

文章目录

商用密码标准实现

简述 GM/T0009 4种数据转换的功能,根据你的理解,每种转换功能给出至少一个例子

  • 位串转8位字节串

    • 功能 :将由字符 '0''1' 组成的二进制位串转换为字节串(即字节数组)。每个字节由8个二进制位组成。
    • 例子 :输入位串为 10000010,长度为8。转换后的字节串为 0x82(十六进制表示),对应的十进制值为 130
  • 字节串转位串

    • 功能:将字节串(即字节数组)转换回二进制位串表示。每个字节被分解为其构成的8个二进制位。
    • 例子 :输入字节串为 {0x12, 0x34, 0x56}。转换后的位串为 000100100011010001010110
  • 字节串转整数

    • 功能:将一个字节串解释为一个大端序(Big-Endian)的无符号整数。字节串中的第一个字节是最显著的字节。
    • 例子 :输入字节串为 {0x12, 0x34}。转换后的整数值为 0x1234(十六进制表示),对应的十进制值为 4660
  • 整数转字节串

    • 功能:将一个整数转换为指定长度的大端序(Big-Endian)字节串。如果整数不足以填满指定长度,则高位用零填充。
    • 例子 :输入整数为 1234,期望的字节串长度为2。转换后的字节串为 {0x04, 0xd2}

参考课程代码sdfproject,基于一个模块utils.c,utils.h使用四个函数分别实现4种数据转换的功能

  • utils.c
c 复制代码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "utils.h"

void bitstring_to_bytes(char *bitstring, int blen, char *bytes) {
    int mlen = (blen + 7) / 8;
    for (int i = 0; i < mlen; i++) {
        bytes[i] = 0;
        for (int j = 0; j < 8; j++) {
            int index = blen - 8 * (mlen - 1 - i) - j - 1;
            if (index >= 0 && bitstring[index] == '1') {
                bytes[i] |= 1 << j;
            }
        }
    }
}

void bytes_to_bitstring(char *bytes, int mlen, char *bitstring) {
    int index = 0;
    for (int i = 0; i < mlen; ++i) {
        char binary[9];
        for (int j = 7; j >= 0; --j) {
            binary[7 - j] = ((bytes[i] & (1 << j))? '1' : '0');
        }
        binary[8] = '\0';
        for (int j = 0; j < 8; ++j) {
            bitstring[index++] = binary[j];
        }
    }
    bitstring[index] = '\0';
}

unsigned int bytes_to_int(unsigned char *M, int mlen) {
    unsigned int x = 0;
    for (int i = 0; i < mlen; i++) {
        x += (1 << (8 * (mlen - 1 - i))) * M[i];
    }
    return x;
}

void int_to_bytes(int x, int mlen, unsigned char *M) {
    for (int i = 0; i < mlen; i++) {
        M[i] = (x >> (8 * (mlen - 1 - i))) & 0xFF;
    }
}
  • utils.h
c 复制代码
#ifndef _UTILS_H_
#define _UTILS_H_

// 位串转8位字节串函数声明
void bitstring_to_bytes(char *bitstring, int blen, char *bytes);

// 8位字节串转位串函数声明
void bytes_to_bitstring(char *bytes, int mlen, char *bitstring);

// 8位字节串转整数函数声明
unsigned int bytes_to_int(unsigned char *M, int mlen);

// 整数转8位字节串函数声明
void int_to_bytes(int x, int mlen, unsigned char *M);

#endif

src中在testsdf.c中编写main函数 测试四个转换功能。

  • testsdf.c
c 复制代码
#include <stdio.h>
#include <stdlib.h>
#include "utils.h"

int main() {
    // 测试位串转8位字节串
    {
        char bitstring[] = "10000010";
        int blen = strlen(bitstring);
        char bytes[100];
        bitstring_to_bytes(bitstring, blen, bytes);
        int mlen = (blen + 7) / 8;
        printf("位串转8位字节串测试:\n");
        printf("原始位串: %s\n", bitstring);
        printf("转换后的字节串: ");
        for (int i = 0; i < mlen; i++) {
            printf("%02X ", (unsigned char)bytes[i]);
        }
        printf("\n\n");
    }

    // 测试8位字节串转位串
    {
        char bytes[] = {0x12, 0x34, 0x56};
        int mlen = sizeof(bytes) / sizeof(bytes[0]);
        char bitstring[100];
        bytes_to_bitstring(bytes, mlen, bitstring);
        printf("8位字节串转位串测试:\n");
        printf("原始字节串: ");
        for (int i = 0; i < mlen; i++) {
            printf("%02X ", (unsigned char)bytes[i]);
        }
        printf("\n转换后的位串: %s\n\n", bitstring);
    }

    // 测试8位字节串转整数
    {
        unsigned char M[] = {0x12, 0x34};
        int mlen = sizeof(M) / sizeof(M[0]);
        unsigned int x = bytes_to_int(M, mlen);
        printf("8位字节串转整数测试:\n");
        printf("原始字节串: ");
        for (int i = 0; i < mlen; i++) {
            printf("%02X ", (unsigned char)M[i]);
        }
        printf("\n转换后的整数: %u\n\n", x);
    }

    // 测试整数转8位字节串
    {
        int x = 1234;
        int mlen = 4;
        unsigned char M[100];
        int_to_bytes(x, mlen, M);
        printf("整数转8位字节串测试:\n");
        printf("原始整数: %d\n", x);
        printf("转换后的字节串: ");
        for (int i = 0; i < mlen; i++) {
            printf("%02X ", M[i]);
        }
        printf("\n\n");
    }

    return 0;
}
bash 复制代码
ld@DESKTOP-69L72QA:~/projects/sdfproject$ ./testsdf
Bit string to byte array test:
Original bit string: 10000010
Converted byte array: 82

Byte array to bit string test:
Original byte array: 12 34 56
Converted bit string: 000100100011010001010110

Byte array to integer test:
Original byte array: 12 34
Converted integer: 4660

Integer to byte array test:
Original integer: 1234
Converted byte array: 00 00 04 D2

提交git log结果

bash 复制代码
ld@DESKTOP-69L72QA:~/projects/sdfproject$ git add .
ld@DESKTOP-69L72QA:~/projects/sdfproject$ git commit -m "sm"
[master eb4dc93] sm
 20 files changed, 317 insertions(+)
 create mode 100755 projects/sdfproject/Makefile
 create mode 100755 projects/sdfproject/bin/test
 create mode 100755 projects/sdfproject/bitstr2bytes
 create mode 100755 projects/sdfproject/bytes2bitstr
 create mode 100755 projects/sdfproject/bytes2int
 create mode 100755 projects/sdfproject/compile.sh
 create mode 100755 projects/sdfproject/docs/ref/GMT0018-2012.pdf
 create mode 100755 projects/sdfproject/include/.utils.h.swp
 create mode 100755 projects/sdfproject/include/sdf.h
 create mode 100755 projects/sdfproject/include/utils.h
 create mode 100755 projects/sdfproject/int2byte
 create mode 100755 projects/sdfproject/readme.md
 create mode 100755 projects/sdfproject/src/sdf.c
 create mode 100755 projects/sdfproject/src/utils.c
 create mode 100755 projects/sdfproject/test/main.c
 create mode 100755 projects/sdfproject/testsdf
 create mode 100644 projects/sdfproject/testsdf.c
 create mode 100755 projects/sdfproject/utils
 create mode 100644 projects/sdfproject/utils.c
 create mode 100644 projects/sdfproject/utils.h
ld@DESKTOP-69L72QA:~/projects/sdfproject$ git log
commit eb4dc93452a684bb430ea427109a8c46bb5aacfd (HEAD -> master)
Author: <E6><9F><B3><E7><AC>11537298+liudi20221408@user.noreply.gitee.com>
Date:   Tue Dec 17 12:36:47 2024 +0800

    sm
相关推荐
鱼力舟10 分钟前
【hot100】240搜索二维矩阵
算法
极客先躯1 小时前
说说高级java每日一道面试题-2025年2月13日-数据库篇-请说说 MySQL 数据库的锁 ?
java·数据库·mysql·数据库的锁·模式分·粒度分·属性分
程序员侠客行1 小时前
Spring事务原理 二
java·后端·spring
小猫猫猫◍˃ᵕ˂◍1 小时前
备忘录模式:快速恢复原始数据
android·java·备忘录模式
liuyuzhongcc1 小时前
List 接口中的 sort 和 forEach 方法
java·数据结构·python·list
北_鱼2 小时前
支持向量机(SVM):算法讲解与原理推导
算法·机器学习·支持向量机
五月茶2 小时前
Spring MVC
java·spring·mvc
sjsjsbbsbsn2 小时前
Spring Boot定时任务原理
java·spring boot·后端
yqcoder2 小时前
Express + MongoDB 实现在筛选时间段中用户名的模糊查询
java·前端·javascript
菜鸟蹦迪2 小时前
八股文实战之JUC:ArrayList不安全性
java