EncryptedPrivateKeyInfoFactory.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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.Pkcs;
  6. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.X509;
  7. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto;
  8. using BestHTTP.SecureProtocol.Org.BouncyCastle.Security;
  9. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Pkcs
  10. {
  11. public sealed class EncryptedPrivateKeyInfoFactory
  12. {
  13. private EncryptedPrivateKeyInfoFactory()
  14. {
  15. }
  16. public static EncryptedPrivateKeyInfo CreateEncryptedPrivateKeyInfo(
  17. DerObjectIdentifier algorithm,
  18. char[] passPhrase,
  19. byte[] salt,
  20. int iterationCount,
  21. AsymmetricKeyParameter key)
  22. {
  23. return CreateEncryptedPrivateKeyInfo(
  24. algorithm.Id, passPhrase, salt, iterationCount,
  25. PrivateKeyInfoFactory.CreatePrivateKeyInfo(key));
  26. }
  27. public static EncryptedPrivateKeyInfo CreateEncryptedPrivateKeyInfo(
  28. string algorithm,
  29. char[] passPhrase,
  30. byte[] salt,
  31. int iterationCount,
  32. AsymmetricKeyParameter key)
  33. {
  34. return CreateEncryptedPrivateKeyInfo(
  35. algorithm, passPhrase, salt, iterationCount,
  36. PrivateKeyInfoFactory.CreatePrivateKeyInfo(key));
  37. }
  38. public static EncryptedPrivateKeyInfo CreateEncryptedPrivateKeyInfo(
  39. string algorithm,
  40. char[] passPhrase,
  41. byte[] salt,
  42. int iterationCount,
  43. PrivateKeyInfo keyInfo)
  44. {
  45. IBufferedCipher cipher = PbeUtilities.CreateEngine(algorithm) as IBufferedCipher;
  46. if (cipher == null)
  47. throw new Exception("Unknown encryption algorithm: " + algorithm);
  48. Asn1Encodable pbeParameters = PbeUtilities.GenerateAlgorithmParameters(
  49. algorithm, salt, iterationCount);
  50. ICipherParameters cipherParameters = PbeUtilities.GenerateCipherParameters(
  51. algorithm, passPhrase, pbeParameters);
  52. cipher.Init(true, cipherParameters);
  53. byte[] encoding = cipher.DoFinal(keyInfo.GetEncoded());
  54. DerObjectIdentifier oid = PbeUtilities.GetObjectIdentifier(algorithm);
  55. AlgorithmIdentifier algID = new AlgorithmIdentifier(oid, pbeParameters);
  56. return new EncryptedPrivateKeyInfo(algID, encoding);
  57. }
  58. public static EncryptedPrivateKeyInfo CreateEncryptedPrivateKeyInfo(
  59. DerObjectIdentifier cipherAlgorithm,
  60. DerObjectIdentifier prfAlgorithm,
  61. char[] passPhrase,
  62. byte[] salt,
  63. int iterationCount,
  64. SecureRandom random,
  65. AsymmetricKeyParameter key)
  66. {
  67. return CreateEncryptedPrivateKeyInfo(
  68. cipherAlgorithm, prfAlgorithm, passPhrase, salt, iterationCount, random,
  69. PrivateKeyInfoFactory.CreatePrivateKeyInfo(key));
  70. }
  71. public static EncryptedPrivateKeyInfo CreateEncryptedPrivateKeyInfo(
  72. DerObjectIdentifier cipherAlgorithm,
  73. DerObjectIdentifier prfAlgorithm,
  74. char[] passPhrase,
  75. byte[] salt,
  76. int iterationCount,
  77. SecureRandom random,
  78. PrivateKeyInfo keyInfo)
  79. {
  80. IBufferedCipher cipher = CipherUtilities.GetCipher(cipherAlgorithm) as IBufferedCipher;
  81. if (cipher == null)
  82. throw new Exception("Unknown encryption algorithm: " + cipherAlgorithm);
  83. Asn1Encodable pbeParameters = PbeUtilities.GenerateAlgorithmParameters(
  84. cipherAlgorithm, prfAlgorithm, salt, iterationCount, random);
  85. ICipherParameters cipherParameters = PbeUtilities.GenerateCipherParameters(
  86. PkcsObjectIdentifiers.IdPbeS2, passPhrase, pbeParameters);
  87. cipher.Init(true, cipherParameters);
  88. byte[] encoding = cipher.DoFinal(keyInfo.GetEncoded());
  89. AlgorithmIdentifier algID = new AlgorithmIdentifier(PkcsObjectIdentifiers.IdPbeS2, pbeParameters);
  90. return new EncryptedPrivateKeyInfo(algID, encoding);
  91. }
  92. }
  93. }
  94. #pragma warning restore
  95. #endif