CipherKeyGeneratorFactory.cs 3.8 KB

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