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

Example of symmetric encryption and decryption using HSM.

See Note on examples.
using System;
using System.Collections.Generic;
using System.Text;
using Dinamo.Hsm;
namespace TesteNF
{
class Program
{
static void Main(string[] args)
{
DinamoClient dinamo = new DinamoClient();
dinamo.Connect( "10.0.62.16",
"master",
"12345678" );
//Nome da chave AES 256 dentro do HSM
string keyId = "aes256";
byte[] iv = new byte[DinamoApi. AES_IV_LEN]{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
//Dado original
byte[] originalData = Encoding.ASCII.GetBytes("asdfasdfasdfasdfas");
//Adiciona o tamanho do bloco para caber um possivel padding.
//O tamanho do IV eh igual ao tamanho do bloco.
byte[] dataToEncrypt = new byte[originalData.Length + DinamoApi.AES_IV_LEN];
//Bloco que sera encriptado. Estes dados serao sobrescritos.
Array.Copy(originalData, dataToEncrypt, originalData.Length);
int dataToEncryptLen = originalData.Length;
dinamo.Encrypt( keyId,
IntPtr.Zero,
true,
iv,
DinamoClient. PADDING_TYPE.PKCS5_PADDING,
dataToEncrypt,
ref dataToEncryptLen,
dataToEncrypt.Length);
dinamo.Decrypt( keyId,
IntPtr.Zero,
true,
iv,
dataToEncrypt,
ref dataToEncryptLen );
Console.Out.WriteLine("Dado decriptado:\n {0}", System.Text.Encoding.Default.GetString(dataToEncrypt));
Console.In.ReadLine();
}
}
}
Low-level class for accessing the HSM. To use this class you need to understand more ...
Definition DinamoApi.cs:15
const Int32 AES_IV_LEN
Definition DinamoApi.cs:903
API class for accessing HSM functionalities Dinamo. In this class you can program using...
Definition DinamoClient.cs:93
MODE_TYPE
Definition DinamoClient.cs:430
PADDING_TYPE
Definition DinamoClient.cs:437
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 Connect(string User, string Password)
Establishes an encrypted connection to the HSM using the load balance settings.
Definition DinamoClient.cs:562
Namespace denoting a set of functions for accessing the HSM Dinamo and their respective exceptions.
Definition DinamoClient.cs:12