int MPRAsymmCrypt_GenerateKeyPair(PKI_Algorithm alg, //[IN]非对称加密算法 const unsigned int key_bits, //[IN]加密算法长度, SM2算法192或256, RSA算法1024或2048 const unsigned char *p_in_data, //[IN]需要加密明文首地址 unsigned char *p_pubkey, //[IN]公钥首地址 unsigned int *pubkey_len, //[IN/OUT]输入buff长度, 返回公钥长度,建议key_bits/8 unsigned char *p_prvkey, //[IN]私钥首地址 unsigned int *prvkey_len) //[OUT]输入buff长度, 返回私钥长度,建议key_bits/16 { int rt = 0; if(p_pubkey == NULL || pubkey_len == NULL || p_prvkey == NULL || prvkey_len == NULL|| key_bits == 0){ return BAD_PARAM; } switch(alg) { case SM2: rt = SDF_GenerateKeyPair_ECC(key_bits, (char *)p_pubkey, (int *)pubkey_len, (char *)p_prvkey, (int *)prvkey_len); if(SDR_OK != rt){ return rt; } return 0; case RSA: return BAD_CALL; break; default: return BAD_PARAM; break; } return BAD_PARAM; }
int main(int argc, char **argv) { void *hSessionHandle; SGD_HANDLE hDeviceHandle; /*设备句柄 typedef void* SGD_HANDLE;*/ int ret; if(0 != (ret = SDF_OpenDevice(&hDeviceHandle))) print_error_msg(ret, "设备打开失败"); if(0 != (ret = SDF_OpenSession(hDeviceHandle, &hSessionHandle))) print_error_msg(ret, "建立会话句柄失败"); unsigned int algorithm_id = SGD_SM2_3; // 指定算法标识 unsigned int key_len = 256; ECCrefPublicKey public_key; ECCrefPrivateKey private_key; if(0 != (ret = SDF_GenerateKeyPair_ECC(hSessionHandle, algorithm_id, key_len, &public_key, &private_key))) { print_error_msg(ret, "产生 ECC 密钥对失败"); } unsigned char puc_old_data[9] = {'w', '3', 'd', 'r', 'w', '3', 'd', 'r', '0'}; // 需要加密的明文 unsigned int puc_len_data = 8; ECCCipher enc_data; //加密之后的密文 if(0 != (ret = SDF_ExternalEncrypt_ECC(hSessionHandle, algorithm_id, &public_key, puc_old_data, puc_len_data, &enc_data))) { print_error_msg(ret, "数据加密失败"); } unsigned char puc_new_data[9] = {'0'}; //解密数据 unsigned int new_len; // 解密得到的数据长度 if(0 != (ret = SDF_ExternalDecrypt_ECC(hSessionHandle, algorithm_id, &private_key, &enc_data, puc_new_data, &new_len))) { print_error_msg(ret, "数据解密失败"); } printf("puc_new_data--> %s, %d", puc_new_data, new_len); if(0 != (ret = SDF_CloseSession(hSessionHandle))) print_error_msg(ret, "关闭会话句柄失败"); if(0 != (ret = SDF_CloseDevice(hDeviceHandle))) print_error_msg(ret, "关闭设备失败"); sleep(2); return 0; }
int main(int argc, char **argv) { if(argc <= 3 || atoi(argv[1]) <= 0 || atoi(argv[1]) >= 3) { printf("输入参数不合法./a.out <算法标识1 or 2 or 3> <需要产生密钥长度>"); return INPUT_SRGV_ILLEGAL; } int temp; printf("%d %d \n", atoi(argv[1]), atoi(argv[2])); unsigned int alg_id; //指定算法标识 switch(atoi(argv[1])) { case 1: alg_id = SGD_SM2_1; break; case 2: alg_id = SGD_SM2_2; break; case 3: alg_id = SGD_SM2_3; break; } unsigned int key_bits; //指定密钥长度 key_bits = atoi(argv[2]); SGD_HANDLE hDeviceHandle; // 设备句柄 int ret; if(SDR_OK != (ret = SDF_OpenDevice(&hDeviceHandle))) { print_error_msg(ret, "Open Device Error"); return 0; } SGD_HANDLE hSessionHandle; if(SDR_OK != (ret = SDF_OpenSession(hDeviceHandle, &hSessionHandle))) { print_error_msg(ret, "Open Session Error"); return 0; } ECCrefPublicKey public_key; // ECC 公钥结构 ECCrefPrivateKey private_key; // ECC 私钥结构 if(SDR_OK != (ret = SDF_GenerateKeyPair_ECC(hSessionHandle, alg_id, key_bits, &public_key, &private_key))) { print_error_msg(ret, "Generate Key Pair ECC Error"); return 0; } PrintData("public_key", &public_key, sizeof(ECCrefPublicKey), 32); PrintData("private_key", &private_key, sizeof(ECCrefPrivateKey), 32); Bin2BcdAndSave(&public_key, sizeof(ECCrefPublicKey), "PublicKey.txt"); Bin2BcdAndSave(&private_key, sizeof(ECCrefPrivateKey), "PrivateKey.txt"); int index; char *filename_public = "public.key"; char *filename_private = "private.key"; save_key_pair_ecc(filename_public, filename_private, &public_key, &private_key); ECCrefPublicKey public_key_r; // ECC 公钥结构 ECCrefPrivateKey private_key_r; // ECC 私钥结构 int puk_len, prk_len; // puk_len = FileRead(filename_public, "rb", (unsigned char *)&public_key_r, sizeof(public_key_r)); // if(puk_len < sizeof(ECCrefPublicKey)) // { // printf("读取公钥失败,按任意键退出................\n"); // return 0; // } read_bcd_key_to_bin("PublicKey.txt", (unsigned char*)&public_key_r, sizeof(ECCrefPublicKey)); read_bcd_key_to_bin("PrivateKey.txt", (unsigned char*)&private_key_r, sizeof(ECCrefPrivateKey)); //read_bcd_key_to_bin(&public_key_r, sizeof(ECCrefPublicKey), filename_public); // prk_len = FileRead(filename_private, "rb", (unsigned char *)&private_key_r, sizeof(private_key_r)); // if(prk_len < sizeof(ECCrefPrivateKey)) // { // printf("读取私钥失败,按任意键退出................\n"); // return 0; // } // unsigned char *tmp = (unsigned char *)&private_key; // for (index = 0; index < sizeof(private_key); index++) // { // printf("%02x", *(tmp + index)); // } // printf("========================================\n"); // // unsigned char key_read_buf[1024]; // unsigned char key_bin_buf[1024]; // memset(key_read_buf, 0, sizeof(key_read_buf)); // memset(key_bin_buf, 0, sizeof(key_bin_buf)); // // FILE *fp_read = NULL; // if((fp_read = fopen("PrivateKey.txt", "rb")) == NULL) // { // printf("file open failed!"); // return 0; // } // // ret = fread(&key_read_buf, 1, 2 * sizeof(ECCrefPrivateKey), fp_read); // perror("222"); // // int n_len = strlen(key_read_buf) / 2; // // printf("%d \n", n_len); // if(n_len <= 0) // return -1; // Bcd2Bin(key_read_buf, n_len, key_bin_buf); // memcpy(&private_key_r, key_bin_buf, sizeof(ECCrefPrivateKey)); // // printf("---------------------------------------\n"); // // tmp = (unsigned char *)&private_key_r; // for (index = 0; index < sizeof(ECCrefPrivateKey); index++) // { // printf("%02x", *(tmp + index)); // } // printf("========================================\n"); char *data_buf = "qwertyuioplkjhgfdsazxcvbnm123423523656788?><:}{+_)(*&^%$#@!"; int data_len = strlen(data_buf); ECCCipher out_data; printf("encypt ---> %s \n", data_buf); if(SDR_OK != (ret = SDF_ExternalEncrypt_ECC(hSessionHandle, alg_id, &public_key_r, data_buf, data_len, &out_data))) { print_error_msg(ret, "Encrypt Error"); return ret; } char data_dec[1024] = {0}; int data_dec_len; if(SDR_OK != (ret = SDF_ExternalDecrypt_ECC(hSessionHandle, alg_id, &private_key_r, &out_data, &data_dec, &data_dec_len))) { print_error_msg(ret, "Decrypt Error"); return ret; } printf(" data ---> %s \n", data_dec); SDF_CloseSession(hSessionHandle); SDF_CloseDevice(hDeviceHandle); return 0; }