数据转换-位串字节数组

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

1 参考《GMT 0009-2012 SM2密码算法使用规范》第6节“数据转换” 在utils.h和utils.c中完成位串与8位字节串的转换功能(10'):
int Bitstr2ByteArr(unsigned char * bs, unsigned char * ba,int *lba);
int ByteArr2Bitstr(unsigned char * ba, unsigned char * bs, int *lbs);

2 并写出测试代码测试上述函数(不能与下面代码一样),比如(10'):

unsigned char bs [] = "1010010100011100";
int len;
Bitstr2ByteArr(bs, char * ba, &len);
//结果:ba = {0x5, 0x1C}; len=2

char ba[] = {0x5, 0x1C}
ByteArr2Bitstr(char * ba, char * bs);
//结果:bs= "1010010100011100"
3 提交代码(或代码链接)和运行结果

#include "sdf.h"
#include <stdio.h>
#include <stdlib.h>

int main(){

    void ** pdh;
    pdh = (void **) malloc(20);
    int ret;

    ret = SDF_OpenDevice(pdh);
    if(ret != SDR_OK){
        printf("error!");
    } else {
        printf("device opened!\n");
    }

    DEVICEINFO testdi;
    ret = SDF_GetDeviceInfo(pdh, &testdi);
    if(ret != SDR_OK){
        printf("error!");
    } else {
        printf("Issuer Name: %s\n", testdi.IssuerName);
        printf("Device Name: %s\n", testdi.DeviceName);
        printf("Device Serial: %s\n", testdi.DeviceSerial);
        printf("Device Version: %d\n", testdi.DeviceVersion);

    }

    char pRandom[10];

    ret = SDF_GenerateRandom(*pdh,10, pRandom);
    if(ret != SDR_OK){
        printf("error!");
    } else {
        for(int i=0; i<10; i++)
            printf("%d\n", pRandom[i]);
    }


    ret = SDF_CloseDevice(*pdh);

    if(ret != SDR_OK){
        printf("error!");
    } else {
        free(pdh);
        printf("device closed!\n");
    }

    return 0;
}
#include "sdf.h"
#include <string.h>
#include <time.h>
#include <stdlib.h>

//********************************
//设备管理
//********************************

int SDF_OpenDevice(void ** phDeviceHandle){
    return SDR_OK;
}


int SDF_CloseDevice(void *hDeviceHandle){

    return SDR_OK;
}

int SDF_GetDeviceInfo( void * hSessionHandle, DEVICEINFO * pstDeviceInfo) {

    DEVICEINFO di;
    strcpy(di.IssuerName,"RocSDF");
    strcpy(di.DeviceName,"SDFBESTI");
    strcpy(di.DeviceSerial,"2021040001");
    di.DeviceVersion = 1;
    //...

    //pstDevicelnfo = &di;
    *pstDeviceInfo = di;

    return SDR_OK;
}

static int myRandom(){
    srand((unsigned)time(NULL));
    return rand();
}

int SDF_GenerateRandom (void * hSessionHandle, unsigned int uiLength, unsigned char * pucRandom){


    for(int i=0; i<uiLength; i++){
        sleep(2);
        *(pucRandom+i) = myRandom();
       // pucRandom[i] = i;
    }

    return SDR_OK;
}
#include <stdio.h>
#include "util.h"

char HStr = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
char Hex2Char(int i){
    return HStr[i];
}

对我的学号进行转换

 

posted @ 2022-06-02 14:12  ewdbdvdv  阅读(40)  评论(0编辑  收藏  举报