Projects
PJyuIchi.NET
PJyuIchi.NET is a PKCS#11 wrapper class library for the .NET Framework.
PKCS#11 or Cryptoki defines a platform-independent API to cryptographic tokens, such as Hardware Security Modules (HSM) and smart cards. Cryptoki, pronounced crypto-key and short for cryptographic token interface, follows a simple object-based approach, addressing the goals of technology independence (any kind of device) and resource sharing (multiple applications accessing multiple devices), presenting to applications a common, logical view of the device called a cryptographic token.
PJyuIchi.NET is free for non-commercial use. Download the beta version from the following location.
Version 0.1.0.0 (beta):
- PJyuIchi.NET Class Library
- PJyuIchi.NET Help
- PJyuIchi.NET Test Application (requires PJyuIchi.NET.dll)
Sample C#
using System;
using System.Text;
using System.Security;
using Akita.Pkcs11;
using Akita.Pkcs11.Config;
using P11 = Akita.Pkcs11;
namespace Sample
{
class Program
{
static void Main(string[] args)
{
Config.Pkcs11LibraryPath = @"C:\Windows\System32\dkck201.dll";
Byte[] message = UTF8Encoding.UTF8.GetBytes("0123456789ABCDEF");
Session session = new Session(Slot.SlotList[0],
SessionInformationFlags.CKF_RW_SESSION);
session.Login(CK_USER_TYPE.CKU_USER, GetPinFromUser());
Template template = new Template();
template.Add(new P11.Attribute(CK_ATTRIBUTE_TYPE.CKA_CLASS, CK_OBJECT_CLASS.CKO_SECRET_KEY));
template.Add(new P11.Attribute(CK_ATTRIBUTE_TYPE.CKA_KEY_TYPE, CK_KEY_TYPE.CKK_DES3));
template.Add(new P11.Attribute(CK_ATTRIBUTE_TYPE.CKA_TOKEN, false));
template.Add(new P11.Attribute(CK_ATTRIBUTE_TYPE.CKA_ENCRYPT, true));
template.Add(new P11.Attribute(CK_ATTRIBUTE_TYPE.CKA_DECRYPT, true));
Key desKey = Key.Generate(session,
new Mechanism(session.Slot, CK_MECHANISM_TYPE.CKM_DES3_KEY_GEN),
template);
template.Dispose();
Byte[] IV = P11.Random.Generate(session, 16);
Encrypt encrypt = new Encrypt(desKey,
new Mechanism(session.Slot, CK_MECHANISM_TYPE.CKM_DES3_CBC,
IV));
Byte[] encrypted = encrypt.Final(message);
Decrypt decrypt = new Decrypt(desKey,
new Mechanism(session.Slot, CK_MECHANISM_TYPE.CKM_DES3_CBC,
IV));
Console.WriteLine(UTF8Encoding.UTF8.GetString(decrypt.Final(encrypted)));
desKey.Destroy();
session.Logout();
Console.WriteLine("Press any key...");
Console.ReadKey();
}
static SecureString GetPinFromUser()
{
SecureString pin = new SecureString();
// [...]
return pin;
}
}
}