Encrypt
JavaScript
Java
Python
PHP
Golang
const { EncryptionClient, KeyClient, RecordClient, AesEncrypter } = require("@bloock/sdk");
let payload = "This will be encrypted";
console.log(`The following payload will be encrypted: ${payload}`);
const recordClient = new RecordClient();
const keyClient = new KeyClient();
let key = await keyClient.newLocalKey(KeyType.Aes256);
// To encrypt a record during the building process
let encryptedRecord = await recordClient.fromString(payload)
.withEncrypter(new AesEncrypter(key))
.build();
// To encrypt a record independently
const encryptionClient = new EncryptionClient();
let record = await recordClient.fromString(payload).build();
encryptedRecord = await encryptionClient.encrypt(record, new AesEncrypter(key));
console.log(`Encryption successfully`);
let encryptedPayload = encryptedRecord.retrieve();
console.log(
`Encrypted payload: ${new TextDecoder().decode(encryptedPayload)}`
);
import com.bloock.sdk.client.EncryptionClient;
import com.bloock.sdk.client.RecordClient;
import com.bloock.sdk.entity.AesEncrypter;
import com.bloock.sdk.entity.Record;
import java.nio.charset.StandardCharsets;
public class App {
public static void main(String[] args) throws Exception {
String payload = "This will be encrypted";
System.out.println("The following payload will be encrypted: " + payload);
RecordClient recordClient = new RecordClient();
KeyClient keyClient = new KeyClient();
LocalKey localKey = keyClient.newLocalKey(KeyType.Aes256);
// To encrypt a record during the building process
Record encryptedRecord = recordClient.fromString(payload)
.withEncrypter(new AesEncrypter(localKey))
.build();
// To encrypt a record independently
EncryptionClient encryptionClient = new EncryptionClient();
Record record = recordClient.fromString(payload).build();
encryptedRecord = encryptionClient.encrypt(record, new AesEncrypter(localKey));
System.out.println("Encryption successful");
System.out.println("Encrypted payload: " + new String(encryptedRecord.retrieve(), StandardCharsets.UTF_8));
}
}
from bloock.entity.encrypter import AesEncrypter
from bloock.client.encryption import EncryptionClient
from bloock.client.record import RecordClient
payload = "This will be encrypted"
record_client = RecordClient()
encryption_client = EncryptionClient()
key_client = KeyClient()
print(f'The following payload will be encrypted: "{payload}"')
key = key_client.new_local_key(KeyType.Aes256)
# To encrypt a record during the building process
encrypted_record = (
record_client.from_string(payload)
.with_encrypter(AesEncrypter(EncrypterArgs(key)))
.build()
)
# To encrypt a record independently
record = record_client.from_string(payload).build()
encrypted_record = encryption_client.encrypt(record, AesEncrypter(EncrypterArgs(key)))
print("Encryption successful")
print(f"Encrypted payload: {encrypted_record.retrieve().decode()}")
<?php
require "./vendor/autoload.php";
use Bloock\Bloock;
use Bloock\Client\KeyClient;
use Bloock\Client\RecordClient;
use Bloock\Entity\Encryption\AesEncrypter;
use Bloock\Entity\Encryption\EncrypterArgs;
use Bloock\Entity\Key\KeyType;
Bloock::$apiKey = getenv("API_KEY");
$keyClient = new KeyClient();
$payload = "This will be encrypted";
$key = $keyClient->newLocalKey(KeyType::Aes256);
$recordClient = new RecordClient();
$encryptedRecord = $recordClient->fromString($payload)
->withEncrypter(new AesEncrypter(new EncrypterArgs($key)))
->build();
$encryptionClient = new \Bloock\Client\EncryptionClient();
$record = $recordClient->fromString($payload)->build();
$encryptedRecord = $encryptionClient->encrypt($record, new AesEncrypter(new EncrypterArgs($key)));
print "Encryption successful\n";
$encryptedPayload = $encryptedRecord->retrieve();
$cipher = implode(array_map("chr", $encryptedPayload));
print "Encrypted payload: $cipher\n";
package main
import (
"fmt"
"github.com/bloock/bloock-sdk-go/v2/client"
"github.com/bloock/bloock-sdk-go/v2/entity"
)
func main() {
payload := "This will be encrypted"
password := "a STRONG password"
encryptionClient := client.NewEncryptionClient()
recordClient := client.NewRecordClient()
keyClient := client.NewKeyClient()
fmt.Printf("The following payload will be encrypted: %s", payload)
key, err := keyClient.NewLocalKey(key.Aes256)
if err != nil {
log.Println(err)
}
// To encrypt a record during the building process
encryptedRecord, err := recordClient.FromString(payload).
WithEncrypter(entity.NewAesEncrypter(encryption.EncrypterArgs{
LocalKey: &key,
})).
Build()
if err != nil {
log.Println(err)
}
// To encrypt a record independently
record, err := recordClient.FromString(payload).Build()
if err != nil {
log.Println(err)
}
encryptedRecord, err := encryptionClient.Encrypt(record, entity.NewAesEncrypter(encryption.EncrypterArgs{
LocalKey: &key,
}))
if err != nil {
log.Println(err)
}
fmt.Println("Encryption successful")
fmt.Println("Encrypted payload: " + string(encryptedRecord.Retrieve()))
}
JavaScript
Java
Python
Golang
PHP
const { EncryptionClient, RecordClient, KeyClient, RsaEncrypter } = require("@bloock/sdk");
let payload = "This will be encrypted";
console.log(`The following payload will be encrypted: ${payload}`);
const encryptionClient = new EncryptionClient();
const keyClient = new KeyClient();
const recordClient = new RecordClient();
let key = await keyClient.newLocalKey(KeyType.Rsa2048);
// To encrypt a record during the building process
let encryptedRecord = await recordClient.fromString(payload)
.withEncrypter(new RsaEncrypter(key))
.build();
// To encrypt a record independently
let record = await recordClient.fromString(payload).build();
encryptedRecord = await encryptionClient.encrypt(record, new RsaEncrypter(key));
console.log(`Encryption successfully`);
let encryptedPayload = encryptedRecord.retrieve();
console.log(
`Encrypted payload: ${new TextDecoder().decode(encryptedPayload)}`
);
import com.bloock.sdk.client.EncryptionClient;
import com.bloock.sdk.client.RecordClient;
import com.bloock.sdk.entity.Record;
import com.bloock.sdk.entity.RsaEncrypter;
import com.bloock.sdk.entity.RsaKeyPair;
import java.nio.charset.StandardCharsets;
public class App {
public static void main(String[] args) throws Exception {
String payload = "This will be encrypted";
System.out.println("The following payload will be encrypted: " + payload);
RecordClient recordClient = new RecordClient();
KeyClient keyClient = new KeyClient();
LocalKey localKey = keyClient.newLocalKey(KeyType.Rsa2048);
// To encrypt a record during the building process
Record encryptedRecord = recordClient.fromString(payload)
.withEncrypter(new RsaEncrypter(localKey))
.build();
// To encrypt a record independently
EncryptionClient encryptionClient = new EncryptionClient();
Record record = recordClient.fromString(payload).build();
encryptedRecord = encryptionClient.encrypt(record, new RsaEncrypter(localKey));
System.out.println("Encryption successful");
System.out.println("Encrypted payload: " + new String(record.retrieve(), StandardCharsets.UTF_8));
}
}
from bloock.entity.encrypter import RsaEncrypter
from bloock.client.encryption import EncryptionClient
from bloock.client.record import RecordClient
payload = "This will be encrypted"
record_client = RecordClient()
encryption_client = EncryptionClient()
key_client = KeyClient()
print(f'The following payload will be encrypted: "{payload}"')
key = key_client.new_local_key(KeyType.Rsa2048)
# To encrypt a record during the building process
encrypted_record = (
record_client.from_string(payload)
.with_encrypter(RsaEncrypter(EncrypterArgs(key)))
.build()
)
# To encrypt a record independently
record = record_client.from_string(payload).build()
encrypted_record = encryption_client.encrypt(record, RsaEncrypter(EncrypterArgs(key)))
print("Encryption successful")
print(f"Encrypted payload: {encrypted_record.retrieve().decode()}")
package main
import (
"fmt"
"github.com/bloock/bloock-sdk-go/v2/client"
"github.com/bloock/bloock-sdk-go/v2/entity"
)
func main() {
payload := "This will be encrypted"
encryptionClient := client.NewEncryptionClient()
recordClient := client.NewRecordClient()
keyClient := client.NewKeyClient()
key, err := keyClient.NewLocalKey(key.Rsa2048)
if err != nil {
log.Println(err)
}
fmt.Printf("The following payload will be encrypted: %s", payload)
// To encrypt a record during the building process
encryptedRecord, err := recordClient.FromString(payload).
WithEncrypter(entity.NewRsaEncrypter(encryption.EncrypterArgs{
LocalKey: &key,
})).
Build()
if err != nil {
log.Println(err)
}
// To encrypt a record independently
record, err := recordClient.FromString(payload).Build()
if err != nil {
log.Println(err)
}
encryptedRecord, err := encryptionClient.Encrypt(record, entity.NewRsaEncrypter(encryption.EncrypterArgs{
LocalKey: &key,
}))
if err != nil {
log.Println(err)
}
fmt.Println("Encryption successful")
fmt.Println("Encrypted payload: " + string(encryptedRecord.Retrieve()))
}
<?php
require "./vendor/autoload.php";
use Bloock\Bloock;
use Bloock\Client\EncryptionClient;
use Bloock\Client\KeyClient;
use Bloock\Client\RecordClient;
use Bloock\Entity\Encryption\EncrypterArgs;
use Bloock\Entity\Encryption\RsaEncrypter;
use Bloock\Entity\Key\KeyType;
Bloock::$apiKey = getenv("API_KEY");
$keyClient = new KeyClient();
$payload = "This will be encrypted";
$key = $keyClient->newLocalKey(KeyType::Rsa2048);
$recordClient = new RecordClient();
$encryptedRecord = $recordClient->fromString($payload)
->withEncrypter(new RsaEncrypter(new EncrypterArgs($key)))
->build();
$encryptionClient = new EncryptionClient();
$record = $recordClient->fromString($payload)->build();
$encryptedRecord = $encryptionClient->encrypt($record, new RsaEncrypter(new EncrypterArgs($key)));
print "Encryption successful\n";
$encryptedPayload = $encryptedRecord->retrieve();
$cipher = implode(array_map("chr", $encryptedPayload));
print "Encrypted payload: $cipher\n";
Last modified 1d ago