商用密码标准实现

文章目录

商用密码标准实现

简述 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>[email protected]>
Date:   Tue Dec 17 12:36:47 2024 +0800

    sm
相关推荐
小羊不会c++吗(黑客小羊)6 分钟前
c++头文件知识
算法
拓端研究室TRL26 分钟前
PyMC+AI提示词贝叶斯项目反应IRT理论Rasch分析篮球比赛官方数据:球员能力与位置层级结构研究
大数据·人工智能·python·算法·机器学习
长沙火山33 分钟前
9.ArkUI List的介绍和使用
数据结构·windows·list
purrrew36 分钟前
【JAVA ee初阶】多线程(3)
java·开发语言
每次的天空1 小时前
Android学习总结之Java篇(一)
android·java·学习
尤物程序猿1 小时前
【2025最新Java面试八股】如何在Spring启动过程中做缓存预热?
java·缓存·面试
CoovallyAIHub1 小时前
Vision Transformers与卷积神经网络详细训练对比(附代码)
深度学习·算法·计算机视觉
地平线开发者1 小时前
征程 6 逆向自证hbm与bc一致性
算法·自动驾驶
算AI1 小时前
LLM用于科学假设生成:探索与挑战
人工智能·算法
1白天的黑夜11 小时前
贪心算法-2208.将数组和减半的最小操作数-力扣(LeetCode)
c++·算法·leetcode·贪心算法