Invalid Private Key, Not a valid string or uint8Array

报这种错误:一般在生成private key前面添加"0x"即可解决。我就是在私钥前面添加了"0x"解决了。

在学习web3时,使用助词生成的私钥,然后由私钥导出keystore就报错:

ERROR
Invalid Private Key, Not a valid string or uint8Array
InvalidPrivateKeyError: Invalid Private Key, Not a valid string or uint8Array
    at parseAndValidatePrivateKey (webpack-internal:///./node_modules/web3-eth-accounts/lib/esm/account.js:132:11)
    at eval (webpack-internal:///./node_modules/web3-eth-accounts/lib/esm/account.js:523:32)
    at Generator.next (<anonymous>)
    at eval (webpack-internal:///./node_modules/web3-eth-accounts/lib/esm/account.js:66:67)
    at new Promise (<anonymous>)
    at __awaiter (webpack-internal:///./node_modules/web3-eth-accounts/lib/esm/account.js:48:10)
    at Object.encrypt (webpack-internal:///./node_modules/web3-eth-accounts/lib/esm/account.js:521:52)
    at genMnemonic (webpack-internal:///./node_modules/unplugin/dist/webpack/loaders/transform.js?unpluginName=unplugin-vue-components!./node_modules/babel-loader/lib/index.js??clonedRuleSet-40.use[0]!./node_modules/vue-loader/dist/index.js??ruleSet[0].use[0]!./src/components/AccountSys.vue?vue&type=script&setup=true&lang=js:46:42)

开始报错以为是自己没开梯子,但我用之前写的demo是能查询到eth账户余额,于是根据报错信息怀疑是生成的privatekey不对,我照着学习的视频检查了代码跟它一样没问题呀,然后我在生成的私钥前面添加了"0x"就可以了。

//获取钱包私钥
    const privateKey=wallet.getPrivateKey().toString("hex");    
const web3 = new Web3(
        Web3.givenProvider || 'wss://sepolia.infura.io/ws/v3/018c25a8b5ce4f23810c49f60478471f');
    const keystore = web3.eth.accounts.encrypt('0x'+privateKey,'111111');

搞定。

/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ /** * @defgroup crypt * @brief crypto module */ /** * @defgroup crypt_algid * @ingroup crypt * @brief id of algorithms */ #ifndef CRYPT_ALGID_H #define CRYPT_ALGID_H #include "bsl_obj.h" #ifdef __cplusplus extern "C" { #endif // __cplusplus /** * @ingroup crypt_algid * * RAND algorithm ID */ typedef enum { CRYPT_RAND_SHA1 = BSL_CID_RAND_SHA1, CRYPT_RAND_SHA224 = BSL_CID_RAND_SHA224, CRYPT_RAND_SHA256 = BSL_CID_RAND_SHA256, CRYPT_RAND_SHA384 = BSL_CID_RAND_SHA384, CRYPT_RAND_SHA512 = BSL_CID_RAND_SHA512, CRYPT_RAND_HMAC_SHA1 = BSL_CID_RAND_HMAC_SHA1, CRYPT_RAND_HMAC_SHA224 = BSL_CID_RAND_HMAC_SHA224, CRYPT_RAND_HMAC_SHA256 = BSL_CID_RAND_HMAC_SHA256, CRYPT_RAND_HMAC_SHA384 = BSL_CID_RAND_HMAC_SHA384, CRYPT_RAND_HMAC_SHA512 = BSL_CID_RAND_HMAC_SHA512, CRYPT_RAND_AES128_CTR = BSL_CID_RAND_AES128_CTR, CRYPT_RAND_AES192_CTR = BSL_CID_RAND_AES192_CTR, CRYPT_RAND_AES256_CTR = BSL_CID_RAND_AES256_CTR, CRYPT_RAND_AES128_CTR_DF = BSL_CID_RAND_AES128_CTR_DF, CRYPT_RAND_AES192_CTR_DF = BSL_CID_RAND_AES192_CTR_DF, CRYPT_RAND_AES256_CTR_DF = BSL_CID_RAND_AES256_CTR_DF, CRYPT_RAND_SM3 = BSL_CID_RAND_SM3, CRYPT_RAND_SM4_CTR_DF = BSL_CID_RAND_SM4_CTR_DF, CRYPT_RAND_ALGID_MAX = BSL_CID_UNKNOWN } CRYPT_RAND_AlgId; /** * @ingroup crypt_algid * * Hash algorithm ID */ typedef enum { CRYPT_MD_MD5 = BSL_CID_MD5, CRYPT_MD_SHA1 = BSL_CID_SHA1, CRYPT_MD_SHA224 = BSL_CID_SHA224, CRYPT_MD_SHA256 = BSL_CID_SHA256, CRYPT_MD_SHA384 = BSL_CID_SHA384, CRYPT_MD_SHA512 = BSL_CID_SHA512, CRYPT_MD_SHA3_224 = BSL_CID_SHA3_224, CRYPT_MD_SHA3_256 = BSL_CID_SHA3_256, CRYPT_MD_SHA3_384 = BSL_CID_SHA3_384, CRYPT_MD_SHA3_512 = BSL_CID_SHA3_512, CRYPT_MD_SHAKE128 = BSL_CID_SHAKE128, CRYPT_MD_SHAKE256 = BSL_CID_SHAKE256, CRYPT_MD_SM3 = BSL_CID_SM3, CRYPT_MD_MAX = BSL_CID_UNKNOWN } CRYPT_MD_AlgId; /** * @ingroup crypt_algid * * MAC algorithm ID */ typedef enum { CRYPT_MAC_HMAC_MD5 = BSL_CID_HMAC_MD5, CRYPT_MAC_HMAC_SHA1 = BSL_CID_HMAC_SHA1, CRYPT_MAC_HMAC_SHA224 = BSL_CID_HMAC_SHA224, CRYPT_MAC_HMAC_SHA256 = BSL_CID_HMAC_SHA256, CRYPT_MAC_HMAC_SHA384 = BSL_CID_HMAC_SHA384, CRYPT_MAC_HMAC_SHA512 = BSL_CID_HMAC_SHA512, CRYPT_MAC_HMAC_SHA3_224 = BSL_CID_HMAC_SHA3_224, CRYPT_MAC_HMAC_SHA3_256 = BSL_CID_HMAC_SHA3_256, CRYPT_MAC_HMAC_SHA3_384 = BSL_CID_HMAC_SHA3_384, CRYPT_MAC_HMAC_SHA3_512 = BSL_CID_HMAC_SHA3_512, CRYPT_MAC_HMAC_SM3 = BSL_CID_HMAC_SM3, CRYPT_MAC_CMAC_AES128 = BSL_CID_CMAC_AES128, CRYPT_MAC_CMAC_AES192 = BSL_CID_CMAC_AES192, CRYPT_MAC_CMAC_AES256 = BSL_CID_CMAC_AES256, CRYPT_MAC_CMAC_SM4 = BSL_CID_CMAC_SM4, CRYPT_MAC_CBC_MAC_SM4 = BSL_CID_CBC_MAC_SM4, CRYPT_MAC_GMAC_AES128 = BSL_CID_GMAC_AES128, CRYPT_MAC_GMAC_AES192 = BSL_CID_GMAC_AES192, CRYPT_MAC_GMAC_AES256 = BSL_CID_GMAC_AES256, CRYPT_MAC_SIPHASH64 = BSL_CID_SIPHASH64, CRYPT_MAC_SIPHASH128 = BSL_CID_SIPHASH128, CRYPT_MAC_MAX = BSL_CID_UNKNOWN } CRYPT_MAC_AlgId; /** * @ingroup crypt_algid * * Asymmetric algorithm ID */ typedef enum { CRYPT_PKEY_DSA = BSL_CID_DSA, CRYPT_PKEY_ED25519 = BSL_CID_ED25519, CRYPT_PKEY_X25519 = BSL_CID_X25519, CRYPT_PKEY_RSA = BSL_CID_RSA, CRYPT_PKEY_DH = BSL_CID_DH, CRYPT_PKEY_ECDSA = BSL_CID_ECDSA, CRYPT_PKEY_ECDH = BSL_CID_ECDH, CRYPT_PKEY_SM2 = BSL_CID_SM2DSA, CRYPT_PKEY_PAILLIER = BSL_CID_PAILLIER, CRYPT_PKEY_ELGAMAL = BSL_CID_ELGAMAL, CRYPT_PKEY_SLH_DSA = BSL_CID_SLH_DSA, CRYPT_PKEY_ML_KEM = BSL_CID_ML_KEM, CRYPT_PKEY_ML_DSA = BSL_CID_ML_DSA, CRYPT_PKEY_HYBRID_KEM = BSL_CID_HYBRID_KEM, CRYPT_PKEY_XMSS = BSL_CID_XMSS, CRYPT_PKEY_MAX = BSL_CID_UNKNOWN } CRYPT_PKEY_AlgId; /** * @ingroup cipher_algid * @brief Symmetric algorithm mode ID * * There is a mapping relationship with the g_ealCipherMethod list. Attention any modification must be synchronized. */ typedef enum { CRYPT_CIPHER_AES128_CBC = BSL_CID_AES128_CBC, CRYPT_CIPHER_AES192_CBC = BSL_CID_AES192_CBC, CRYPT_CIPHER_AES256_CBC = BSL_CID_AES256_CBC, CRYPT_CIPHER_AES128_CTR = BSL_CID_AES128_CTR, CRYPT_CIPHER_AES192_CTR = BSL_CID_AES192_CTR, CRYPT_CIPHER_AES256_CTR = BSL_CID_AES256_CTR, CRYPT_CIPHER_AES128_ECB = BSL_CID_AES128_ECB, CRYPT_CIPHER_AES192_ECB = BSL_CID_AES192_ECB, CRYPT_CIPHER_AES256_ECB = BSL_CID_AES256_ECB, CRYPT_CIPHER_AES128_XTS = BSL_CID_AES128_XTS, CRYPT_CIPHER_AES256_XTS = BSL_CID_AES256_XTS, CRYPT_CIPHER_AES128_CCM = BSL_CID_AES128_CCM, CRYPT_CIPHER_AES192_CCM = BSL_CID_AES192_CCM, CRYPT_CIPHER_AES256_CCM = BSL_CID_AES256_CCM, CRYPT_CIPHER_AES128_GCM = BSL_CID_AES128_GCM, CRYPT_CIPHER_AES192_GCM = BSL_CID_AES192_GCM, CRYPT_CIPHER_AES256_GCM = BSL_CID_AES256_GCM, CRYPT_CIPHER_CHACHA20_POLY1305 = BSL_CID_CHACHA20_POLY1305, CRYPT_CIPHER_SM4_XTS = BSL_CID_SM4_XTS, CRYPT_CIPHER_SM4_CBC = BSL_CID_SM4_CBC, CRYPT_CIPHER_SM4_ECB = BSL_CID_SM4_ECB, CRYPT_CIPHER_SM4_CTR = BSL_CID_SM4_CTR, CRYPT_CIPHER_SM4_GCM = BSL_CID_SM4_GCM, CRYPT_CIPHER_SM4_CFB = BSL_CID_SM4_CFB, CRYPT_CIPHER_SM4_OFB = BSL_CID_SM4_OFB, CRYPT_CIPHER_AES128_CFB = BSL_CID_AES128_CFB, CRYPT_CIPHER_AES192_CFB = BSL_CID_AES192_CFB, CRYPT_CIPHER_AES256_CFB = BSL_CID_AES256_CFB, CRYPT_CIPHER_AES128_OFB = BSL_CID_AES128_OFB, CRYPT_CIPHER_AES192_OFB = BSL_CID_AES192_OFB, CRYPT_CIPHER_AES256_OFB = BSL_CID_AES256_OFB, CRYPT_CIPHER_MAX = BSL_CID_UNKNOWN, } CRYPT_CIPHER_AlgId; /** * @ingroup crypt_algid * * Parameter ID of an asymmetric algorithm. The most significant 16 bits indicate the algorithm ID, * and the least significant 16 bits map the ID definition of the algorithm LowLevel. */ typedef enum { CRYPT_DH_RFC2409_768 = BSL_CID_DH_RFC2409_768, CRYPT_DH_RFC2409_1024 = BSL_CID_DH_RFC2409_1024, CRYPT_DH_RFC3526_1536 = BSL_CID_DH_RFC3526_1536, CRYPT_DH_RFC3526_2048 = BSL_CID_DH_RFC3526_2048, CRYPT_DH_RFC3526_3072 = BSL_CID_DH_RFC3526_3072, CRYPT_DH_RFC3526_4096 = BSL_CID_DH_RFC3526_4096, CRYPT_DH_RFC3526_6144 = BSL_CID_DH_RFC3526_6144, CRYPT_DH_RFC3526_8192 = BSL_CID_DH_RFC3526_8192, CRYPT_DH_RFC7919_2048 = BSL_CID_DH_RFC7919_2048, CRYPT_DH_RFC7919_3072 = BSL_CID_DH_RFC7919_3072, CRYPT_DH_RFC7919_4096 = BSL_CID_DH_RFC7919_4096, CRYPT_DH_RFC7919_6144 = BSL_CID_DH_RFC7919_6144, CRYPT_DH_RFC7919_8192 = BSL_CID_DH_RFC7919_8192, CRYPT_ECC_NISTP224 = BSL_CID_NIST_PRIME224, CRYPT_ECC_NISTP256 = BSL_CID_PRIME256V1, CRYPT_ECC_NISTP384 = BSL_CID_SECP384R1, CRYPT_ECC_NISTP521 = BSL_CID_SECP521R1, CRYPT_ECC_BRAINPOOLP256R1 = BSL_CID_ECC_BRAINPOOLP256R1, CRYPT_ECC_BRAINPOOLP384R1 = BSL_CID_ECC_BRAINPOOLP384R1, CRYPT_ECC_BRAINPOOLP512R1 = BSL_CID_ECC_BRAINPOOLP512R1, CRYPT_ECC_SM2 = BSL_CID_SM2PRIME256, CRYPT_HYBRID_X25519_MLKEM512 = BSL_CID_X25519_MLKEM512, CRYPT_HYBRID_X25519_MLKEM768 = BSL_CID_X25519_MLKEM768, CRYPT_HYBRID_X25519_MLKEM1024 = BSL_CID_X25519_MLKEM1024, CRYPT_HYBRID_ECDH_NISTP256_MLKEM512 = BSL_CID_ECDH_NISTP256_MLKEM512, CRYPT_HYBRID_ECDH_NISTP256_MLKEM768 = BSL_CID_ECDH_NISTP256_MLKEM768, CRYPT_HYBRID_ECDH_NISTP256_MLKEM1024 = BSL_CID_ECDH_NISTP256_MLKEM1024, CRYPT_HYBRID_ECDH_NISTP384_MLKEM512 = BSL_CID_ECDH_NISTP384_MLKEM512, CRYPT_HYBRID_ECDH_NISTP384_MLKEM768 = BSL_CID_ECDH_NISTP384_MLKEM768, CRYPT_HYBRID_ECDH_NISTP384_MLKEM1024 = BSL_CID_ECDH_NISTP384_MLKEM1024, CRYPT_HYBRID_ECDH_NISTP521_MLKEM512 = BSL_CID_ECDH_NISTP521_MLKEM512, CRYPT_HYBRID_ECDH_NISTP521_MLKEM768 = BSL_CID_ECDH_NISTP521_MLKEM768, CRYPT_HYBRID_ECDH_NISTP521_MLKEM1024 = BSL_CID_ECDH_NISTP521_MLKEM1024, CRYPT_MLDSA_TYPE_MLDSA_44 = BSL_CID_ML_DSA_44, CRYPT_MLDSA_TYPE_MLDSA_65 = BSL_CID_ML_DSA_65, CRYPT_MLDSA_TYPE_MLDSA_87 = BSL_CID_ML_DSA_87, CRYPT_KEM_TYPE_MLKEM_512 = BSL_CID_ML_KEM_512, CRYPT_KEM_TYPE_MLKEM_768 = BSL_CID_ML_KEM_768, CRYPT_KEM_TYPE_MLKEM_1024 = BSL_CID_ML_KEM_1024, CRYPT_SLH_DSA_SHA2_128S = BSL_CID_SLH_DSA_SHA2_128S, CRYPT_SLH_DSA_SHAKE_128S = BSL_CID_SLH_DSA_SHAKE_128S, CRYPT_SLH_DSA_SHA2_128F = BSL_CID_SLH_DSA_SHA2_128F, CRYPT_SLH_DSA_SHAKE_128F = BSL_CID_SLH_DSA_SHAKE_128F, CRYPT_SLH_DSA_SHA2_192S = BSL_CID_SLH_DSA_SHA2_192S, CRYPT_SLH_DSA_SHAKE_192S = BSL_CID_SLH_DSA_SHAKE_192S, CRYPT_SLH_DSA_SHA2_192F = BSL_CID_SLH_DSA_SHA2_192F, CRYPT_SLH_DSA_SHAKE_192F = BSL_CID_SLH_DSA_SHAKE_192F, CRYPT_SLH_DSA_SHA2_256S = BSL_CID_SLH_DSA_SHA2_256S, CRYPT_SLH_DSA_SHAKE_256S = BSL_CID_SLH_DSA_SHAKE_256S, CRYPT_SLH_DSA_SHA2_256F = BSL_CID_SLH_DSA_SHA2_256F, CRYPT_SLH_DSA_SHAKE_256F = BSL_CID_SLH_DSA_SHAKE_256F, CRYPT_XMSS_SHA2_10_256 = BSL_CID_XMSS_SHA2_10_256, CRYPT_XMSS_SHA2_16_256 = BSL_CID_XMSS_SHA2_16_256, CRYPT_XMSS_SHA2_20_256 = BSL_CID_XMSS_SHA2_20_256, CRYPT_XMSS_SHA2_10_512 = BSL_CID_XMSS_SHA2_10_512, CRYPT_XMSS_SHA2_16_512 = BSL_CID_XMSS_SHA2_16_512, CRYPT_XMSS_SHA2_20_512 = BSL_CID_XMSS_SHA2_20_512, CRYPT_XMSS_SHAKE_10_256 = BSL_CID_XMSS_SHAKE_10_256, CRYPT_XMSS_SHAKE_16_256 = BSL_CID_XMSS_SHAKE_16_256, CRYPT_XMSS_SHAKE_20_256 = BSL_CID_XMSS_SHAKE_20_256, CRYPT_XMSS_SHAKE_10_512 = BSL_CID_XMSS_SHAKE_10_512, CRYPT_XMSS_SHAKE_16_512 = BSL_CID_XMSS_SHAKE_16_512, CRYPT_XMSS_SHAKE_20_512 = BSL_CID_XMSS_SHAKE_20_512, CRYPT_XMSS_SHA2_10_192 = BSL_CID_XMSS_SHA2_10_192, CRYPT_XMSS_SHA2_16_192 = BSL_CID_XMSS_SHA2_16_192, CRYPT_XMSS_SHA2_20_192 = BSL_CID_XMSS_SHA2_20_192, CRYPT_XMSS_SHAKE256_10_256 = BSL_CID_XMSS_SHAKE256_10_256, CRYPT_XMSS_SHAKE256_16_256 = BSL_CID_XMSS_SHAKE256_16_256, CRYPT_XMSS_SHAKE256_20_256 = BSL_CID_XMSS_SHAKE256_20_256, CRYPT_XMSS_SHAKE256_10_192 = BSL_CID_XMSS_SHAKE256_10_192, CRYPT_XMSS_SHAKE256_16_192 = BSL_CID_XMSS_SHAKE256_16_192, CRYPT_XMSS_SHAKE256_20_192 = BSL_CID_XMSS_SHAKE256_20_192, CRYPT_XMSSMT_SHA2_20_2_256 = BSL_CID_XMSSMT_SHA2_20_2_256, CRYPT_XMSSMT_SHA2_20_4_256 = BSL_CID_XMSSMT_SHA2_20_4_256, CRYPT_XMSSMT_SHA2_40_2_256 = BSL_CID_XMSSMT_SHA2_40_2_256, CRYPT_XMSSMT_SHA2_40_4_256 = BSL_CID_XMSSMT_SHA2_40_4_256, CRYPT_XMSSMT_SHA2_40_8_256 = BSL_CID_XMSSMT_SHA2_40_8_256, CRYPT_XMSSMT_SHA2_60_3_256 = BSL_CID_XMSSMT_SHA2_60_3_256, CRYPT_XMSSMT_SHA2_60_6_256 = BSL_CID_XMSSMT_SHA2_60_6_256, CRYPT_XMSSMT_SHA2_60_12_256 = BSL_CID_XMSSMT_SHA2_60_12_256, CRYPT_XMSSMT_SHA2_20_2_512 = BSL_CID_XMSSMT_SHA2_20_2_512, CRYPT_XMSSMT_SHA2_20_4_512 = BSL_CID_XMSSMT_SHA2_20_4_512, CRYPT_XMSSMT_SHA2_40_2_512 = BSL_CID_XMSSMT_SHA2_40_2_512, CRYPT_XMSSMT_SHA2_40_4_512 = BSL_CID_XMSSMT_SHA2_40_4_512, CRYPT_XMSSMT_SHA2_40_8_512 = BSL_CID_XMSSMT_SHA2_40_8_512, CRYPT_XMSSMT_SHA2_60_3_512 = BSL_CID_XMSSMT_SHA2_60_3_512, CRYPT_XMSSMT_SHA2_60_6_512 = BSL_CID_XMSSMT_SHA2_60_6_512, CRYPT_XMSSMT_SHA2_60_12_512 = BSL_CID_XMSSMT_SHA2_60_12_512, CRYPT_XMSSMT_SHAKE_20_2_256 = BSL_CID_XMSSMT_SHAKE_20_2_256, CRYPT_XMSSMT_SHAKE_20_4_256 = BSL_CID_XMSSMT_SHAKE_20_4_256, CRYPT_XMSSMT_SHAKE_40_2_256 = BSL_CID_XMSSMT_SHAKE_40_2_256, CRYPT_XMSSMT_SHAKE_40_4_256 = BSL_CID_XMSSMT_SHAKE_40_4_256, CRYPT_XMSSMT_SHAKE_40_8_256 = BSL_CID_XMSSMT_SHAKE_40_8_256, CRYPT_XMSSMT_SHAKE_60_3_256 = BSL_CID_XMSSMT_SHAKE_60_3_256, CRYPT_XMSSMT_SHAKE_60_6_256 = BSL_CID_XMSSMT_SHAKE_60_6_256, CRYPT_XMSSMT_SHAKE_60_12_256 = BSL_CID_XMSSMT_SHAKE_60_12_256, CRYPT_XMSSMT_SHAKE_20_2_512 = BSL_CID_XMSSMT_SHAKE_20_2_512, CRYPT_XMSSMT_SHAKE_20_4_512 = BSL_CID_XMSSMT_SHAKE_20_4_512, CRYPT_XMSSMT_SHAKE_40_2_512 = BSL_CID_XMSSMT_SHAKE_40_2_512, CRYPT_XMSSMT_SHAKE_40_4_512 = BSL_CID_XMSSMT_SHAKE_40_4_512, CRYPT_XMSSMT_SHAKE_40_8_512 = BSL_CID_XMSSMT_SHAKE_40_8_512, CRYPT_XMSSMT_SHAKE_60_3_512 = BSL_CID_XMSSMT_SHAKE_60_3_512, CRYPT_XMSSMT_SHAKE_60_6_512 = BSL_CID_XMSSMT_SHAKE_60_6_512, CRYPT_XMSSMT_SHAKE_60_12_512 = BSL_CID_XMSSMT_SHAKE_60_12_512, CRYPT_XMSSMT_SHA2_20_2_192 = BSL_CID_XMSSMT_SHA2_20_2_192, CRYPT_XMSSMT_SHA2_20_4_192 = BSL_CID_XMSSMT_SHA2_20_4_192, CRYPT_XMSSMT_SHA2_40_2_192 = BSL_CID_XMSSMT_SHA2_40_2_192, CRYPT_XMSSMT_SHA2_40_4_192 = BSL_CID_XMSSMT_SHA2_40_4_192, CRYPT_XMSSMT_SHA2_40_8_192 = BSL_CID_XMSSMT_SHA2_40_8_192, CRYPT_XMSSMT_SHA2_60_3_192 = BSL_CID_XMSSMT_SHA2_60_3_192, CRYPT_XMSSMT_SHA2_60_6_192 = BSL_CID_XMSSMT_SHA2_60_6_192, CRYPT_XMSSMT_SHA2_60_12_192 = BSL_CID_XMSSMT_SHA2_60_12_192, CRYPT_XMSSMT_SHAKE256_20_2_256 = BSL_CID_XMSSMT_SHAKE256_20_2_256, CRYPT_XMSSMT_SHAKE256_20_4_256 = BSL_CID_XMSSMT_SHAKE256_20_4_256, CRYPT_XMSSMT_SHAKE256_40_2_256 = BSL_CID_XMSSMT_SHAKE256_40_2_256, CRYPT_XMSSMT_SHAKE256_40_4_256 = BSL_CID_XMSSMT_SHAKE256_40_4_256, CRYPT_XMSSMT_SHAKE256_40_8_256 = BSL_CID_XMSSMT_SHAKE256_40_8_256, CRYPT_XMSSMT_SHAKE256_60_3_256 = BSL_CID_XMSSMT_SHAKE256_60_3_256, CRYPT_XMSSMT_SHAKE256_60_6_256 = BSL_CID_XMSSMT_SHAKE256_60_6_256, CRYPT_XMSSMT_SHAKE256_60_12_256 = BSL_CID_XMSSMT_SHAKE256_60_12_256, CRYPT_XMSSMT_SHAKE256_20_2_192 = BSL_CID_XMSSMT_SHAKE256_20_2_192, CRYPT_XMSSMT_SHAKE256_20_4_192 = BSL_CID_XMSSMT_SHAKE256_20_4_192, CRYPT_XMSSMT_SHAKE256_40_2_192 = BSL_CID_XMSSMT_SHAKE256_40_2_192, CRYPT_XMSSMT_SHAKE256_40_4_192 = BSL_CID_XMSSMT_SHAKE256_40_4_192, CRYPT_XMSSMT_SHAKE256_40_8_192 = BSL_CID_XMSSMT_SHAKE256_40_8_192, CRYPT_XMSSMT_SHAKE256_60_3_192 = BSL_CID_XMSSMT_SHAKE256_60_3_192, CRYPT_XMSSMT_SHAKE256_60_6_192 = BSL_CID_XMSSMT_SHAKE256_60_6_192, CRYPT_XMSSMT_SHAKE256_60_12_192 = BSL_CID_XMSSMT_SHAKE256_60_12_192, CRYPT_PKEY_PARAID_MAX = BSL_CID_UNKNOWN } CRYPT_PKEY_ParaId; /** * @ingroup crypt_algid * * Elliptic Curve Point Encoding Format */ typedef enum { CRYPT_POINT_COMPRESSED, CRYPT_POINT_UNCOMPRESSED, /**< default format. */ CRYPT_POINT_HYBRID, CRYPT_POINT_MAX } CRYPT_PKEY_PointFormat; /** * @ingroup crypt_algid * * KDF algorithm ID */ typedef enum { CRYPT_KDF_SCRYPT = BSL_CID_SCRYPT, CRYPT_KDF_PBKDF2 = BSL_CID_PBKDF2, CRYPT_KDF_KDFTLS12 = BSL_CID_KDFTLS12, CRYPT_KDF_HKDF = BSL_CID_HKDF, CRYPT_KDF_MAX = BSL_CID_UNKNOWN } CRYPT_KDF_AlgId; #ifdef __cplusplus } #endif // __cplusplus #endif // CRYPT_ALGID_H /* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #ifndef CRYPT_RSA_H #define CRYPT_RSA_H #include "hitls_build.h" #ifdef HITLS_CRYPTO_RSA #include <stdlib.h> #include <stdint.h> #include "crypt_local_types.h" #include "bsl_params.h" #ifdef __cplusplus extern "C" { #endif /* __cpluscplus */ #define RSA_MIN_MODULUS_BITS 1024 #define RSA_MAX_MODULUS_BITS 16384 #define RSA_SMALL_MODULUS_BYTES (3072 / 8) #define RSA_MAX_PUBEXP_BYTES (64 / 8) #define RSA_MIN_MODULUS_LEN (RSA_MIN_MODULUS_BITS / 8) #define RSA_MAX_MODULUS_LEN (RSA_MAX_MODULUS_BITS / 8) /* RSA */ typedef struct RSA_Ctx CRYPT_RSA_Ctx; typedef struct RSA_Para CRYPT_RSA_Para; /* RSA method */ /** * @ingroup rsa * @brief Allocate rsa context memory space. * * @retval (CRYPT_RSA_Ctx *) Pointer to the memory space of the allocated context * @retval NULL Invalid null pointer. */ CRYPT_RSA_Ctx *CRYPT_RSA_NewCtx(void); // create key structure /** * @ingroup rsa * @brief Allocate rsa context memory space. * * @param libCtx [IN] Library context * * @retval (CRYPT_RSA_Ctx *) Pointer to the memory space of the allocated context * @retval NULL Invalid null pointer. */ CRYPT_RSA_Ctx *CRYPT_RSA_NewCtxEx(void *libCtx); /** * @ingroup rsa * @brief Copy the RSA context. After the duplication is complete, call the CRYPT_RSA_FreeCtx to release the memory. * * @param ctx [IN] RSA context * * @return CRYPT_RSA_Ctx Rsa context pointer * If the operation fails, a null value is returned. */ CRYPT_RSA_Ctx *CRYPT_RSA_DupCtx(CRYPT_RSA_Ctx *keyCtx); /** * @ingroup rsa * @brief Create rsa key parameter structure * * @param para [IN] RSA External parameter * * @retval (CRYPT_RSA_Para *) Pointer to the allocated memory space of the structure * @retval NULL Invalid null pointer. */ CRYPT_RSA_Para *CRYPT_RSA_NewParaEx(const BSL_Param *para); /** * @ingroup rsa * @brief Release rsa key parameter structure * * @param para [IN] Storage pointer in the parameter structure to be released. The parameter is set NULL by the invoker. */ void CRYPT_RSA_FreePara(CRYPT_RSA_Para *para); /** * @ingroup rsa * @brief release rsa key context structure * * @param ctx [IN] Pointer to the context structure to be released. The ctx is set NULL by the invoker. */ void CRYPT_RSA_FreeCtx(CRYPT_RSA_Ctx *ctx); /** * @ingroup rsa * @brief Set the data of the key parameter structure to the key structure. * * @param ctx [OUT] Key structure for which related parameters need to be set * @param para [IN] Key parameter structure * * @retval CRYPT_NULL_INPUT Invalid null pointer input. * @retval CRYPT_RSA_ERR_KEY_BITS The expected key length does not meet the requirements. * @retval CRYPT_RSA_ERR_E_VALUE The expected value of e does not meet the requirements. * @retval CRYPT_MEM_ALLOC_FAIL internal memory allocation error * @retval CRYPT_SUCCESS set successfully. */ int32_t CRYPT_RSA_SetPara(CRYPT_RSA_Ctx *ctx, const CRYPT_RsaPara *para); /** * @ingroup rsa * @brief Obtain the valid length of the key. * * @param ctx [IN] Structure from which the key length is expected to be obtained * * @retval 0: The input is incorrect or the corresponding key structure does not have a valid key length. * @retval uint32_t: Valid key length */ uint32_t CRYPT_RSA_GetBits(const CRYPT_RSA_Ctx *ctx); #ifdef HITLS_CRYPTO_RSA_GEN /** * @ingroup rsa * @brief Generate the RSA key pair. * * @param ctx [IN/OUT] rsa context structure * * @retval CRYPT_NULL_INPUT Error null pointer input * @retval CRYPT_RSA_ERR_KEY_BITS The value of e in the context structure does not meet the requirements. * @retval CRYPT_MEM_ALLOC_FAIL Memory allocation failure * @retval BN error An error occurs in the internal BigNum operation. * @retval CRYPT_SUCCESS The key pair is successfully generated. */ int32_t CRYPT_RSA_Gen(CRYPT_RSA_Ctx *ctx); #endif #if defined(HITLS_CRYPTO_RSA_ENCRYPT) || defined(HITLS_CRYPTO_RSA_VERIFY) || defined(HITLS_CRYPTO_RSA_SIGN) /** * @ingroup rsa * @brief RSA public key encryption * * @param ctx [IN] RSA context structure * @param input [IN] Information to be encrypted * @param inputLen [IN] Length of the information to be encrypted * @param out [OUT] Pointer to the encrypted information output. * @param outLen [IN/OUT] Pointer to the length of the encrypted information. * Before being transferred, the value must be set to the maximum length of the array. * * @retval CRYPT_NULL_INPUT Invalid null pointer input * @retval CRYPT_RSA_NO_KEY_INFO does not contain the key information. * @retval CRYPT_RSA_ERR_INPUT_VALUE The entered value does not meet the calculation conditions. * @retval CRYPT_MEM_ALLOC_FAIL Memory allocation failure * @retval CRYPT_SECUREC_FAIL A security function error occurs. * @retval BN error An error occurs in the internal BigNum operation. * @retval CRYPT_SUCCESS encryption succeeded. */ int32_t CRYPT_RSA_PubEnc(const CRYPT_RSA_Ctx *ctx, const uint8_t *input, uint32_t inputLen, uint8_t *out, uint32_t *outLen); #endif /** * @ingroup rsa * @brief RSA private key decryption * * @param ctx [IN] RSA context structure * @param input [IN] Information to be decrypted * @param inputLen [IN] Length of the information to be decrypted * @param out [OUT] Pointer to the decrypted information output. * @param outLen [IN/OUT] Pointer to the length of the decrypted information. * Before being transferred, the value must be set to the maximum length of the array. * * @retval CRYPT_NULL_INPUT Invalid null pointer input * @retval CRYPT_RSA_ERR_DEC_BITS Incorrect length of the encrypted private key. * @retval CRYPT_RSA_NO_KEY_INFO does not contain the key information. * @retval CRYPT_RSA_ERR_INPUT_VALUE The entered value does not meet the calculation conditions. * @retval CRYPT_MEM_ALLOC_FAIL Memory allocation failure * @retval CRYPT_SECUREC_FAIL A security function error occurs. * @retval BN error. An error occurs in the internal BigNum operation. * @retval CRYPT_SUCCESS Decrypted Successfully */ int32_t CRYPT_RSA_PrvDec(const CRYPT_RSA_Ctx *ctx, const uint8_t *input, uint32_t inputLen, uint8_t *out, uint32_t *outLen); /** * @ingroup rsa * @brief RSA Set the private key information. * * @param ctx [OUT] rsa context structure * @param prv [IN] Private key data * * @retval CRYPT_NULL_INPUT Error null pointer input * @retval CRYPT_RSA_ERR_KEY_BITS The key length does not meet the requirements. * @retval CRYPT_RSA_NO_KEY_INFO does not contain the key information. * @retval CRYPT_RSA_ERR_INPUT_VALUE The entered value does not meet the calculation conditions. * @retval CRYPT_MEM_ALLOC_FAIL Memory allocation failure * @retval BN error. An error occurs in the internal BigNum operation. * @retval CRYPT_SUCCESS The private key is successfully set. */ int32_t CRYPT_RSA_SetPrvKey(CRYPT_RSA_Ctx *ctx, const CRYPT_RsaPrv *prv); /** * @ingroup rsa * @brief RSA Set the public key information. * * @param ctx [OUT] RSA context structure * @param pub [IN] Public key data * * @retval CRYPT_NULL_INPUT Error null pointer input * @retval CRYPT_RSA_ERR_KEY_BITS The key length does not meet the requirements. * @retval CRYPT_RSA_ERR_INPUT_VALUE The entered value does not meet the calculation conditions. * @retval CRYPT_MEM_ALLOC_FAIL Memory allocation failure * @retval BN error. An error occurs in the internal BigNum operation. * @retval CRYPT_SUCCESS The public key is successfully set. */ int32_t CRYPT_RSA_SetPubKey(CRYPT_RSA_Ctx *ctx, const CRYPT_RsaPub *pub); /** * @ingroup rsa * @brief RSA Obtain the private key information. * * @param ctx [IN] RSA context structure * @param prv [OUT] Private key data * * @retval CRYPT_NULL_INPUT Invalid null pointer input * @retval BN error. An error occurs in the internal BigNum operation. * @retval CRYPT_SUCCESS The private key is obtained successfully. */ int32_t CRYPT_RSA_GetPrvKey(const CRYPT_RSA_Ctx *ctx, CRYPT_RsaPrv *prv); /** * @ingroup rsa * @brief RSA Obtain the public key information. * * @param ctx [IN] RSA context structure * @param pub [OUT] Public key data * * @retval CRYPT_NULL_INPUT Invalid null pointer input * @retval BN error. An error occurs in the internal BigNum operation. * @retval CRYPT_SUCCESS The public key is obtained successfully. */ int32_t CRYPT_RSA_GetPubKey(const CRYPT_RSA_Ctx *ctx, CRYPT_RsaPub *pub); #ifdef HITLS_BSL_PARAMS /** * @ingroup rsa * @brief RSA Set the private key information. * * @param ctx [OUT] rsa context structure * @param para [IN] Private key data * * @retval CRYPT_NULL_INPUT Error null pointer input * @retval CRYPT_RSA_ERR_KEY_BITS The key length does not meet the requirements. * @retval CRYPT_RSA_NO_KEY_INFO does not contain the key information. * @retval CRYPT_RSA_ERR_INPUT_VALUE The entered value does not meet the calculation conditions. * @retval CRYPT_MEM_ALLOC_FAIL Memory allocation failure * @retval BN error. An error occurs in the internal BigNum operation. * @retval CRYPT_SUCCESS The private key is successfully set. */ int32_t CRYPT_RSA_SetPrvKeyEx(CRYPT_RSA_Ctx *ctx, const BSL_Param *para); /** * @ingroup rsa * @brief RSA Set the public key information. * * @param ctx [OUT] RSA context structure * @param para [IN] Public key data * * @retval CRYPT_NULL_INPUT Error null pointer input * @retval CRYPT_RSA_ERR_KEY_BITS The key length does not meet the requirements. * @retval CRYPT_RSA_ERR_INPUT_VALUE The entered value does not meet the calculation conditions. * @retval CRYPT_MEM_ALLOC_FAIL Memory allocation failure * @retval BN error. An error occurs in the internal BigNum operation. * @retval CRYPT_SUCCESS The public key is successfully set. */ int32_t CRYPT_RSA_SetPubKeyEx(CRYPT_RSA_Ctx *ctx, const BSL_Param *para); /** * @ingroup rsa * @brief RSA Obtain the private key information. * * @param ctx [IN] RSA context structure * @param para [OUT] Private key data * * @retval CRYPT_NULL_INPUT Invalid null pointer input * @retval BN error. An error occurs in the internal BigNum operation. * @retval CRYPT_SUCCESS The private key is obtained successfully. */ int32_t CRYPT_RSA_GetPrvKeyEx(const CRYPT_RSA_Ctx *ctx, BSL_Param *para); /** * @ingroup rsa * @brief RSA Obtain the public key information. * * @param ctx [IN] RSA context structure * @param para [OUT] Public key data * * @retval CRYPT_NULL_INPUT Invalid null pointer input * @retval BN error. An error occurs in the internal BigNum operation. * @retval CRYPT_SUCCESS The public key is obtained successfully. */ int32_t CRYPT_RSA_GetPubKeyEx(const CRYPT_RSA_Ctx *ctx, BSL_Param *para); /** * @ingroup rsa * @brief Set the data of the key parameter structure to the key structure. * * @param ctx [OUT] Key structure for which related parameters need to be set * @param para [IN] Key parameter structure * * @retval CRYPT_NULL_INPUT Invalid null pointer input. * @retval CRYPT_RSA_ERR_KEY_BITS The expected key length does not meet the requirements. * @retval CRYPT_RSA_ERR_E_VALUE The expected value of e does not meet the requirements. * @retval CRYPT_MEM_ALLOC_FAIL internal memory allocation error * @retval CRYPT_SUCCESS set successfully. */ int32_t CRYPT_RSA_SetParaEx(CRYPT_RSA_Ctx *ctx, const BSL_Param *para); #endif int32_t CRYPT_RSA_Ctrl(CRYPT_RSA_Ctx *ctx, int32_t opt, void *val, uint32_t len); #ifdef HITLS_CRYPTO_RSA_BSSA #ifdef HITLS_CRYPTO_RSA_SIGN /** * @ingroup RSA * @brief RSA blind operation for blind signature * * @param ctx [IN] RSA Context structure * @param algId [IN] hash Id for input * @param input [IN] Message to be blinded * @param inputLen [IN] Length of input message * @param out [OUT] Blinded message * @param outLen [OUT] Length of blinded message * * @retval CRYPT_SUCCESS on success * For other error codes, see crypt_errno.h. */ int32_t CRYPT_RSA_Blind(CRYPT_RSA_Ctx *ctx, int32_t algId, const uint8_t *input, uint32_t inputLen, uint8_t *out, uint32_t *outLen); #endif #ifdef HITLS_CRYPTO_RSA_VERIFY /** * @ingroup RSA * @brief RSA unblind operation for blind signature * * @param ctx [IN] RSA Context structure * @param input [IN] Blind signature to be unblinded * @param inputLen [IN] Length of blind signature * @param out [OUT] Final unblinded signature * @param outLen [OUT] Length of unblinded signature * * @retval CRYPT_SUCCESS on success * For other error codes, see crypt_errno.h. */ int32_t CRYPT_RSA_UnBlind(const CRYPT_RSA_Ctx *ctx, const uint8_t *input, uint32_t inputLen, uint8_t *out, uint32_t *outLen); #endif #endif #ifdef HITLS_CRYPTO_RSA_EMSA_PSS #if defined(HITLS_CRYPTO_RSA_SIGN) || defined(HITLS_CRYPTO_RSA_BSSA) /** * @ingroup rsa * @brief Set the PSS for the original data. * * @param ctx [IN] CRYPT_RSA_Ctx * @param hashMethod [IN] pss Required Hash Method * @param mgfMethod [IN] pss Internal hash method required by the mgf. * @param saltLen [IN] Length of the input salt. * @param data [IN] Original data * @param dataLen [IN] Length of the original data * @param pad [OUT] pss Output buffer * @param padLen [OUT] Maximum length of the array output by the PSS. * * @retval CRYPT_NULL_INPUT Error null pointer input * @retval CRYPT_RSA_ERR_PSS_SALT_DATA The salt value does not meet the requirements. * @retval CRYPT_RSA_ERR_KEY_BITS The key length does not meet the requirements. * @retval CRYPT_RSA_ERR_PSS_SALT_LEN The salt length does not meet the requirements. * @retval CRYPT_RSA_BUFF_LEN_NOT_ENOUGH The length of the reserved buffer is insufficient. * @retval CRYPT_MEM_ALLOC_FAIL Memory allocation failure * @retval CRYPT_SUCCESS Succeeded in setting the PSS. */ int32_t CRYPT_RSA_SetPss(CRYPT_RSA_Ctx *ctx, const EAL_MdMethod *hashMethod, const EAL_MdMethod *mgfMethod, uint32_t saltLen, const uint8_t *data, uint32_t dataLen, uint8_t *pad, uint32_t padLen); #endif // HITLS_CRYPTO_RSA_SIGN || HITLS_CRYPTO_RSA_BSSA #ifdef HITLS_CRYPTO_RSA_VERIFY /** * @ingroup rsa * @brief Compare the original data from the PSS. * * @param ctx [IN] CRYPT_RSA_Ctx * @param hashMethod [IN] pss Required the hash method * @param mgfMethod [IN] pss Internal hash method required by the mgf. * @param saltLen [IN] Salt value length * @param data [IN] Original data * @param dataLen [IN] Length of the original data * @param pad [IN] Data after PSS is set. * @param padLen [IN] Data length after PSS is set. * * @retval CRYPT_NULL_INPUT Invalid null pointer input * @retval CRYPT_RSA_ERR_PSS_SALT_DATA The salt value does not meet the requirements. * @retval CRYPT_RSA_ERR_PSS_SALT_LEN The salt length does not meet the requirements. * @retval CRYPT_RSA_BUFF_LEN_NOT_ENOUGH The length required for padding does not match the input parameter. * @retval CRYPT_MEM_ALLOC_FAIL Memory allocation failure * @retval CRYPT_SUCCESS pss comparison succeeded. */ int32_t CRYPT_RSA_VerifyPss(CRYPT_RSA_Ctx *ctx, const EAL_MdMethod *hashMethod, const EAL_MdMethod *mgfMethod, uint32_t saltLen, const uint8_t *data, uint32_t dataLen, const uint8_t *pad, uint32_t padLen); #endif // HITLS_CRYPTO_RSA_VERIFY #endif // HITLS_CRYPTO_RSA_EMSA_PSS #ifdef HITLS_CRYPTO_RSA_EMSA_PKCSV15 /** * @ingroup rsa * @brief Set pkcsv1.5 padding. * * @param hashId [IN] the hash method required by pkcsv1.5 setting. * @param data [IN] Original data * @param dataLen [IN] Length of the original data * @param pad [OUT] Pointer to the array for receiving the padding. * @param padLen [IN] Array length for receiving padding. * * @retval CRYPT_NULL_INPUT Invalid null pointer input * @retval CRYPT_RSA_NO_KEY_INFO The key information is insufficient. * @retval CRYPT_SECUREC_FAIL The security function fails. * @retval CRYPT_RSA_BUFF_LEN_NOT_ENOUGH The length required by the padding does not match the input parameter. * @retval CRYPT_RSA_ERR_INPUT_VALUE The hash algorithm ID is not supported. * @retval CRYPT_SUCCESS The pkcsv1.5 padding is successfully set. */ int32_t CRYPT_RSA_SetPkcsV15Type1(CRYPT_MD_AlgId hashId, const uint8_t *data, uint32_t dataLen, uint8_t *pad, uint32_t padLen); #ifdef HITLS_CRYPTO_RSA_VERIFY /** * @ingroup rsa * @brief Verify pkcsv1.5 padding. * * @param hashId [IN] the hash method corresponding to pkcsv1.5 verification. * @param pad [IN] Data after padding * @param padLen [IN] Data length after padding * @param data [IN] Original data * @param dataLen [IN] Length of the original data * * @retval CRYPT_NULL_INPUT Invalid null pointer input * @retval CRYPT_RSA_ERR_PKCSV15_SALT_DATA Incorrect padding value. * @retval CRYPT_SECUREC_FAIL Security Function Failure * @retval CRYPT_RSA_BUFF_LEN_NOT_ENOUGH The length required for padding does not match the input parameter. * @retval CRYPT_RSA_ERR_INPUT_VALUE The hash algorithm ID is not supported. * @retval CRYPT_SUCCESS Verify pkcsv1.5 is padded successfully. */ int32_t CRYPT_RSA_VerifyPkcsV15Type1(CRYPT_MD_AlgId hashId, const uint8_t *pad, uint32_t padLen, const uint8_t *data, uint32_t dataLen); #endif // HITLS_CRYPTO_RSA_VERIFY #endif // HITLS_CRYPTO_RSA_EMSA_PKCSV15 #if defined(HITLS_CRYPTO_RSA_SIGN) || defined(HITLS_CRYPTO_RSA_VERIFY) /** * @ingroup rsa * @brief Obtain the maximum length of RSA signature data. * * @param ctx [IN] Maximum length of the RSA signature data that is expected to be obtained * * @retval 0 The input is incorrect or the corresponding key structure does not contain valid key information. * @retval uint32_t Maximum length of the signature data */ uint32_t CRYPT_RSA_GetSignLen(const CRYPT_RSA_Ctx *ctx); #endif #ifdef HITLS_CRYPTO_RSA_VERIFY int32_t CRYPT_RSA_VerifyData(CRYPT_RSA_Ctx *ctx, const uint8_t *data, uint32_t dataLen, const uint8_t *sign, uint32_t signLen); int32_t CRYPT_RSA_Verify(CRYPT_RSA_Ctx *ctx, int32_t algId, const uint8_t *data, uint32_t dataLen, const uint8_t *sign, uint32_t signLen); #endif #ifdef HITLS_CRYPTO_RSA_SIGN int32_t CRYPT_RSA_SignData(CRYPT_RSA_Ctx *ctx, const uint8_t *data, uint32_t dataLen, uint8_t *sign, uint32_t *signLen); int32_t CRYPT_RSA_Sign(CRYPT_RSA_Ctx *ctx, int32_t algId, const uint8_t *data, uint32_t dataLen, uint8_t *sign, uint32_t *signLen); #endif #ifdef HITLS_CRYPTO_RSA_ENCRYPT /** * @ingroup rsa * @brief RSA public key encryption * * @param ctx [IN] RSA context structure * @param data [IN] Information to be encrypted * @param dataLen [IN] Length of the information to be encrypted * @param out [OUT] Pointer to the encrypted information output. * @param outLen [OUT] Pointer to the length of the encrypted information * * @retval CRYPT_NULL_INPUT Invalid null pointer input * @retval CRYPT_RSA_NO_KEY_INFO does not contain the key information. * @retval CRYPT_RSA_ERR_INPUT_VALUE The entered value does not meet the calculation conditions. * @retval CRYPT_RSA_BUFF_LEN_NOT_ENOUGH Outbuf Insufficient * @retval CRYPT_MEM_ALLOC_FAIL Memory allocation failure * @retval CRYPT_SECUREC_FAIL A safe function error occurs. * @retval BN error. An error occurs in the internal BigNum operation. * @retval CRYPT_EAL_ALG_NOT_SUPPORT does not register the encryption method. * @retval CRYPT_SUCCESS encryption succeeded. */ int32_t CRYPT_RSA_Encrypt(CRYPT_RSA_Ctx *ctx, const uint8_t *data, uint32_t dataLen, uint8_t *out, uint32_t *outLen); #endif #ifdef HITLS_CRYPTO_RSA_DECRYPT /** * @ingroup rsa * @brief RSA private key decryption * * @param ctx [IN] RSA context structure * @param data [IN] Information to be decrypted * @param dataLen [IN] Length of the information to be decrypted * @param out [OUT] Pointer to the output information after decryption. * @param outLen [OUT] Pointer to the length of the decrypted information * * @retval CRYPT_NULL_INPUT Error null pointer input * @retval CRYPT_RSA_NO_KEY_INFO does not contain the key information. * @retval CRYPT_RSA_ERR_INPUT_VALUE The entered value does not meet the calculation conditions. * @retval CRYPT_RSA_BUFF_LEN_NOT_ENOUGH Outbuf Insufficient * @retval CRYPT_MEM_ALLOC_FAIL Memory allocation failure * @retval CRYPT_SECUREC_FAIL A security function error occurs. * @retval CRYPT_EAL_ALG_NOT_SUPPORT does not register the decryption method. * @retval BN error. An error occurs in the internal BigNum operation. * @retval CRYPT_SUCCESS Decryption succeeded. */ int32_t CRYPT_RSA_Decrypt(CRYPT_RSA_Ctx *ctx, const uint8_t *data, uint32_t dataLen, uint8_t *out, uint32_t *outLen); #endif #ifdef HITLS_CRYPTO_RSA_VERIFY /** * @ingroup rsa * @brief RSA public key decryption * * @param ctx [IN] RSA context structure * @param data [IN] Information to be decrypted * @param dataLen [IN] Length of the information to be decrypted * @param out [OUT] Pointer to the output information after decryption. * @param outLen [IN/OUT] Pointer to the length of the decrypted information. * Before being transferred, the value must be set to the maximum length of the array. * * @retval CRYPT_NULL_INPUT Invalid null pointer input * @retval CRYPT_RSA_NO_KEY_INFO does not contain the key information. * @retval CRYPT_RSA_PAD_NO_SET_ERROR The padding type is not set. * @retval CRYPT_RSA_BUFF_LEN_NOT_ENOUGH The space is insufficient after decryption. * @retval CRYPT_RSA_ERR_INPUT_VALUE The input parameter does not meet the requirements. * @retval CRYPT_MEM_ALLOC_FAIL Memory allocation failure * @retval Other error codes, for example, the CRYPT_RSA_UnPackPkcsV15Type1 de-padding function. * @retval CRYPT_SUCCESS Decrypted Successfully */ int32_t CRYPT_RSA_Recover(CRYPT_RSA_Ctx *ctx, const uint8_t *data, uint32_t dataLen, uint8_t *out, uint32_t *outLen); #endif /** * @ingroup rsa * @brief RSA compare the public key * * @param a [IN] RSA context structure * @param b [IN] RSA context structure * * @retval CRYPT_SUCCESS is the same * @retval CRYPT_NULL_INPUT Invalid null pointer input * @retval CRYPT_RSA_NO_KEY_INFO No public key * @retval CRYPT_RSA_PUBKEY_NOT_EQUAL Public Keys are not equal */ int32_t CRYPT_RSA_Cmp(const CRYPT_RSA_Ctx *a, const CRYPT_RSA_Ctx *b); #ifdef HITLS_CRYPTO_RSAES_OAEP #ifdef HITLS_CRYPTO_RSA_ENCRYPT /** * @ingroup rsa * @brief oaep padding * * @param hashMethod [IN] Hash method. Only sha1, sha244, sha256, sha384, and sha512 are supported. * @param mgfMethod [IN] Hash method required by mgf * @param in [IN] Original data * @param inLen [IN] Original data length * @param param [IN] oaep parameter, which can be null * @param paramLen [IN] oaep Parameter length * @param pad [IN] Data after padding * @param padLen [IN] Data length after padding * * @retval CRYPT_NULL_INPUT Error null pointer input * @retval CRYPT_RSA_ERR_INPUT_VALUE The entered value does not meet the calculation conditions. * @retval CRYPT_SECUREC_FAIL A security function error occurs. * @retval CRYPT_MEM_ALLOC_FAIL Memory allocation failure * @retval CRYPT_RSA_BUFF_LEN_NOT_ENOUGH Outbuf Insufficient * */ int32_t CRYPT_RSA_SetPkcs1Oaep(CRYPT_RSA_Ctx *ctx, const uint8_t *in, uint32_t inLen, uint8_t *pad, uint32_t padLen); #endif // HITLS_CRYPTO_RSA_ENCRYPT #ifdef HITLS_CRYPTO_RSA_DECRYPT /** * @ingroup rsa * @brief Verify the oaep padding. * * @param pad [IN] oaep parameter, which can be null * @param in [IN] Data after padding * @param inLen [IN] Data length after padding * @param param [IN] oaep parameter, which can be null * @param paramLen [IN] oaep Parameter length * @param msg [IN] Data after the de-padding * @param msgLen [IN/OUT] The input parameter is the length of the msg buffer, * and the output parameter is the length of the msg after the de-padding. * * @retval CRYPT_NULL_INPUT Error null pointer input * @retval CRYPT_RSA_ERR_INPUT_VALUE The entered value does not meet the calculation conditions. * @retval CRYPT_SECUREC_FAIL A security function error occurs. * @retval CRYPT_MEM_ALLOC_FAIL Memory allocation failure * */ int32_t CRYPT_RSA_VerifyPkcs1Oaep(RSA_PadingPara *pad, const uint8_t *in, uint32_t inLen, const uint8_t *param, uint32_t paramLen, uint8_t *msg, uint32_t *msgLen); #endif // HITLS_CRYPTO_RSA_DECRYPT #endif // HITLS_CRYPTO_RSAES_OAEP #if defined(HITLS_CRYPTO_RSA_ENCRYPT) && \ (defined(HITLS_CRYPTO_RSAES_PKCSV15_TLS) || defined(HITLS_CRYPTO_RSAES_PKCSV15)) int32_t CRYPT_RSA_SetPkcsV15Type2(void *libCtx, const uint8_t *in, uint32_t inLen, uint8_t *out, uint32_t outLen); #endif #ifdef HITLS_CRYPTO_RSA_DECRYPT #ifdef HITLS_CRYPTO_RSAES_PKCSV15 int32_t CRYPT_RSA_VerifyPkcsV15Type2(const uint8_t *in, uint32_t inLen, uint8_t *out, uint32_t *outLen); #endif #ifdef HITLS_CRYPTO_RSAES_PKCSV15_TLS int32_t CRYPT_RSA_VerifyPkcsV15Type2TLS(const uint8_t *in, uint32_t inLen, uint8_t *out, uint32_t *outLen); #endif #endif // HITLS_CRYPTO_RSA_DECRYPT /** * @ingroup rsa * @brief rsa get security bits * * @param ctx [IN] rsa Context structure * * @retval security bits */ int32_t CRYPT_RSA_GetSecBits(const CRYPT_RSA_Ctx *ctx); #ifdef HITLS_CRYPTO_RSA_CHECK /** * @ingroup rsa * @brief check the key pair consistency * * @param checkType [IN] check type * @param pkey1 [IN] rsa key context structure * @param pkey2 [IN] rsa key context structure * * @retval CRYPT_SUCCESS check success. * Others. For details, see error code in errno. */ int32_t CRYPT_RSA_Check(uint32_t checkType, const CRYPT_RSA_Ctx *pkey1, const CRYPT_RSA_Ctx *pkey2); #endif // HITLS_CRYPTO_RSA_CHECK #ifdef HITLS_CRYPTO_PROVIDER /** * @ingroup RSA * @brief RSA import key * * @param ctx [IN/OUT] RSA context structure * @param params [IN] parameters */ int32_t CRYPT_RSA_Import(CRYPT_RSA_Ctx *ctx, const BSL_Param *params); /** * @ingroup RSA * @brief RSA export key * * @param ctx [IN] RSA context structure * @param params [IN/OUT] key parameters */ int32_t CRYPT_RSA_Export(const CRYPT_RSA_Ctx *ctx, BSL_Param *params); #endif // HITLS_CRYPTO_PROVIDER #ifdef __cplusplus } #endif #endif // HITLS_CRYPTO_RSA #endif // CRYPT_RSA_H 这是头文件以及rsa的源代码,请根据这些来编写上述的demo
最新发布
09-07
classdef WebSocketClient < handle %WEBSOCKETCLIENT WebSocketClient is an ABSTRACT class that allows %MATLAB to start a java-websocket client instance and connect to a %WebSocket server. % % In order to make a valid implementation of the class, some methods % must be defined in the superclass: % onOpen(obj,message) % onTextMessage(obj,message) % onBinaryMessage(obj,bytearray) % onError(obj,message) % onClose((obj,message) % The "callback" behavior of the client can be defined there. If % the client needs to perform actions that are not responses to a % server-caused event, these actions must be performed outside of the % callback methods. properties (SetAccess = private) URI % The URI of the server Secure = false % True if the connection is using WebSocketSecure Status = false % Status of the connection, true if the connection is open ClientObj % Java-WebSocket client object end properties (Access = private) HttpHeaders = {} % Cell array of additional headers {&#39;key1&#39;, &#39;value1&#39;,...} UseKeyStore = false KeyStore % Location of the keystore StorePassword % Keystore password KeyPassword % Key password end methods function obj = WebSocketClient(URI,varargin) % WebSocketClient Constructor % Creates a java client to connect to the designated server. % Arguments: URI, [keyStore, storePassword, keyPassword], [httpHeaders] % The URI must be of the form &#39;ws[s]://some.server.org:30000&#39;. obj.URI = lower(URI); if strfind(obj.URI,&#39;wss&#39;) obj.Secure = true; end % Parse optional arguments if nargin == 2 obj.HttpHeaders = varargin{1}; elseif obj.Secure && (nargin == 4 || nargin == 5) obj.UseKeyStore = true; obj.KeyStore = varargin{1}; obj.StorePassword = varargin{2}; obj.KeyPassword = varargin{3}; if nargin == 5 obj.HttpHeaders = varargin{4}; end elseif obj.Secure && nargin > 2 error(&#39;Invalid number of arguments for secure connection with keystore!&#39;); elseif ~obj.Secure && nargin > 2 warning([&#39;You are passing a keystore, but the given &#39;... &#39;server URI does not start with "wss". &#39;... &#39;The connection will not be secure.&#39;]); end % Check headers are valid if ~iscellstr(obj.HttpHeaders) || logical(mod(length(obj.HttpHeaders),2)) error([&#39;Invalid HTTP header format! You must pass a cell array&#39;... &#39;of key/value pairs: {&#39;&#39;key1&#39;&#39;, &#39;&#39;value1&#39;&#39;,...}&#39;]); end % Connect the client to the server obj.open(); end function status = get.Status(obj) % Get the status of the connection if isempty(obj.ClientObj) status = false; else status = obj.ClientObj.isOpen(); end end function open(obj) % Open the connection to the server % Create the java client object in with specified URI if obj.Status; warning(&#39;Connection is already open!&#39;);return; end import io.github.jebej.matlabwebsocket.* uri = handle(java.net.URI(obj.URI)); headers = handle(java.util.HashMap()); for i = 1:2:length(obj.HttpHeaders)-1 headers.put(obj.HttpHeaders{i}, obj.HttpHeaders{i+1}); end if obj.Secure && ~obj.UseKeyStore obj.ClientObj = handle(MatlabWebSocketSSLClient(uri, headers),&#39;CallbackProperties&#39;); elseif obj.Secure && obj.UseKeyStore obj.ClientObj = handle(MatlabWebSocketSSLClient(uri, headers,... obj.KeyStore,obj.StorePassword,obj.KeyPassword),&#39;CallbackProperties&#39;); else obj.ClientObj = handle(MatlabWebSocketClient(uri, headers),&#39;CallbackProperties&#39;); end % Set callbacks set(obj.ClientObj,&#39;OpenCallback&#39;,@obj.openCallback); set(obj.ClientObj,&#39;TextMessageCallback&#39;,@obj.textMessageCallback); set(obj.ClientObj,&#39;BinaryMessageCallback&#39;,@obj.binaryMessageCallback); set(obj.ClientObj,&#39;ErrorCallback&#39;,@obj.errorCallback); set(obj.ClientObj,&#39;CloseCallback&#39;,@obj.closeCallback); % Connect to the websocket server obj.ClientObj.connectBlocking(); end function close(obj) % Close the websocket connection and explicitely delete the % java client object if ~obj.Status; warning(&#39;Connection is already closed!&#39;);return; end obj.ClientObj.closeBlocking() delete(obj.ClientObj); obj.ClientObj = []; end function delete(obj) % Destructor % Closes the websocket if it&#39;s open. if obj.Status obj.close(); end end function send(obj,message) % Send a message to the server if ~obj.Status; warning(&#39;Connection is closed!&#39;);return; end if ~ischar(message) && ~isa(message,&#39;int8&#39;) && ~isa(message,&#39;uint8&#39;) error(&#39;You can only send character arrays or byte arrays!&#39;); end obj.ClientObj.send(message); end end % Implement these methods in a subclass. methods (Abstract, Access = protected) onOpen(obj,message) onTextMessage(obj,message) onBinaryMessage(obj,bytearray) onError(obj,message) onClose(obj,message) end % Private methods triggered by the callbacks defined above. methods (Access = private) function openCallback(obj,~,e) % Define behavior in an onOpen method of a subclass obj.onOpen(char(e.message)); end function textMessageCallback(obj,~,e) % Define behavior in an onTextMessage method of a subclass obj.onTextMessage(char(e.message)); end function binaryMessageCallback(obj,~,e) % Define behavior in an onBinaryMessage method of a subclass obj.onBinaryMessage(e.blob.array); end function errorCallback(obj,~,e) % Define behavior in an onError method of a subclass obj.onError(char(e.message)); end function closeCallback(obj,~,e) % Define behavior in an onClose method of a subclass obj.onClose(char(e.message)); % Delete java client object if needed if ~isvalid(obj); return; end delete(obj.ClientObj); obj.ClientObj = []; end end end
08-22
### 关于Node.js路径参数中的空字节错误 在Node.js中,当处理文件操作时,如果传递给`path`参数的字符串包含空字节(null byte),则会抛出类似于“path argument must be a string or Uint8Array without null bytes”的错误。这种错误通常发生在尝试打开或读取文件的过程中。 为了防止此类错误的发生,可以采取以下措施: 1. **验证输入数据** 在执行任何文件操作之前,应确保传入的路径不包含非法字符或空字节。可以通过正则表达式或其他方法来清理和验证路径字符串[^2]。 ```javascript function sanitizePath(path) { if (!Buffer.isUint8Array(path) && typeof path !== &#39;string&#39;) { throw new TypeError(&#39;Path must be a string or Uint8Array&#39;); } const sanitizedPath = Buffer.from(String(path), &#39;utf-8&#39;).toString(&#39;binary&#39;); // 移除可能存在的空字节 if (sanitizedPath.includes(&#39;\0&#39;)) { throw new Error(&#39;Path contains invalid null bytes&#39;); } return sanitizedPath; } ``` 2. **调试与日志记录** 如果无法确定为何会出现空字节,可以在程序运行期间增加详细的日志记录功能,以便捕获并分析异常情况下的路径值[^3]。 ```javascript try { const safePath = sanitizePath("john.dat"); console.log(`Using file at ${safePath}`); } catch (error) { console.error(&#39;Invalid path provided:&#39;, error.message); } ``` 通过上述方式能够有效减少因路径中含有空字节而导致的操作失败风险。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值