--- title: "Encryption" slug: "dsm-example-code-encryption" updated: 2025-04-02T09:47:38Z published: 2025-04-02T09:47:38Z canonical: "support.fortanix.com/dsm-example-code-encryption" --- > ## Documentation Index > Fetch the complete documentation index at: https://support.fortanix.com/llms.txt > Use this file to discover all available pages before exploring further. # Encryption ### 1.0 AES Encryption Encryption using an AES Key requires the following parameters to be set: - **Alg**: Same as the key type: *AES* - **Mode**: One of the supported values: ECB, CBC, CBCNOPAD, CFB, CTR, OFB, GCM, CCM, KW, KWP - **IV**: Initialization Vector can be passed when *Mode* is one of the values: CBC, CBCNOPAD, CFB, CTR, OFB, GCM, CCM. If IV is not passed, then Fortanix DSM will generate a random IV for the operation and return in the response. One should note this IV for decryption purposes later. - **Tag Len**: For mode GCM and CCM, tag length needs to be passed. - **AD**: For mode GCM and CCM, Authentication Data needs to be passed. ### 2.0 RSA Encryption Encryption using an RSA Key requires the following parameters to be set: - **Alg**: Same as the key type: *RSA* - **Mode**: One of the supported values: PKCS1_V15, OAEP_MGF1_SHA1, OAEP_MGF1_SHA256, OAEP_MGF1_SHA384, OAEP_MGF1_SHA512 > [!NOTE] > NOTE > > RSA key cannot encrypt data that is larger than the size of the key. For example, with RSA 1024 key, one can encrypt data not larger than 1024 bits. - [C++](/docs/dsm-example-code-encryption#tabs-1) - [C#](/docs/dsm-example-code-encryption#tabs-2) - [Go](/docs/dsm-example-code-encryption#tabs-3) - [Java](/docs/dsm-example-code-encryption#tabs-4) - [Python](/docs/dsm-example-code-encryption#tabs-5) - [PHP](/docs/dsm-example-code-encryption#tabs-6) - [Javascript](/docs/dsm-example-code-encryption#tabs-7) - [REST API using curl](/docs/dsm-example-code-encryption#tabs-8) ### C++ ```bash >string encrypt(CK_FUNCTION_LIST_PTR p11, CK_SESSION_HANDLE hSession, CK_OBJECT_HANDLE hKey, string plain) { CK_RV rv; CK_BYTE *iv; CK_ULONG iv_len; string iv_b64; CK_BYTE *cipher; CK_ULONG cipher_len; string cipher_b64; iv_len = (CK_ULONG) AES_KEYLENGTH/8; iv = (CK_BYTE *)malloc(iv_len * sizeof(CK_BYTE)); CK_MECHANISM mechanism = { CKM_AES_CBC_PAD, iv, iv_len }; Base64::Encode(string((char *)iv, iv_len), &iv_b64); rv = p11->C_EncryptInit(hSession, &mechanism, hKey); if (rv == CKR_OK) { rv = p11->C_Encrypt(hSession, (CK_BYTE_PTR) plain.c_str(), plain.length(), NULL, &cipher_len); if (rv == CKR_OK) { cipher = (CK_BYTE *)malloc(cipher_len * sizeof(CK_BYTE)); rv = p11->C_Encrypt(hSession, (CK_BYTE_PTR) plain.c_str(), plain.length(), cipher, &cipher_len); } } if (rv != CKR_OK) { cout << "Encryption failed. Error code = " << rv << endl; return string(); } Base64::Encode(string((char *)cipher, cipher_len), &cipher_b64); return iv_b64 + ":" + cipher_b64; } ``` ### C# ```bash public void encrypt() {    EncryptRequest encryptRequest = new EncryptRequest(Alg: ObjectType.AES, Mode: CryptMode.CBC, Plain: Encoding.ASCII.GetBytes("Hello World"));    EncryptionAndDecryptionApi encryptionAndDecryptionApi = new EncryptionAndDecryptionApi();    EncryptResponse encResp = encryptionAndDecryptionApi.Encrypt(name, encryptRequest); } ``` ### Go ```plaintext //AES Encryption data := byte[]("Hello World!") keyName := encryptReq := sdkms.EncryptRequest{            Plain: byte[]("Hello World!"),            Alg: sdkms.AlgorithmAes,            Key: sdkms.SobjectByName(keyName),            Mode: sdkms.CryptModeSymmetric(sdkms.CipherModeCbc), } encryptResp, err := client.Encrypt(ctx, encryptReq) encryptResp.Cipher //encrypted data as bytes encryptResp.Iv //Initialization vector //RSA Encryption padding := sdkms.RsaEncryptionPaddingOAEPMGF1( sdkms.DigestAlgorithmSha1) encryptReq := sdkms.EncryptRequest{            Plain: byte[]("Hello World!"),            Alg: sdkms.AlgorithmRsa,            Key: sdkms.SobjectByName(),            Mode: sdkms.CryptModeRSA(padding), } encryptResp, err := client.Encrypt(ctx, encryptReq) encryptResp.Cipher //encrypted data as bytes encryptResp.Iv //Initialization vector ``` ### Java ```bash //AES Encryption String data = "Hello World!"; byte[] plain = data.getBytes(); EncryptRequest encryptRequest = new EncryptRequest(); encryptRequest       .alg(ObjectType.AES)       .plain(plain)       .mode(CryptMode.CBC); EncryptResponse encryptResponse = encryptionAndDecryptionApi.encrypt(, encryptRequest); encryptResponse.cipher //encrypted data in bytes[] encryptResponse.iv //Initialization vector //RSA Encryption String data = "Hello World!"; byte[] plain = data.getBytes(); EncryptRequest encryptRequest = new EncryptRequest(); encryptRequest       .alg(ObjectType.RSA)       .plain(plain)       .mode(CryptMode.OAEP_MGF1_SHA1); EncryptResponse encryptResponse = encryptionAndDecryptionApi.encrypt(, encryptRequest); encryptResponse.cipher //encrypted data in bytes[] ``` ### Python ```bash #AES Encryption api_instance = sdkms.v1.EncryptionAndDecryptionApi(api_client=client) data = "Hello World!" request = sdkms.v1.EncryptRequest(           alg=ObjectType.AES,           plain= bytearray (data),           mode=CipherMode.CBC) encryption_response = api_instance.encrypt(, request) encryption_response.cipher #encrypted data in bytearray encryption_response.iv #Initialization vector #RSA Encryption api_instance = sdkms.v1.EncryptionAndDecryptionApi(api_client=client) data = "Hello World!" request = sdkms.v1.EncryptRequest(           alg=ObjectType.RSA,           plain= bytearray (data),           mode=CipherMode.OAEP_MGF1_SHA1) encryption_response = api_instance.encrypt(, request) encryption_response.cipher #encrypted data in bytearray ``` ### PHP ```bash #AES Encryption public function encrypt() {    $cryptMode = new Swagger\Client\Model\CryptMode();    $encryptionRequestBody = array('alg' => $objType::AES, 'mode' => $cryptMode::CBC, 'plain' => $plain);    $encryptionRequest = new Swagger\Client\Model\EncryptRequest($encryptionRequestBody);    $encryptionAndDecryptionApi = new Swagger\Client\Api\EncryptionAndDecryptionApi($client);    $encryptionResponse = $encryptionAndDecryptionApi->encrypt($securityObjectResponse["name"], $encryptionRequest); } ``` ### Javascript ```bash //AES Encryption var encryptCallback = function(error, data, response) {    if (error) {        console.error("Error: " + JSON.stringify(response));    } else {        console.log('Data encrypted successfully. result: ' + JSON.stringify(data));    } }; var encryptApi = new FortanixSdkmsRestApi.EncryptionAndDecryptionApi() var plain = btoa("Hello World!") var encryptRequest = FortanixSdkmsRestApi.EncryptRequest.constructFromObject({"alg" :"AES", "plain": plain, "mode": "CBC"}) encryptApi.encrypt(data["name"], encryptRequest, encryptCallback) ``` ### REST API using curl ```bash #AES Encryption $ echo "Hello World!" | base64 SGVsbG8gV29ybGQhCg== $ curl /crypto/v1/encrypt -H 'Authorization: Bearer YhXwwa-6C...ig5g' -d '{"key": {"name": ""}, "alg": "AES", "mode": "FPE", "plain": "SGVsbG8gV29ybGQhCg=="}' #RSA Encryption $ echo "Hello World!" | base64 SGVsbG8gV29ybGQhCg== $ curl /crypto/v1/encrypt -H 'Authorization: Bearer YhXwwa-6C...ig5g' -d '{"key": {"name": ""}, "alg": "RSA", "mode": "OAEP_MGF1_SHA1", "plain": "SGVsbG8gV29ybGQhCg=="}' {"cipher": " YiBmaHViIGNpdXJl…ZyB1eXZpZyB2ZQoK"} ``` Fortanix Data Security Manager (DSM) is the world’s first cloud service secured with Intel® SGX. With Fortanix DSM, you can securely generate, store, and use cryptographic keys and certificates, as well as other secrets such as passwords, API keys, tokens, or any blob of data. Your business-critical applications and containers can integrate with Fortanix DSM using legacy cryptographic interfaces (PKCS#11, CNG, and JCE) or using the native Fortanix DSM RESTful interface. ## Related - [Decryption](/dsm-example-code-decryption.md) - [Fortanix DSM SDK Contents](/fortanix-data-security-manager-sdk-contents.md) - [PKCS#11 Supported Functions and Mechanisms](/fortanix-dsm-clients-pkcs11-supported-functions-and-mechanisms.md) - [Signature Generation](/dsm-example-code-signature-generation.md)