PDFSM4TryDecryptString

Cryptography

Description

Decrypts an IV-prefixed SM4-CBC string produced by PDFSM4EncryptString and strips the PKCS#7 padding, reporting success as a Boolean instead of relying on an empty result. The first 16 bytes of the ciphertext are the IV, the remainder is the CBC body.

The input contract is strict: the key must be exactly 16 bytes, the ciphertext at least 32 bytes and a multiple of 16. Anything else fails without raising, and a wrong key surfaces as a padding failure.

Syntax

Delphi

Function PDFSM4TryDecryptString(Const Key, Ciphertext: AnsiString;
  Out Plaintext: AnsiString): Boolean;

Parameters

KeyExactly 16 bytes
CiphertextAt least 32 bytes and a multiple of 16; the first 16 bytes are the IV
PlaintextReceives the unpadded plaintext on success; set to an empty string on failure

Return values

TrueThe CBC body decrypted and carried valid PKCS#7 padding; Plaintext holds the original bytes
FalseThe input violates the length contract or the padding check failed, which is how a wrong key or damaged ciphertext presents itself

Remarks

Part of the pure-Pascal SM4 block cipher (GB/T 32907-2016).

See also

PDFSM4EncryptString, PDFSM4DecryptString, PDFSM4DecryptStringBuffer, SM4TryUnpadPKCS7, SM4DecryptCBC