API .Net HSM Dinamo
API Proprietária .Net do HSM Dinamo
rsa_enc_dec.cs

Exemplo de encriptação e decriptação RSA direta.

Veja Nota sobre os exemplos.
using System;
using System.IO;
using Dinamo.Hsm;
namespace RSAEncDec
{
class Program
{
static void Main(string[] args)
{
string address = "127.0.0.1";
string user = "master";
string pass = "12345678";
DinamoClient hsm = new DinamoClient();
/*
Conecta ao HSM
*/
hsm.Connect(address, user, pass);
/*
Gera uma chave de testes
*/
string keyId = "key_id";
IntPtr keyHandle = hsm.GenerateKey(keyId,
DinamoClient.KEY_ALG.ALG_RSA_2048,
true);
/*
Monta buffer de encriptação. Deve ter o tamanho do bloco da
chave e conter os dados que serão encriptados.
*/
byte[] data = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
byte[] buffer = new byte[256];
data.CopyTo(buffer, 0);
Console.WriteLine("Dados entrada(len {0}): {1}", data.Length,
BitConverter.ToString(data));
/*
Recupera o handle da chave pública.
*/
byte[] pubKeyData = hsm.ExportKey(keyHandle, IntPtr.Zero,
DinamoClient.BLOB_TYPE.PUBLICKEY_BLOB);
string pubKeyId = "rsa2048_pub";
IntPtr pubKeyHandle = hsm.ImportKey(pubKeyId, IntPtr.Zero,
pubKeyData,
DinamoClient.BLOB_TYPE.PUBLICKEY_BLOB,
DinamoClient.KEY_ALG.ALG_RSA_2048_PUB);
/*
Encripta usando padding PKCS#1 v1.5 tipo 2
*/
int outDataLen = data.Length;
Console.WriteLine("OutDataLen: {0}", outDataLen);
hsm.Encrypt(pubKeyHandle, IntPtr.Zero, true, 0, null,
DinamoClient.MODE_TYPE.MODE_NONE,
DinamoClient.PADDING_TYPE.PKCS1_PADDING, buffer,
ref outDataLen, buffer.Length);
Console.WriteLine("Dados encriptados (len {0}): {1}", outDataLen,
BitConverter.ToString(buffer));
/*
Decripta usaando padding PKCS#1 v1.5 tipo 2
*/
outDataLen = buffer.Length;
hsm.Decrypt(keyHandle, IntPtr.Zero, true, 0, null,
DinamoClient.MODE_TYPE.MODE_NONE,
DinamoClient.PADDING_TYPE.PKCS1_PADDING, buffer,
ref outDataLen);
Console.WriteLine("Dados decriptados (len {0}): {1}", outDataLen,
BitConverter.ToString(buffer));
/*
Libera handle, remove chave e desconecta
*/
hsm.DestroyKey(keyHandle);
hsm.RemoveObject(keyId);
hsm.Disconnect();
}
}
}
Namespace que denota um conjunto de funções para acesso ao HSM Dinamo e suas respectivas exceptions.
Definition: DinamoClient.cs:12
Definition: DinamoClient.cs:12