RsaEngine.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using BestHTTP.SecureProtocol.Org.BouncyCastle.Math;
  5. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Engines
  6. {
  7. /**
  8. * this does your basic RSA algorithm.
  9. */
  10. public class RsaEngine
  11. : IAsymmetricBlockCipher
  12. {
  13. private readonly IRsa core;
  14. public RsaEngine()
  15. : this(new RsaCoreEngine())
  16. {
  17. }
  18. public RsaEngine(IRsa rsa)
  19. {
  20. this.core = rsa;
  21. }
  22. public virtual string AlgorithmName
  23. {
  24. get { return "RSA"; }
  25. }
  26. /**
  27. * initialise the RSA engine.
  28. *
  29. * @param forEncryption true if we are encrypting, false otherwise.
  30. * @param param the necessary RSA key parameters.
  31. */
  32. public virtual void Init(
  33. bool forEncryption,
  34. ICipherParameters parameters)
  35. {
  36. core.Init(forEncryption, parameters);
  37. }
  38. /**
  39. * Return the maximum size for an input block to this engine.
  40. * For RSA this is always one byte less than the key size on
  41. * encryption, and the same length as the key size on decryption.
  42. *
  43. * @return maximum size for an input block.
  44. */
  45. public virtual int GetInputBlockSize()
  46. {
  47. return core.GetInputBlockSize();
  48. }
  49. /**
  50. * Return the maximum size for an output block to this engine.
  51. * For RSA this is always one byte less than the key size on
  52. * decryption, and the same length as the key size on encryption.
  53. *
  54. * @return maximum size for an output block.
  55. */
  56. public virtual int GetOutputBlockSize()
  57. {
  58. return core.GetOutputBlockSize();
  59. }
  60. /**
  61. * Process a single block using the basic RSA algorithm.
  62. *
  63. * @param inBuf the input array.
  64. * @param inOff the offset into the input buffer where the data starts.
  65. * @param inLen the length of the data to be processed.
  66. * @return the result of the RSA process.
  67. * @exception DataLengthException the input block is too large.
  68. */
  69. public virtual byte[] ProcessBlock(
  70. byte[] inBuf,
  71. int inOff,
  72. int inLen)
  73. {
  74. BigInteger input = core.ConvertInput(inBuf, inOff, inLen);
  75. BigInteger output = core.ProcessBlock(input);
  76. return core.ConvertOutput(output);
  77. }
  78. }
  79. }
  80. #pragma warning restore
  81. #endif