/// <summary> /// /// </summary> /// <param name="x509_certificate2"></param> /// <param name="encrypted_data"></param> /// <returns></returns> public byte[] GetDecryptedContent(X509Certificate2 x509_certificate2, byte[] encrypted_data) { Org.BouncyCastle.Asn1.Cms.ContentInfo _content = Org.BouncyCastle.Asn1.Cms.ContentInfo.GetInstance(Asn1Sequence.FromByteArray(encrypted_data)); EnvelopedData _envelopedData = EnvelopedData.GetInstance(_content.Content); EncryptedContentInfo _encryptedContentInfo = _envelopedData.EncryptedContentInfo; byte[] _encrypt = _encryptedContentInfo.EncryptedContent.GetOctets(); RecipientInfo _recipientInfo = RecipientInfo.GetInstance(_envelopedData.RecipientInfos[0]); KeyTransRecipientInfo _keyTransRecipientInfo = KeyTransRecipientInfo.GetInstance(_recipientInfo.Info); byte[] _byteEncryptedKey = _keyTransRecipientInfo.EncryptedKey.GetOctets(); RSACryptoServiceProvider _rsaCrypto = (RSACryptoServiceProvider)x509_certificate2.PrivateKey; byte[] _randomKey = _rsaCrypto.Decrypt(_byteEncryptedKey, false); AlgorithmIdentifier _contentEncryptionAlgorithm = _encryptedContentInfo.ContentEncryptionAlgorithm; Asn1OctetString _paramIV = Asn1OctetString.GetInstance(_contentEncryptionAlgorithm.Parameters); byte[] _initVector = _paramIV.GetOctets(); tDESCrypto _cryptoService = new tDESCrypto(_randomKey, _initVector); return(_cryptoService.Decrypt(_encrypt)); }
/// <summary> /// /// </summary> /// <param name="x509_certificate2"></param> /// <param name="plain_data"></param> /// <returns></returns> public byte[] GetEncryptedContent(X509Certificate2 x509_certificate2, byte[] plain_data) { tDESCrypto _cryptoService = new tDESCrypto(); // RecipientInfo 구조체 생성 및 설정 RecipientInfo _recipientInfo = this.GetKeyTransRecipientInfo(x509_certificate2, _cryptoService.Key); // EncryptedContentInfo 구조체 생성 및 설정 DerOctetString _taxInvoce = new DerOctetString(plain_data); byte[] _package = _taxInvoce.GetOctets(); byte[] _encrypt = _cryptoService.Encrypt(_package); // 대칭키로 암호화 EncryptedContentInfo _encryptedContentInfo = this.GetEncryptedContentInfo(_encrypt, _cryptoService.IV); // EnvelopedData 구조체 생성 및 설정 Asn1Set _receipientInfos = new DerSet(_recipientInfo); EnvelopedData _envelopedData = new EnvelopedData((OriginatorInfo)null, _receipientInfos, _encryptedContentInfo, (Asn1Set)null); Org.BouncyCastle.Asn1.Cms.ContentInfo _content = new Org.BouncyCastle.Asn1.Cms.ContentInfo(new DerObjectIdentifier("1.2.840.113549.1.7.3"), _envelopedData); return(_content.GetEncoded()); }
//-------------------------------------------------------------------------------------------------------------------------// // //-------------------------------------------------------------------------------------------------------------------------// /// <summary> /// RFC 3852 CMS 에 정의된 ContentInfo 구조체를 생성한다. /// </summary> /// <param name="x509_certificate2">랜덤키를 암호화하기 위한 공인인증서(국세청 공인인증서)</param> /// <param name="plain_data">데이터</param> /// <returns></returns> public byte[] GetContentInfo(X509Certificate2 x509_certificate2, ArrayList plain_data) { tDESCrypto _cryptoService = new tDESCrypto(); // RecipientInfo 구조체 생성 및 설정 RecipientInfo _recipientInfo = this.GetKeyTransRecipientInfo(x509_certificate2, _cryptoService.Key); // EncryptedContentInfo 구조체 생성 및 설정 byte[] _package = this.GetTaxInvoicePackage(plain_data); byte[] _encrypt = _cryptoService.Encrypt(_package); // 대칭키로 암호화 EncryptedContentInfo _encryptedContentInfo = this.GetEncryptedContentInfo(_encrypt, _cryptoService.IV); // EnvelopedData 구조체 생성 및 설정 Asn1Set _asn1Set = new DerSet(_recipientInfo); EnvelopedData _envelope = new EnvelopedData((OriginatorInfo)null, _asn1Set, _encryptedContentInfo, (Asn1Set)null); // RFC 3852의 구성 데이터인 SignedData, EnvelopedData, EncryptedData 등을 넣어주는 컨테이너인 ContentInfo 구조체를 생성 및 설정한다. // ContentInfo 구조체는 표준전자세금계산서 개발지침(v1.0)의 58페이지 참조 Org.BouncyCastle.Asn1.Cms.ContentInfo _content = new Org.BouncyCastle.Asn1.Cms.ContentInfo(new DerObjectIdentifier("1.2.840.113549.1.7.3"), _envelope); return(_content.GetEncoded()); }