Encrypt
Symmetric (AES)
- Typescript
- Python
- Java
- PHP
- Golang
import {
Encrypter,
EncryptionClient,
KeyClient,
KeyType,
RecordClient,
} from '@bloock/sdk';
const payload = 'This will be encrypted';
console.log(`The following payload will be encrypted: ${payload}`);
const recordClient = new RecordClient();
const keyClient = new KeyClient();
const key = await keyClient.newLocalKey(KeyType.Aes256);
// To encrypt a record during the building process
let encryptedRecord = await recordClient
.fromString(payload)
.withEncrypter(new Encrypter(key))
.build();
// To encrypt a record independently
const encryptionClient = new EncryptionClient();
const record = await recordClient.fromString(payload).build();
encryptedRecord = await encryptionClient.encrypt(record, new Encrypter(key));
console.log(`Encryption successfully`);
const encryptedPayload = encryptedRecord.retrieve();
console.log(`Encrypted payload: ${new TextDecoder().decode(encryptedPayload)}`);
from bloock.entity.encryption.encrypter import Encrypter
from bloock.entity.key.key_type import KeyType
from bloock.client.encryption import EncryptionClient
from bloock.client.record import RecordClient
from bloock.client.key import KeyClient
if __name__ == "__main__":
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(Encrypter(key))
.build()
)
print("Encryption successful")
print(f"Encrypted payload: {encrypted_record.retrieve().decode()}")
# To encrypt a record independently
record = record_client.from_string(payload).build()
encrypted_record = encryption_client.encrypt(record, Encrypter(key))
print("Encryption successful")
print(f"Encrypted payload: {encrypted_record.retrieve().decode()}")
import com.bloock.sdk.client.EncryptionClient;
import com.bloock.sdk.client.KeyClient;
import com.bloock.sdk.client.RecordClient;
import com.bloock.sdk.entity.encryption.Encrypter;
import com.bloock.sdk.entity.key.KeyType;
import com.bloock.sdk.entity.key.LocalKey;
import com.bloock.sdk.entity.record.Record;
import java.nio.charset.StandardCharsets;
public class EncryptAes {
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 Encrypter(localKey))
.build();
// To encrypt a record independently
EncryptionClient encryptionClient = new EncryptionClient();
Record record = recordClient.fromString(payload).build();
encryptedRecord = encryptionClient.encrypt(record, new Encrypter(localKey));
System.out.println("Encryption successful");
System.out.println("Encrypted payload: " + new String(encryptedRecord.retrieve(), StandardCharsets.UTF_8));
}
}
<?php
require "./vendor/autoload.php";
use Bloock\Bloock;
use Bloock\Client\KeyClient;
use Bloock\Client\RecordClient;
use Bloock\Entity\Encryption\Encrypter;
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 Encrypter($key))
->build();
$encryptionClient = new \Bloock\Client\EncryptionClient();
$record = $recordClient->fromString($payload)->build();
$encryptedRecord = $encryptionClient->encrypt($record, new Encrypter($key));
print "Encryption successful\n";
$encryptedPayload = $encryptedRecord->retrieve();
$cipher = implode(array_map("chr", $encryptedPayload));
print "Encrypted payload: $cipher\n";
package main
import (
"fmt"
"log"
"github.com/bloock/bloock-sdk-go/v2/client"
"github.com/bloock/bloock-sdk-go/v2/entity/encryption"
"github.com/bloock/bloock-sdk-go/v2/entity/key"
)
func main() {
payload := "This will be encrypted"
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(encryption.NewEncrypterWithLocalKey(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, encryption.NewEncrypterWithLocalKey(key))
if err != nil {
log.Println(err)
}
fmt.Println("Encryption successful")
fmt.Println("Encrypted payload: " + string(encryptedRecord.Retrieve()))
}
Asymmetric (RSA)
- Typescript
- Python
- Java
- PHP
- Golang
import {
Encrypter,
EncryptionClient,
KeyClient,
KeyType,
RecordClient,
} from '@bloock/sdk';
const 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();
const key = await keyClient.newLocalKey(KeyType.Rsa2048);
// To encrypt a record during the building process
let encryptedRecord = await recordClient
.fromString(payload)
.withEncrypter(new Encrypter(key))
.build();
// To encrypt a record independently
const record = await recordClient.fromString(payload).build();
encryptedRecord = await encryptionClient.encrypt(record, new Encrypter(key));
console.log(`Encryption successfully`);
const encryptedPayload = encryptedRecord.retrieve();
console.log(`Encrypted payload: ${new TextDecoder().decode(encryptedPayload)}`);
from bloock.entity.encryption.encrypter import Encrypter
from bloock.entity.key.key_type import KeyType
from bloock.client.encryption import EncryptionClient
from bloock.client.record import RecordClient
from bloock.client.key import KeyClient
if __name__ == "__main__":
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(Encrypter(key))
.build()
)
print("Encryption successful")
print(f"Encrypted payload: {encrypted_record.retrieve().decode()}")
# To encrypt a record independently
record = record_client.from_string(payload).build()
encrypted_record = encryption_client.encrypt(record, Encrypter(key))
print("Encryption successful")
print(f"Encrypted payload: {encrypted_record.retrieve().decode()}")
import com.bloock.sdk.client.EncryptionClient;
import com.bloock.sdk.client.KeyClient;
import com.bloock.sdk.client.RecordClient;
import com.bloock.sdk.entity.encryption.Encrypter;
import com.bloock.sdk.entity.key.KeyType;
import com.bloock.sdk.entity.key.LocalKey;
import com.bloock.sdk.entity.record.Record;
import java.nio.charset.StandardCharsets;
public class EncryptRsa {
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 Encrypter(localKey))
.build();
// To encrypt a record independently
EncryptionClient encryptionClient = new EncryptionClient();
Record record = recordClient.fromString(payload).build();
encryptedRecord = encryptionClient.encrypt(record, new Encrypter(localKey));
System.out.println("Encryption successful");
System.out.println("Encrypted payload: " + new String(record.retrieve(), StandardCharsets.UTF_8));
}
}
<?php
require "./vendor/autoload.php";
use Bloock\Bloock;
use Bloock\Client\EncryptionClient;
use Bloock\Client\KeyClient;
use Bloock\Client\RecordClient;
use Bloock\Entity\Encryption\Encrypter;
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 Encrypter($key))
->build();
$encryptionClient = new EncryptionClient();
$record = $recordClient->fromString($payload)->build();
$encryptedRecord = $encryptionClient->encrypt($record, new Encrypter($key));
print "Encryption successful\n";
$encryptedPayload = $encryptedRecord->retrieve();
$cipher = implode(array_map("chr", $encryptedPayload));
print "Encrypted payload: $cipher\n";
package main
import (
"fmt"
"log"
"github.com/bloock/bloock-sdk-go/v2/client"
"github.com/bloock/bloock-sdk-go/v2/entity/encryption"
"github.com/bloock/bloock-sdk-go/v2/entity/key"
)
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(encryption.NewEncrypterWithLocalKey(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, encryption.NewEncrypterWithLocalKey(key))
if err != nil {
log.Println(err)
}
fmt.Println("Encryption successful")
fmt.Println("Encrypted payload: " + string(encryptedRecord.Retrieve()))
}