Asn1CipherBuilder.cs 3.2 KB

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