Decrypt
Symmetric (AES)
- Typescript
- Python
- Java
- PHP
- Golang
import {
Encrypter,
EncryptionClient,
KeyClient,
KeyType,
Record,
RecordClient,
} from '@bloock/sdk';
// encryption ...
const recordClient = new RecordClient();
const keyClient = new KeyClient();
const key = await keyClient.newLocalKey(KeyType.Aes256);
// encryption ...
let encryptedRecord: Record;
// To decrypt a record during the building process
// we build a record from the encrypted record and add a decrypter
let decryptedRecord = await recordClient
.fromRecord(encryptedRecord)
.withDecrypter(new Encrypter(key))
.build();
// To decrypt an already encrypted record independently
const encryptionClient = new EncryptionClient();
decryptedRecord = await encryptionClient.decrypt(
encryptedRecord,
new Encrypter(key)
);
const decryptedPayload = decryptedRecord.retrieve();
console.log(`Decryption successfully`);
console.log(`Decrypted payload: ${new TextDecoder().decode(decryptedPayload)}`);
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()
# encryption ...
encrypted_record = record_client.from_string("Hello world").build()
print("Trying to decrypt with the valid password")
key = key_client.new_local_key(KeyType.Aes256)
# To decrypt a record during the building process
# we build a record from the encrypted record and add a decrypter
decrypted_record = (
record_client.from_record(encrypted_record)
.with_decrypter(Encrypter(key))
.build()
)
# To decrypt an already encrypted record independently
decrypted_record = encryption_client.decrypt(
encrypted_record, Encrypter(key)
)
print("Decryption successful")
print(f'Decrypted payload: "{decrypted_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 DecryptAes {
public static void main(String[] args) throws Exception {
String payload = "This will be encrypted";
// encryption ...
Record encryptedRecord = null;
System.out.println("Trying to decrypt with the valid password");
RecordClient recordClient = new RecordClient();
KeyClient keyClient = new KeyClient();
LocalKey localKey = keyClient.newLocalKey(KeyType.Aes256);
// To decrypt a record during the building process
// we build a record from the encrypted record and add a decrypter
Record decryptedRecord = recordClient.fromRecord(encryptedRecord)
.withDecrypter(new Encrypter(localKey))
.build();
// To decrypt an already encrypted record independently
EncryptionClient encryptionClient = new EncryptionClient();
Record decryptedRecord1 = encryptionClient.decrypt(encryptedRecord, new Encrypter(localKey));
System.out.println(
"Decrypted payload: " + new String(decryptedRecord.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();
$recordClient = new RecordClient();
$payload = "This will be encrypted";
$key = $keyClient->newLocalKey(KeyType::Aes256);
// encryption ...
$encryptedRecord = $recordClient->fromString($payload)
->withEncrypter(new Encrypter($key))
->build();
$decryptedRecord = $recordClient->fromRecord($encryptedRecord)
->withDecrypter(new Encrypter($key))
->build();
$encryptionClient = new EncryptionClient();
$decryptedRecord = $encryptionClient->decrypt($encryptedRecord, new Encrypter($key));
$decryptedPayload = $decryptedRecord->retrieve();
print "Decryption successfully\n";
$decrypted = implode(array_map("chr", $decryptedPayload));
print "Decrypted payload: $decrypted";
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"
"github.com/bloock/bloock-sdk-go/v2/entity/record"
)
func main() {
recordClient := client.NewRecordClient()
encryptionClient := client.NewEncryptionClient()
keyClient := client.NewKeyClient()
key, err := keyClient.NewLocalKey(key.Aes256)
if err != nil {
log.Println(err)
}
// encryption ...
var encryptedRecord record.Record
fmt.Println("Trying to decrypt with the valid password")
// To decrypt a record during the building process
// we build a record from the encrypted record and add a decrypter
decryptedRecord, err := recordClient.FromRecord(encryptedRecord).
WithDecrypter(encryption.NewEncrypterWithLocalKey(key)).
Build()
if err != nil {
log.Println(err)
}
// To decrypt an already encrypted record independently
decryptedRecord, err = encryptionClient.Decrypt(encryptedRecord, encryption.NewEncrypterWithLocalKey(key))
if err != nil {
log.Println(err)
}
fmt.Println("Decryption successful")
fmt.Println("Decrypted payload: " + string(decryptedRecord.Retrieve()))
}
Asymmetric (RSA)
- Typescript
- Python
- Java
- PHP
- Golang
import {
Encrypter,
EncryptionClient,
KeyClient,
KeyType,
Record,
RecordClient,
} from '@bloock/sdk';
// encryption ...
const recordClient = new RecordClient();
const keyClient = new KeyClient();
const key = await keyClient.newLocalKey(KeyType.Rsa2048);
// encryption ...
let encryptedRecord: Record;
// To decrypt a record during the building process
// we build a record from the encrypted record and add a decrypter
let decryptedRecord = await recordClient
.fromRecord(encryptedRecord)
.withDecrypter(new Encrypter(key))
.build();
// To decrypt an already encrypted record independently
const encryptionClient = new EncryptionClient();
decryptedRecord = await encryptionClient.decrypt(
encryptedRecord,
new Encrypter(key)
);
const decryptedPayload = decryptedRecord.retrieve();
console.log(`Decryption successfully`);
console.log(`Decrypted payload: ${new TextDecoder().decode(decryptedPayload)}`);
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()
# an encrypted record
encrypted_record = record_client.from_string("Hello world").build()
print("Trying to decrypt with the valid password")
key = key_client.new_local_key(KeyType.Rsa2048)
# To decrypt a record during the building process
# we build a record from the encrypted record and add a decrypter
decrypted_record = (
record_client.from_record(encrypted_record)
.with_decrypter(Encrypter(key))
.build()
)
# To decrypt an already encrypted record independently
decrypted_record = encryption_client.decrypt(
encrypted_record, Encrypter(key)
)
print("Decryption successful")
print(f'Decrypted payload: "{decrypted_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 DecryptRsa {
public static void main(String[] args) throws Exception {
String payload = "This will be encrypted";
// encryption ...
Record encryptedRecord = null;
RecordClient recordClient = new RecordClient();
KeyClient keyClient = new KeyClient();
LocalKey localKey = keyClient.newLocalKey(KeyType.Rsa2048);
// To decrypt a record during the building process
// we build a record from the encrypted record and add a decrypter
Record decryptedRecord = recordClient.fromRecord(encryptedRecord)
.withDecrypter(new Encrypter(localKey))
.build();
// To decrypt an already encrypted record independently
EncryptionClient encryptionClient = new EncryptionClient();
decryptedRecord = encryptionClient.decrypt(encryptedRecord, new Encrypter(localKey));
System.out.println(
"Decrypted payload: " + new String(decryptedRecord.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();
$recordClient = new RecordClient();
$payload = "This will be encrypted";
$key = $keyClient->newLocalKey(KeyType::Rsa2048);
// encryption ...
$encryptedRecord = $recordClient->fromString($payload)
->withEncrypter(new Encrypter($key))
->build();
$decryptedRecord = $recordClient->fromRecord($encryptedRecord)
->withDecrypter(new Encrypter($key))
->build();
$encryptionClient = new EncryptionClient();
$decryptedRecord = $encryptionClient->decrypt($encryptedRecord, new Encrypter($key));
$decryptedPayload = $decryptedRecord->retrieve();
print "Decryption successfully\n";
$decrypted = implode(array_map("chr", $decryptedPayload));
print "Decrypted payload: $decrypted";
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"
"github.com/bloock/bloock-sdk-go/v2/entity/record"
)
func main() {
recordClient := client.NewRecordClient()
encryptionClient := client.NewEncryptionClient()
keyClient := client.NewKeyClient()
key, err := keyClient.NewLocalKey(key.Rsa2048)
if err != nil {
log.Println(err)
}
// encryption ...
var encryptedRecord record.Record
fmt.Println("Trying to decrypt with the valid password")
// To decrypt a record during the building process
// we build a record from the encrypted record and add a decrypter
decryptedRecord, err := recordClient.FromRecord(encryptedRecord).
WithDecrypter(encryption.NewEncrypterWithLocalKey(key)).
Build()
if err != nil {
log.Println(err)
}
// To decrypt an already encrypted record independently
decryptedRecord, err = encryptionClient.Decrypt(encryptedRecord, encryption.NewEncrypterWithLocalKey(key))
if err != nil {
log.Println(err)
}
fmt.Println("Decryption successful")
fmt.Println("Decrypted payload: " + string(decryptedRecord.Retrieve()))
}