Asn1CipherBuilder.cs 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using System.IO;
  5. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Asn1;
  6. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Asn1.X509;
  7. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Cms;
  8. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto.IO;
  9. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto.Parameters;
  10. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto.Utilities;
  11. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Security;
  12. namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto.Operators
  13. {
  14. public class Asn1CipherBuilderWithKey : ICipherBuilderWithKey
  15. {
  16. private readonly KeyParameter encKey;
  17. private AlgorithmIdentifier algorithmIdentifier;
  18. public Asn1CipherBuilderWithKey(DerObjectIdentifier encryptionOID, int keySize, SecureRandom random)
  19. {
  20. random = CryptoServicesRegistrar.GetSecureRandom(random);
  21. CipherKeyGenerator keyGen = CipherKeyGeneratorFactory.CreateKeyGenerator(encryptionOID, random);
  22. encKey = new KeyParameter(keyGen.GenerateKey());
  23. algorithmIdentifier = AlgorithmIdentifierFactory.GenerateEncryptionAlgID(encryptionOID, encKey.GetKey().Length * 8, random);
  24. }
  25. public object AlgorithmDetails
  26. {
  27. get { return algorithmIdentifier; }
  28. }
  29. public int GetMaxOutputSize(int inputLen)
  30. {
  31. throw new NotImplementedException();
  32. }
  33. public ICipher BuildCipher(Stream stream)
  34. {
  35. object cipher = EnvelopedDataHelper.CreateContentCipher(true, encKey, algorithmIdentifier);
  36. //
  37. // BufferedBlockCipher
  38. // IStreamCipher
  39. //
  40. if (cipher is IStreamCipher)
  41. {
  42. cipher = new BufferedStreamCipher((IStreamCipher)cipher);
  43. }
  44. if (stream == null)
  45. {
  46. stream = new MemoryStream();
  47. }
  48. return new BufferedCipherWrapper((IBufferedCipher)cipher, stream);
  49. }
  50. public ICipherParameters Key
  51. {
  52. get { return encKey; }
  53. }
  54. }
  55. public class BufferedCipherWrapper : ICipher
  56. {
  57. private readonly IBufferedCipher bufferedCipher;
  58. private readonly CipherStream stream;
  59. public BufferedCipherWrapper(IBufferedCipher bufferedCipher, Stream source)
  60. {
  61. this.bufferedCipher = bufferedCipher;
  62. stream = new CipherStream(source, bufferedCipher, bufferedCipher);
  63. }
  64. public int GetMaxOutputSize(int inputLen)
  65. {
  66. return bufferedCipher.GetOutputSize(inputLen);
  67. }
  68. public int GetUpdateOutputSize(int inputLen)
  69. {
  70. return bufferedCipher.GetUpdateOutputSize(inputLen);
  71. }
  72. public Stream Stream
  73. {
  74. get { return stream; }
  75. }
  76. }
  77. }
  78. #pragma warning restore
  79. #endif