private static byte[] RSAEncryptData(AsymmetricKeyParameter publicKey, byte[] originalDataBytes) { bool isEncryption = true; byte[] encryptedDataArray; using (MemoryStream originalDataStream = new MemoryStream(originalDataBytes, false)) { using (MemoryStream encryptedDataStream = new MemoryStream()) { IBufferedCipher rsacipher = RSACreateCipher(isEncryption, publicKey); using (CipherStream cipherStream = new CipherStream(originalDataStream, rsacipher, null)) { int oneByte; while ((oneByte = cipherStream.ReadByte()) >= 0) { encryptedDataStream.WriteByte((byte)oneByte); } cipherStream.Close(); cipherStream.Dispose(); } encryptedDataArray = encryptedDataStream.ToArray(); encryptedDataStream.Close(); encryptedDataStream.Dispose(); } originalDataStream.Close(); originalDataStream.Dispose(); } return(encryptedDataArray); }
protected override void Dispose(bool disposing) { if (disposing) { _out.Dispose(); // TODO Parent context(s) should really be be closed explicitly _eiGen.Close(); if (_outer.unprotectedAttributeGenerator != null) { Asn1.Cms.AttributeTable attrTable = _outer.unprotectedAttributeGenerator.GetAttributes(Platform.CreateHashtable()); Asn1Set unprotectedAttrs = new BerSet(attrTable.ToAsn1EncodableVector()); _envGen.AddObject(new DerTaggedObject(false, 1, unprotectedAttrs)); } _envGen.Close(); _cGen.Close(); } base.Dispose(disposing); }
private void Dispose(bool Disposing) { if (!m_isDisposed && Disposing) { try { m_progressTotal = 0; m_volumeKey.Reset(); if (m_cipherStream != null) { m_cipherStream.Dispose(); } } finally { m_isDisposed = true; } } }
protected override void Dispose(bool disposing) { _out.Dispose(); this.CloseParentContext(); base.Dispose(disposing); }
/// <summary> /// Generate an enveloped object that contains a CMS Enveloped Data /// object using the passed in key generator. /// </summary> private CmsEnvelopedData Generate( CmsProcessable content, string encryptionOid, CipherKeyGenerator keyGen) { AlgorithmIdentifier encAlgId = null; KeyParameter encKey; Asn1OctetString encContent; try { byte[] encKeyBytes = keyGen.GenerateKey(); encKey = ParameterUtilities.CreateKeyParameter(encryptionOid, encKeyBytes); Asn1Encodable asn1Params = GenerateAsn1Parameters(encryptionOid, encKeyBytes); ICipherParameters cipherParameters; encAlgId = GetAlgorithmIdentifier( encryptionOid, encKey, asn1Params, out cipherParameters); IBufferedCipher cipher = CipherUtilities.GetCipher(encryptionOid); cipher.Init(true, new ParametersWithRandom(cipherParameters, rand)); MemoryStream bOut = new MemoryStream(); CipherStream cOut = new CipherStream(bOut, null, cipher); content.Write(cOut); cOut.Dispose(); encContent = new BerOctetString(bOut.ToArray()); } catch (SecurityUtilityException e) { throw new CmsException("couldn't create cipher.", e); } catch (InvalidKeyException e) { throw new CmsException("key invalid in message.", e); } catch (IOException e) { throw new CmsException("exception decoding algorithm parameters.", e); } Asn1EncodableVector recipientInfos = new Asn1EncodableVector(); foreach (RecipientInfoGenerator rig in recipientInfoGenerators) { try { recipientInfos.Add(rig.Generate(encKey, rand)); } catch (InvalidKeyException e) { throw new CmsException("key inappropriate for algorithm.", e); } catch (GeneralSecurityException e) { throw new CmsException("error making encrypted content.", e); } } EncryptedContentInfo eci = new EncryptedContentInfo( CmsObjectIdentifiers.Data, encAlgId, encContent); Asn1Set unprotectedAttrSet = null; if (unprotectedAttributeGenerator != null) { Asn1.Cms.AttributeTable attrTable = unprotectedAttributeGenerator.GetAttributes(Platform.CreateHashtable()); unprotectedAttrSet = new BerSet(attrTable.ToAsn1EncodableVector()); } ContentInfo contentInfo = new ContentInfo( CmsObjectIdentifiers.EnvelopedData, new EnvelopedData(null, new DerSet(recipientInfos), eci, unprotectedAttrSet)); return(new CmsEnvelopedData(contentInfo)); }