openharmony 鸿蒙 capi-crypto-sym-cipher-h

2026-08-25 浏览 (1)

crypto_sym_cipher.h

Overview

Defines APIs for encrypting and decrypting symmetric keys.

Header file: <CryptoArchitectureKit/crypto_sym_cipher.h>

Library: libohcrypto.so

System capability: SystemCapability.Security.CryptoFramework

Since: 12

Related module: CryptoSymCipherApi

Summary

Structs

Nametypedef KeywordDescription
OH_CryptoSymCipherOH_CryptoSymCipherDefines a symmetric encryption and decryption instance.
OH_CryptoSymCipherParamsOH_CryptoSymCipherParamsDefines symmetric encryption and decryption parameters.

Enums

Nametypedef KeywordDescription
CryptoSymCipher_ParamsTypeCryptoSymCipher_ParamsTypeEnumerates the types of symmetric encryption and decryption parameters.

Functions

NameDescription
OH_Crypto_ErrCode OH_CryptoSymCipherParams_Create(OH_CryptoSymCipherParams **params)Creates a symmetric encryption/decryption parameter instance.
Note: The created resource must be destroyed by calling OH_CryptoSymCipherParams_Destroy.
OH_Crypto_ErrCode OH_CryptoSymCipherParams_SetParam(OH_CryptoSymCipherParams *params, CryptoSymCipher_ParamsType paramsType, Crypto_DataBlob *value)Sets symmetric encryption/decryption parameters.
void OH_CryptoSymCipherParams_Destroy(OH_CryptoSymCipherParams *params)Destroys a symmetric encryption/decryption parameter instance.
OH_Crypto_ErrCode OH_CryptoSymCipher_Create(const char *algoName, OH_CryptoSymCipher **ctx)Creates a symmetric encryption/decryption instance based on the given algorithm name.
Note: The created resource must be destroyed by calling OH_CryptoSymCipher_Destroy.
OH_Crypto_ErrCode OH_CryptoSymCipher_Init(OH_CryptoSymCipher *ctx, Crypto_CipherMode mod, OH_CryptoSymKey *key, OH_CryptoSymCipherParams *params)Initializes a symmetric encryption/decryption instance.
OH_Crypto_ErrCode OH_CryptoSymCipher_Update(OH_CryptoSymCipher *ctx, Crypto_DataBlob *in, Crypto_DataBlob *out)Updates the data to be encrypted or decrypted.
Note: After the use is complete, the memory for storing the out parameter must be released by calling OH_Crypto_FreeDataBlob.
OH_Crypto_ErrCode OH_CryptoSymCipher_Final(OH_CryptoSymCipher *ctx, Crypto_DataBlob *in, Crypto_DataBlob *out)Outputs the remaining data (generated by the block cipher mode) and finishes the encryption or decryption operation.
Note: After the use is complete, the memory for storing the out parameter must be released by calling OH_Crypto_FreeDataBlob.
const char *OH_CryptoSymCipher_GetAlgoName(OH_CryptoSymCipher *ctx)Obtains the symmetric encryption/decryption algorithm.
void OH_CryptoSymCipher_Destroy(OH_CryptoSymCipher *ctx)Destroys a symmetric encryption/decryption instance.

Enum Description

CryptoSymCipher_ParamsType

enum CryptoSymCipher_ParamsType

Description

Enumerates the types of symmetric encryption and decryption parameters.

Since: 12

Enum ItemDescription
CRYPTO_IV_DATABLOB = 100Parameters such as iv.
CRYPTO_AAD_DATABLOB = 101Additional authentication data in GCM mode.
CRYPTO_TAG_DATABLOB = 102Output tag of the encryption operation, which is used for integrity check.

Function Description

OH_CryptoSymCipherParams_Create()

OH_Crypto_ErrCode OH_CryptoSymCipherParams_Create(OH_CryptoSymCipherParams **params)

Description

Creates a symmetric encryption/decryption parameter instance.
Note: The created resource must be destroyed by calling OH_CryptoSymCipherParams_Destroy.

Since: 12

Parameters

NameDescription
OH_CryptoSymCipherParams **paramsPointer to the symmetric encryption/decryption parameter instance.

Returns

TypeDescription
OH_Crypto_ErrCodeCRYPTO_SUCCESS: The operation is successful.
CRYPTO_INVALID_PARAMS: A parameter is invalid.
CRYPTO_NOT_SUPPORTED: The operation is not supported.
CRYPTO_MEMORY_ERROR: A memory error occurs.
CRYPTO_OPERTION_ERROR: Failed to call an API of a third-party algorithm library.

OH_CryptoSymCipherParams_SetParam()

OH_Crypto_ErrCode OH_CryptoSymCipherParams_SetParam(OH_CryptoSymCipherParams *params, CryptoSymCipher_ParamsType paramsType, Crypto_DataBlob *value)

Description

Sets symmetric encryption/decryption parameters.

Since: 12

Parameters

NameDescription
OH_CryptoSymCipherParams *paramsPointer to the symmetric encryption/decryption parameter instance.
CryptoSymCipher_ParamsType paramsTypeName of the parameter to set.
Crypto_DataBlob *valuePointer to the parameter value set.

Returns

TypeDescription
OH_Crypto_ErrCodeCRYPTO_SUCCESS: The operation is successful.
CRYPTO_INVALID_PARAMS: A parameter is invalid.
CRYPTO_NOT_SUPPORTED: The operation is not supported.
CRYPTO_MEMORY_ERROR: A memory error occurs.
CRYPTO_OPERTION_ERROR: Failed to call an API of a third-party algorithm library.

OH_CryptoSymCipherParams_Destroy()

void OH_CryptoSymCipherParams_Destroy(OH_CryptoSymCipherParams *params)

Description

Destroys a symmetric encryption/decryption parameter instance.

Since: 12

Parameters

NameDescription
OH_CryptoSymCipherParams *paramsPointer to the symmetric encryption/decryption parameter instance.

OH_CryptoSymCipher_Create()

OH_Crypto_ErrCode OH_CryptoSymCipher_Create(const char *algoName, OH_CryptoSymCipher **ctx)

Description

Creates a symmetric encryption/decryption instance based on the given algorithm name.
Note: The created resource must be destroyed by calling OH_CryptoSymCipher_Destroy.

Since: 12

Parameters

NameDescription
const char *algoNamePointer to the algorithm used to generate a cipher instance.
For example, AES128|GCM|PKCS7.
OH_CryptoSymCipher **ctxPointer to the symmetric cipher instance created.

Returns

TypeDescription
OH_Crypto_ErrCodeCRYPTO_SUCCESS: The operation is successful.
CRYPTO_INVALID_PARAMS: A parameter is invalid.
CRYPTO_NOT_SUPPORTED: The operation is not supported.
CRYPTO_MEMORY_ERROR: A memory error occurs.
CRYPTO_PARAMETER_CHECK_FAILED: Parameter check failed.
CRYPTO_OPERTION_ERROR: Failed to call an API of a third-party algorithm library.

OH_CryptoSymCipher_Init()

OH_Crypto_ErrCode OH_CryptoSymCipher_Init(OH_CryptoSymCipher *ctx, Crypto_CipherMode mod, OH_CryptoSymKey *key, OH_CryptoSymCipherParams *params)

Description

Initializes a symmetric encryption/decryption instance.

Since: 12

Parameters

NameDescription
OH_CryptoSymCipher *ctxPointer to the symmetric key cipher instance.
Crypto_CipherMode modOperation to perform, encryption or decryption.
OH_CryptoSymKey *keyPointer to the symmetric key.
OH_CryptoSymCipherParams *paramsPointer to the parameters for encryption or decryption.

Returns

TypeDescription
OH_Crypto_ErrCodeCRYPTO_SUCCESS: The operation is successful.
CRYPTO_INVALID_PARAMS: A parameter is invalid.
CRYPTO_NOT_SUPPORTED: The operation is not supported.
CRYPTO_MEMORY_ERROR: A memory error occurs.
CRYPTO_PARAMETER_CHECK_FAILED: Parameter check failed.
CRYPTO_OPERTION_ERROR: Failed to call an API of a third-party algorithm library.

Reference

OH_CryptoSymCipher_Update

OH_CryptoSymCipher_Final

OH_CryptoSymCipher_Update()

OH_Crypto_ErrCode OH_CryptoSymCipher_Update(OH_CryptoSymCipher *ctx, Crypto_DataBlob *in, Crypto_DataBlob *out)

Description

Updates the data to be encrypted or decrypted.
Note: After the use is complete, the memory for storing the out parameter must be released by calling OH_Crypto_FreeDataBlob.

Since: 12

Parameters

NameDescription
OH_CryptoSymCipher *ctxPointer to the symmetric cipher instance.
Crypto_DataBlob *inPointer to the data to be encrypted or decrypted.
Crypto_DataBlob *outPointer to the data updated.

Returns

TypeDescription
OH_Crypto_ErrCodeCRYPTO_SUCCESS: The operation is successful.
CRYPTO_INVALID_PARAMS: A parameter is invalid.
CRYPTO_NOT_SUPPORTED: The operation is not supported.
CRYPTO_MEMORY_ERROR: A memory error occurs.
CRYPTO_PARAMETER_CHECK_FAILED: Parameter check failed.
CRYPTO_OPERTION_ERROR: Failed to call an API of a third-party algorithm library.

Reference

OH_CryptoSymCipher_Init

OH_CryptoSymCipher_Final

OH_CryptoSymCipher_Final()

OH_Crypto_ErrCode OH_CryptoSymCipher_Final(OH_CryptoSymCipher *ctx, Crypto_DataBlob *in, Crypto_DataBlob *out)

Description

Outputs the remaining data (generated by the block cipher mode) and finishes the encryption or decryption operation.
Note: After the use is complete, the memory for storing the out parameter must be released by calling OH_Crypto_FreeDataBlob.

Since: 12

Parameters

NameDescription
OH_CryptoSymCipher *ctxPointer to the symmetric key cipher instance.
Crypto_DataBlob *inPointer to the data to be encrypted or decrypted.
Crypto_DataBlob *outPointer to the remaining data encrypted or decrypted.

Returns

TypeDescription
OH_Crypto_ErrCodeCRYPTO_SUCCESS: The operation is successful.
CRYPTO_INVALID_PARAMS: A parameter is invalid.
CRYPTO_NOT_SUPPORTED: The operation is not supported.
CRYPTO_MEMORY_ERROR: A memory error occurs.
CRYPTO_PARAMETER_CHECK_FAILED: Parameter check failed.
CRYPTO_OPERTION_ERROR: Failed to call an API of a third-party algorithm library.

Reference

OH_CryptoSymCipher_Init

OH_CryptoSymCipher_Update

OH_CryptoSymCipher_GetAlgoName()

const char *OH_CryptoSymCipher_GetAlgoName(OH_CryptoSymCipher *ctx)

Description

Obtains the symmetric encryption/decryption algorithm.

Since: 12

Parameters

NameDescription
OH_CryptoSymCipher *ctxPointer to the symmetric cipher instance.

Returns

TypeDescription
const char *Symmetric key encryption algorithm obtained.

OH_CryptoSymCipher_Destroy()

void OH_CryptoSymCipher_Destroy(OH_CryptoSymCipher *ctx)

Description

Destroys a symmetric encryption/decryption instance.

Since: 12

Parameters

NameDescription
OH_CryptoSymCipher *ctxPointer to the symmetric cipher instance.

你可能感兴趣的鸿蒙文章

openharmony 鸿蒙 capi-cryptosymkeyapi-oh-cryptosymkey

openharmony 鸿蒙 capi-cryptoasymkeyapi-oh-cryptoasymkeygeneratorwithspec

openharmony 鸿蒙 capi-cryptoasymcipherapi-oh-cryptoasymcipher

openharmony 鸿蒙 capi-cryptokdfapi-oh-cryptokdfparams

openharmony 鸿蒙 capi-cryptomacapi

openharmony 鸿蒙 capi-cryptodigestapi-oh-cryptodigest

openharmony 鸿蒙 capi-cryptomacapi-oh-cryptomac

openharmony 鸿蒙 capi-cryptosignatureapi-oh-cryptoeccsignaturespec

openharmony 鸿蒙 capi-cryptocommonapi-crypto-datablob

openharmony 鸿蒙 capi-cryptoasymcipherapi-oh-cryptosm2ciphertextspec

  • 所属分类: 后端技术
  • 本文标签: 鸿蒙 软件
  • 版权声明: 本文链接 https://seaxiang.com/blog/qQIYThBD