Exemplo de encriptação e decriptação RSA direta.
using System;
using System.IO;
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();
hsm.Connect(address, user, pass);
string keyId = "key_id";
IntPtr keyHandle = hsm.GenerateKey(keyId,
DinamoClient.KEY_ALG.ALG_RSA_2048,
true);
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));
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);
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));
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));
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