/// <summary> /// Initializes a new instance of the CkAesCbcEncryptDataParams class. /// </summary> /// <param name='aesKeyBits'>Length of the temporary AES key in bits</param> /// <param name='oaepParams'>Parameters of the temporary AES key wrapping</param> public CkRsaAesKeyWrapParams(ulong aesKeyBits, CkRsaPkcsOaepParams oaepParams) { if (oaepParams == null) { throw new ArgumentNullException("oaepParams"); } // Keep the reference to OAEP params so GC will not free it while this object exists _oaepParams = oaepParams; if (Platform.UnmanagedLongSize == 4) { if (Platform.StructPackingSize == 0) { _params40 = new HighLevelAPI40.MechanismParams.CkRsaAesKeyWrapParams(Convert.ToUInt32(aesKeyBits), _oaepParams._params40); } else { _params41 = new HighLevelAPI41.MechanismParams.CkRsaAesKeyWrapParams(Convert.ToUInt32(aesKeyBits), _oaepParams._params41); } } else { if (Platform.StructPackingSize == 0) { _params80 = new HighLevelAPI80.MechanismParams.CkRsaAesKeyWrapParams(aesKeyBits, _oaepParams._params80); } else { _params81 = new HighLevelAPI81.MechanismParams.CkRsaAesKeyWrapParams(aesKeyBits, _oaepParams._params81); } } }
/// <summary> /// Disposes object /// </summary> /// <param name="disposing">Flag indicating whether managed resources should be disposed</param> protected virtual void Dispose(bool disposing) { if (!this._disposed) { if (disposing) { // Dispose managed objects if (_params40 != null) { _params40.Dispose(); _params40 = null; } if (_params41 != null) { _params41.Dispose(); _params41 = null; } if (_params80 != null) { _params80.Dispose(); _params80 = null; } if (_params81 != null) { _params81.Dispose(); _params81 = null; } // Release the reference to OAEP params so GC knows this object doesn't need it anymore _oaepParams = null; } // Dispose unmanaged objects _disposed = true; } }
public void _03_EncryptAndDecryptSinglePartOaepTest() { using (Pkcs11 pkcs11 = new Pkcs11(Settings.Pkcs11LibraryPath, Settings.UseOsLocking)) { // Find first slot with token present Slot slot = Helpers.GetUsableSlot(pkcs11); // Open RW session using (Session session = slot.OpenSession(false)) { // Login as normal user session.Login(CKU.CKU_USER, Settings.NormalUserPin); // Generate key pair ObjectHandle publicKey = null; ObjectHandle privateKey = null; Helpers.GenerateKeyPair(session, out publicKey, out privateKey); // Specify mechanism parameters CkRsaPkcsOaepParams mechanismParams = new CkRsaPkcsOaepParams((ulong)CKM.CKM_SHA_1, (ulong)CKG.CKG_MGF1_SHA1, (ulong)CKZ.CKZ_DATA_SPECIFIED, null); // Specify encryption mechanism with parameters Mechanism mechanism = new Mechanism(CKM.CKM_RSA_PKCS_OAEP, mechanismParams); byte[] sourceData = ConvertUtils.Utf8StringToBytes("Hello world"); // Encrypt data byte[] encryptedData = session.Encrypt(mechanism, publicKey, sourceData); // Do something interesting with encrypted data // Decrypt data byte[] decryptedData = session.Decrypt(mechanism, privateKey, encryptedData); // Do something interesting with decrypted data Assert.IsTrue(Convert.ToBase64String(sourceData) == Convert.ToBase64String(decryptedData)); session.DestroyObject(privateKey); session.DestroyObject(publicKey); session.Logout(); } } }