CipherKeyGeneratorFactory.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1;
  5. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.Kisa;
  6. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.Nist;
  7. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.Ntt;
  8. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.Oiw;
  9. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.Pkcs;
  10. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Generators;
  11. using BestHTTP.SecureProtocol.Org.BouncyCastle.Security;
  12. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Utilities
  13. {
  14. public class CipherKeyGeneratorFactory
  15. {
  16. private CipherKeyGeneratorFactory()
  17. {
  18. }
  19. /**
  20. * Create a key generator for the passed in Object Identifier.
  21. *
  22. * @param algorithm the Object Identifier indicating the algorithn the generator is for.
  23. * @param random a source of random to initialise the generator with.
  24. * @return an initialised CipherKeyGenerator.
  25. * @throws IllegalArgumentException if the algorithm cannot be identified.
  26. */
  27. public static CipherKeyGenerator CreateKeyGenerator(DerObjectIdentifier algorithm, SecureRandom random)
  28. {
  29. if (NistObjectIdentifiers.IdAes128Cbc.Equals(algorithm))
  30. {
  31. return CreateCipherKeyGenerator(random, 128);
  32. }
  33. else if (NistObjectIdentifiers.IdAes192Cbc.Equals(algorithm))
  34. {
  35. return CreateCipherKeyGenerator(random, 192);
  36. }
  37. else if (NistObjectIdentifiers.IdAes256Cbc.Equals(algorithm))
  38. {
  39. return CreateCipherKeyGenerator(random, 256);
  40. }
  41. else if (PkcsObjectIdentifiers.DesEde3Cbc.Equals(algorithm))
  42. {
  43. DesEdeKeyGenerator keyGen = new DesEdeKeyGenerator();
  44. keyGen.Init(new KeyGenerationParameters(random, 192));
  45. return keyGen;
  46. }
  47. else if (NttObjectIdentifiers.IdCamellia128Cbc.Equals(algorithm))
  48. {
  49. return CreateCipherKeyGenerator(random, 128);
  50. }
  51. else if (NttObjectIdentifiers.IdCamellia192Cbc.Equals(algorithm))
  52. {
  53. return CreateCipherKeyGenerator(random, 192);
  54. }
  55. else if (NttObjectIdentifiers.IdCamellia256Cbc.Equals(algorithm))
  56. {
  57. return CreateCipherKeyGenerator(random, 256);
  58. }
  59. else if (KisaObjectIdentifiers.IdSeedCbc.Equals(algorithm))
  60. {
  61. return CreateCipherKeyGenerator(random, 128);
  62. }
  63. else if (AlgorithmIdentifierFactory.CAST5_CBC.Equals(algorithm))
  64. {
  65. return CreateCipherKeyGenerator(random, 128);
  66. }
  67. else if (OiwObjectIdentifiers.DesCbc.Equals(algorithm))
  68. {
  69. DesKeyGenerator keyGen = new DesKeyGenerator();
  70. keyGen.Init(new KeyGenerationParameters(random, 64));
  71. return keyGen;
  72. }
  73. else if (PkcsObjectIdentifiers.rc4.Equals(algorithm))
  74. {
  75. return CreateCipherKeyGenerator(random, 128);
  76. }
  77. else if (PkcsObjectIdentifiers.RC2Cbc.Equals(algorithm))
  78. {
  79. return CreateCipherKeyGenerator(random, 128);
  80. }
  81. else
  82. {
  83. throw new InvalidOperationException("cannot recognise cipher: " + algorithm);
  84. }
  85. }
  86. private static CipherKeyGenerator CreateCipherKeyGenerator(SecureRandom random, int keySize)
  87. {
  88. CipherKeyGenerator keyGen = new CipherKeyGenerator();
  89. keyGen.Init(new KeyGenerationParameters(random, keySize));
  90. return keyGen;
  91. }
  92. }
  93. }
  94. #pragma warning restore
  95. #endif