sm2_encrypt_asna1 {smcryptoR} | R Documentation |
SM2 Encrypt/Decrypt - asn.1
Description
According to the usage specifications of the SM2 algorithm, the encrypted data should be encoded using ASN.1, specifically including XCoordinate, YCoordinate, HASH, and CipherText. Among them, XCoordinate and YCoordinate each occupy 32 bytes, HASH occupies 32 bytes, and CipherText is the same length as the plaintext, plus a one-byte "04" identifier. After SM2 encryption and ASN.1 encoding, the ciphertext data will be 97 bytes longer than the original plaintext data.
Usage
sm2_encrypt_asna1(data, public_key)
sm2_decrypt_asna1(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_asna1
returns a raw vector of the cipher text in the asn.1 encoding
- sm2_decrypt_asna1
returns a raw vector of the plain text
Examples
## encrypt and decrypt as asn.1
keypair <- sm2_gen_keypair()
private_key <- keypair$private_key
public_key <- keypair$public_key
data <- 'abc' |> charToRaw()
enc <- sm2_encrypt_asna1(data, public_key)
enc
dec <- sm2_decrypt_asna1(enc, private_key)
dec
[Package smcryptoR version 0.1.2 Index]