sm2_encrypt {smcryptoR}R Documentation

SM2 Encrypt/Decrypt

Description

SM2 is an asymmetric encryption algorithm that can also be used to directly encrypt data. Typically, A encrypts a file or data using the public key, passes the ciphertext to B, and B decrypts it using the corresponding private key. SM2 encryption and decryption are suitable for shorter texts only. For larger files, the process can be very slow. According to the SM2 algorithm usage specifications, the encrypted ciphertext needs to be ASN.1 encoded. We provide the functions sm2_encrypt_asna1 and sm2_decrypt_asna1 for this purpose. Additionally, some scenarios use different arrangements of c1, c2, c3, so we also offer the functions sm2_encrypt_c1c2c3 and sm2_decrypt_c1c2c3. To facilitate the transmission of binary data, we also provide functions to encrypt data into hexadecimal or base64 strings and decrypt from them.

Usage

sm2_encrypt(data, public_key)

sm2_decrypt(data, private_key)

Arguments

data

data to be encrypted or decrypted, must be a raw vector

public_key

a public key represented as a hexadecimal string

private_key

a private key represented as a hexadecimal string

Value

sm2_encrypt

returns a raw vector of the cipher text

sm2_decrypt

returns a raw vector of the plain text

Examples

## encrypt and decrypt - raw
keypair <- sm2_gen_keypair()
private_key <- keypair$private_key
public_key <- keypair$public_key
data <- 'abc' |> charToRaw()
enc <- sm2_encrypt(data, public_key)
enc
dec <- sm2_decrypt(enc, private_key)
dec

[Package smcryptoR version 0.1.2 Index]