密钥管理系统 白盒密钥解密代码示例

这篇以 Golang、Python、C、Java 这四种语言为例,分别提供相应的示例代码。

相关产品
密钥管理系统 产品页,含版本规格、价格与购买入口。

一、基于 Golang 的解密代码示例

package mainimport ( "encoding/base64" "fmt" "unsafe")func main() { fmt.Println("——— test case for AES_256 ———") // 解密密钥文件所在的目录 decryptKeyFileDirectoryName := "./data" // 解密密钥文件名 decryptKeyFileName := "decrypt_key_aes256.bin" // 初始化向量,base64 编码 iv := "EUi3Vv7DiCf73D6XbVzMYg==" // 白盒密钥加密后的密文,并 base64 编码 cipherText := "HKyXV1Xoodi1P/sdf/cYLw==" // 创建白盒密钥时用的加密算法, 0: AES_256, 1: SM4 algoType := 0 fmt.Println(fmt.Sprintf("demo start for decryptKeyFileName=%s, iv=%s, cipherText=%s, algoType=%d", decryptKeyFileName, iv, cipherText, algoType)) whitebox_decrypt(decryptKeyFileDirectoryName, decryptKeyFileName, iv, cipherText, algoType) fmt.Println("——— test case for SM4 ———") // 解密密钥文件所在的目录 decryptKeyFileDirectoryName = "./data" // 解密密钥文件名 decryptKeyFileName = "decrypt_key_sm4.bin" // 初始化向量,base64 编码 iv = "9+COkyNOrT8mvWN6CgTjKw==" // 白盒密钥加密后的密文,并 base64 编码 cipherText = "83ji4vKFwtVSAN1LSh1aOQ==" // 创建白盒密钥时用的加密算法, 0: AES_256, 1: SM4 algoType = 1 fmt.Println(fmt.Sprintf("demo start for decryptKeyFileName=%s, iv=%s, cipherText=%s, algoType=%d", decryptKeyFileName, iv, cipherText, algoType)) whitebox_decrypt(decryptKeyFileDirectoryName, decryptKeyFileName, iv, cipherText, algoType) fmt.Println("——— test cases finished ———")}

二、基于 Python 的解密代码示例

#!/usr/bin/pythonimport osfrom ctypes import *import base64if __name__ == "__main__": print("——— test case for AES_256 ———") # 解密密钥文件所在的目录 decryptKeyFileDirectoryName = "../data"; # 解密密钥文件名 decryptKeyFileName = "decrypt_key_aes256.bin" # 初始化向量,base64 编码 iv = "EUi3Vv7DiCf73D6XbVzMYg==" # 白盒密钥加密后的密文,并 base64 编码 cipherText = "HKyXV1Xoodi1P/sdf/cYLw==" # 创建白盒密钥时用的加密算法, 0: AES_256, 1: SM4 algoType = 0 try: print("decryptKeyFileName=%s, iv=%s, cipherText=%s, algoType=%d" % (decryptKeyFileName, iv, cipherText, algoType)) plain = demo_clt_cbc_dec(decryptKeyFileDirectoryName, decryptKeyFileName, base64.b64decode(cipherText), base64.b64decode(iv), algoType) print ("decrypt success") print ("plain: %s" % plain) except YdwbCryptoException as e: print (e.msg) print("——— test case for SM4 ———") # 解密密钥文件所在的目录 decryptKeyFileDirectoryName = "../data"; # 解密密钥文件名 decryptKeyFileName = "decrypt_key_sm4.bin" # 初始化向量,base64 编码 iv = "9+COkyNOrT8mvWN6CgTjKw==" # 白盒密钥加密后的密文,并 base64 编码 cipherText = "83ji4vKFwtVSAN1LSh1aOQ==" # 创建白盒密钥时用的加密算法, 0: AES_256, 1: SM4 algoType = 1 try: print("decryptKeyFileName=%s, iv=%s, cipherText=%s, algoType=%d" % (decryptKeyFileName, iv, cipherText, algoType)) plain = demo_clt_cbc_dec(decryptKeyFileDirectoryName, decryptKeyFileName, base64.b64decode(cipherText), base64.b64decode(iv), algoType) print ("decrypt success") print ("plain: %s" % plain) except YdwbCryptoException as e: print (e.msg) print("——— test case finished ———") pass

三、基于 C 的解密代码示例

分为 Windows 和 Linux 两个平台代码:

·Windows 平台:打开 vs 文件夹下面的 demo.sln,demo 使用的是 vs2017,有静态链接和动态链接两个 Demo,直接编译和运行即可。因为路径问题,如果在命令行中使用 exe,需要将 /data 目录拷贝到上一层目录。

·Linux 平台:需要将 lib 加入环境变量:

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:../lib

示例代码如下:

#include "../include/wrp.h"#include "base64.h"#include <stdint.h>#include <stdio.h>#include <string.h>int demo_aes() { unsigned char cipher_base64[TEST_BUFF_SIZE] = "snPqPZaFN9CQc5WH/Tx5jA=="; //填入白盒密钥加密后的密文 unsigned char cipher[TEST_BUFF_SIZE] = {0}; int cipher_len = Base64decode(cipher, cipher_base64); if (cipher_len > TEST_BUFF_SIZE) { printf("base64 decode cipher text failed, memory is not enough.\\n"); return (-1); } unsigned char iv_base64[TEST_BUFF_SIZE] = "WBbaiNLcEYSbjKxoJt66UQ=="; //填入初始化向量 unsigned char iv_bin[TEST_BUFF_SIZE] = { 0 }; int iv_len = Base64decode(iv_bin, iv_base64); if (iv_len != 16) { printf("iv is not invalidate.\\n"); return (-1); } char * whitebox_decrypt_key = "decrypt_key_aes256.bin"; //填入解密密钥文件名 whitebox_decrypt(ALG_AES, whitebox_decrypt_key, cipher, cipher_len, iv_bin); return 0;}int demo_sm4() { unsigned char cipher_base64[TEST_BUFF_SIZE] = "IwNgzruYfHQ6oQz2PLdyRQ=="; //填入白盒密钥加密后的密文 unsigned char cipher[TEST_BUFF_SIZE] = {0}; int cipher_len = Base64decode(cipher, cipher_base64); if (cipher_len > TEST_BUFF_SIZE) { printf("base64 decode cipher text failed, memory is not enough.\\n"); return (-1); } unsigned char iv_base64[TEST_BUFF_SIZE] = "4qaj6cVd8msMVBqNTRG4Pg=="; //填入初始化向量 unsigned char iv_bin[TEST_BUFF_SIZE] = {0}; int iv_len = Base64decode(iv_bin, iv_base64); if (iv_len != 16) { printf("iv is not invalidate.\\n"); return (-1); } char * whitebox_decrypt_key = "decrypt_key_sm4.bin"; //填入解密密钥文件名 whitebox_decrypt(ALG_SM4, whitebox_decrypt_key, cipher, cipher_len, iv_bin); return 0;}int main(int argc, const char *argv[]) { demo_aes(); demo_sm4(); return 0;}

四、基于 Java 的解密代码示例

import com.tencent.yunding.lightjce.CipherWhiteBox;import com.tencent.yunding.lightjce.params.*;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.util.Base64;public class Main { public static void main(String[] args) throws Exception { System.out.println("——— test case for AES_256 ———"); // 解密密钥文件所在的目录 String decryptKeyFileDirectoryName = "../data"; // 解密密钥文件名 String decryptKeyFileName = "decrypt_key_aes256.bin"; // 初始化向量,base64 编码 String iv = "EUi3Vv7DiCf73D6XbVzMYg=="; // 白盒密钥加密后的密文,并 base64 编码 String cipherText = "HKyXV1Xoodi1P/sdf/cYLw=="; // 创建白盒密钥时用的加密算法, 0: AES_256, 1: SM4 int algoType = 0; System.out.printf("demo start for decryptKeyFileName=%s, iv=%s, cipherText=%s, algoType=%d \\n", decryptKeyFileName, iv, cipherText, algoType); whitebox_decrypt(decryptKeyFileDirectoryName, decryptKeyFileName, iv, cipherText, algoType); System.out.println("——— test case for SM4 ———"); // 解密密钥文件所在的目录 decryptKeyFileDirectoryName = "../data"; // 解密密钥文件名 decryptKeyFileName = "decrypt_key_sm4.bin"; // 初始化向量,base64 编码 iv = "9+COkyNOrT8mvWN6CgTjKw=="; // 白盒密钥加密后的密文,并 base64 编码 cipherText = "83ji4vKFwtVSAN1LSh1aOQ=="; // 创建白盒密钥时用的加密算法, 0: AES_256, 1: SM4 algoType = 1; System.out.printf("demo start for decryptKeyFileName=%s, iv=%s, cipherText=%s, algoType=%d \\n", decryptKeyFileName, iv, cipherText, algoType); whitebox_decrypt(decryptKeyFileDirectoryName, decryptKeyFileName, iv, cipherText, algoType); System.out.println("——— test case finished ———"); } public static void whitebox_decrypt(String decrypt_key_bin_dir, String fileName, String Iv, String CipherText, int algorithmType) throws Exception { byte[] cipher = Base64.getDecoder().decode(CipherText); byte[] iv = Base64.getDecoder().decode(Iv); String decryptKeyFilePath = decrypt_key_bin_dir + "/" + fileName; if (algorithmType == 0) { byte[] result1 = decAESData(decryptKeyFilePath, cipher, iv); System.out.println("AES decrypted text length: " + result1.length); System.out.println("AES decrypted text : " + new String(result1)); } else if (algorithmType == 1) { byte[] result2 = decSM4Data(decryptKeyFilePath, cipher, iv); System.out.println("SM4 decrypted text length: " + result2.length); System.out.println("SM4 decrypted text : " + new String(result2)); } } public static byte[] decAESData(String keyFilePath, byte[] data, byte[] iv) throws Exception { CipherWhiteBox instance = CipherWhiteBox.getInstance(SymAlgType.AES, BlockMode.cbc_mode, PaddingMode.p5padding); File file = new File(keyFilePath); instance.init(file, iv, CryptMode.decrypt_mode); instance.update(data); return instance.doFinal(); } public static byte[] decSM4Data(String keyFilePath, byte[] data, byte[] iv) throws Exception { CipherWhiteBox instance = CipherWhiteBox.getInstance(SymAlgType.SM4, BlockMode.cbc_mode, PaddingMode.p5padding); File file = new File(keyFilePath); instance.init(file, iv, CryptMode.decrypt_mode); instance.update(data); return instance.doFinal(); }}

赞 (0)
上一篇 5天前
下一篇 5天前