NET API
HSM Dinamo
Loading...
Looking for...
No entries found
rsa_enc_dec.cs

Example of direct RSA encryption and decryption.

See Note on examples.
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";
/*
Connects to HSM
*/
hsm.Connect(address, user, pass);
/*
Generates a test key
*/
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
key and contain the data that will be encrypted.
*/
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));
/*
Retrieves the public key handle.
*/
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);
/*
Encrypts using PKCS#1 v1.5 type 2 padding
*/
int outDataLen = data.Length;
Console.WriteLine("OutDataLen: {0}", outDataLen);
hsm.Encrypt(pubKeyHandle, IntPtr.Zero, true, 0, null,
DinamoClient. PADDING_TYPE.PKCS1_PADDING, buffer,
ref outDataLen, buffer.Length);
Console.WriteLine("Dados encriptados (len {0}): {1}", outDataLen,
BitConverter.ToString(buffer));
/*
Decrypt using PKCS#1 v1.5 type 2 padding
*/
outDataLen = buffer.Length;
hsm.Decrypt(keyHandle, IntPtr.Zero, true, 0, null,
DinamoClient.PADDING_TYPE.PKCS1_PADDING, buffer,
ref outDataLen);
Console.WriteLine("Dados decriptados (len {0}): {1}", outDataLen,
BitConverter.ToString(buffer));
/*
Release handle, remove key and disconnect
*/
hsm.DestroyKey(keyHandle);
hsm.RemoveObject(keyId);
hsm.Disconnect();
}
}
}
API class for accessing HSM functionalities Dinamo. In this class you can program using...
Definition DinamoClient.cs:93
KEY_ALG
Definition DinamoClient.cs:227
MODE_TYPE
Definition DinamoClient.cs:430
PADDING_TYPE
Definition DinamoClient.cs:437
BLOB_TYPE
Definition DinamoClient.cs:449
void Encrypt(IntPtr hKey, bool Final, byte[] byData, ref int DataLen, int BufferLen)
Encrypt blocks or files by passing the key reference. Uses default mode/padding,...
Definition DinamoClient.cs:3163
void Decrypt(string strKeyId, IntPtr hHash, bool Final, byte[] byData, ref int DataLen)
Decrypts a hash, general data or a file.
Definition DinamoClient.cs:3504
void DestroyKey(IntPtr hKey)
Release key handle.
Definition DinamoClient.cs:1953
byte[] ExportKey(IntPtr hKey, IntPtr hKeyEncryptionKey, BLOB_TYPE BlobType)
Definition DinamoClient.cs:2648
IntPtr ImportKey(string KeyId, IntPtr hKeyEncryptionKey, byte[] byKeyBlob, BLOB_TYPE BlobType, KEY_ALG AlgId)
Definition DinamoClient.cs:2668
IntPtr GenerateKey(string KeyId, DinamoClient.KEY_ALG Alg, bool Exportable)
Generates a permanent key in the HSM.
Definition DinamoClient.cs:1891
void RemoveObject(string ObjectId)
Removes an object from the HSM.
Definition DinamoClient.cs:2397
void Connect(string User, string Password)
Establishes an encrypted connection to the HSM using the load balance settings.
Definition DinamoClient.cs:562
void Disconnect(bool flagClose)
Terminates the connection to the HSM.
Definition DinamoClient.cs:814
Namespace denoting a set of functions for accessing the HSM Dinamo and their respective exceptions.
Definition DinamoClient.cs:12