public void Decrypt(Mechanism mechanism, ObjectHandle keyHandle, Stream inputStream, Stream outputStream, int bufferLength)
{
if (this._disposed)
throw new ObjectDisposedException(this.GetType().FullName);
if (mechanism == null)
throw new ArgumentNullException("mechanism");
if (keyHandle == null)
throw new ArgumentNullException("keyHandle");
if (inputStream == null)
throw new ArgumentNullException("inputStream");
if (outputStream == null)
throw new ArgumentNullException("outputStream");
if (bufferLength < 1)
throw new ArgumentException("Value has to be positive number", "bufferLength");
CK_MECHANISM ckMechanism = mechanism.CkMechanism;
CKR rv = _p11.C_DecryptInit(_sessionId, ref ckMechanism, keyHandle.ObjectId);
if (rv != CKR.CKR_OK)
throw new Pkcs11Exception("C_DecryptInit", rv);
byte[] encryptedPart = new byte[bufferLength];
byte[] part = new byte[bufferLength];
ulong partLen = Convert.ToUInt64(part.Length);
int bytesRead = 0;
while ((bytesRead = inputStream.Read(encryptedPart, 0, encryptedPart.Length)) > 0)
{
partLen = Convert.ToUInt64(part.Length);
rv = _p11.C_DecryptUpdate(_sessionId, encryptedPart, Convert.ToUInt64(bytesRead), part, ref partLen);
if (rv != CKR.CKR_OK)
throw new Pkcs11Exception("C_DecryptUpdate", rv);
outputStream.Write(part, 0, Convert.ToInt32(partLen));
}
byte[] lastPart = null;
ulong lastPartLen = 0;
rv = _p11.C_DecryptFinal(_sessionId, null, ref lastPartLen);
if (rv != CKR.CKR_OK)
throw new Pkcs11Exception("C_DecryptFinal", rv);
lastPart = new byte[lastPartLen];
rv = _p11.C_DecryptFinal(_sessionId, lastPart, ref lastPartLen);
if (rv != CKR.CKR_OK)
throw new Pkcs11Exception("C_DecryptFinal", rv);
if (lastPartLen > 0)
outputStream.Write(lastPart, 0, Convert.ToInt32(lastPartLen));
}