CryptoKey: Encrypt, Decrypt, Sign, and Verify!

Automatically translated from English

4D solutions have always been safe and stable. This is because 4D invests heavily in developing and updating security features. 4D continues this focus in 4D v18 R4 with a new feature to further enhance its security toolkit. A new class providing a set of methods to perform common cryptographic operations is now available: CryptoKey class.

HDI Sign and verify

Cryptography functions

Cryptography functions can be used to implement security features such as privacy and authentication. The CryptoKey class provides the following cryptography functions:

  • sign() and verify(): create and verify digital signatures
  • encrypt() and decrypt(): encrypt and decrypt data


In addition to these operations, the CryptoKey class also allows you to generate new RSA or ECDSA keys, or load an existing key pair from a PEM definition.

Sign and verify

The sign() and verify() functions allow you to sign and verify message signatures. This makes it possible for you to provide proof of authenticity for digital messages or electronic documents.


Digital signatures provide:

  • Message authentication: proof that a known sender has created and signed the message
  • Message integrity: proof that the message was not altered after being signed
  • Non-repudiation: the signer cannot deny the signing of a document once the signature has been created


A message is signed by a private key and the signature is verified by the corresponding public key:


Here is a code snippet showing how you can sign and verify a message signature thanks to the methods of 4D’s new CryptoKey class:

Bob’s side

// Create the message
$message:="hello world"
Folder(fk desktop folder).file("message.txt").setText($message)

// Create a key
$type:=New object("type";"RSA")

// Get the public key and save it
Folder(fk desktop folder).file("public.pem").setText($key.getPublicKey())

// Get signature as base64 and save it
Folder(fk desktop folder).file("signature").setText($key.sign($message;$type))

/*Bob sent the message, the public key and the signature to Alice*/

Alice’s side

// Get message, public key & signature
$message:=Folder(fk desktop folder).file("message.txt").getText()
$publicKey:=Folder(fk desktop folder).file("public.pem").getText()
$signature:=Folder(fk desktop folder).file("signature").getText()

// Create a key
$type:=New object("type";"PEM";"pem";$publicKey)

// Verify signature
If ($key.verify($message;$signature;$type).success)

// The signature is valid

End if 


Encryption and decryption is performed using key pairs. The encryption process transforms the original information into an unrecognizable form, while the decryption process converts encrypted data into a form which can be read and understood by a human or a computer.

Effective security requires:

  • Private keys remain private
  • Public keys can be openly distributed without compromising security


Let’s say I want to encrypt “hello world”. Here’s how to do it:

$encrypted:=$key.encrypt("hello world")

And for decryption:


For more in-depth information, feel free to check out the documentation!

In an upcoming blog post, we’ll show you how far you can go with this class and the possibilities it offers for building sophisticated applications. In the meantime, feel free to share your crypto experiences on the 4D forum!

David Azancot

• 4D for iOS Product Owner •

David Azancot joined the 4D Product team as a Product Owner in 2017. He's in charge of writing the user stories and translating them into functional specifications. His role also includes making sure that feature implementations meet customers' needs.

David graduated with an MBA in Marketing, Internet and Mobility from the Leonard De Vinci Institute and began his career in 2011 with a mobile start-up company, later acquired by Madvertise (a mobile marketing group). Passionate about mobile interfaces, he was the natural choice to develop interactive mobile ad formats for the group in 2015. In parallel, David has been developing his own iOS and Android applications since 2012.