RarCryptoWrapper.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #if !NO_CRYPTO
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. namespace SharpCompress.Common.Rar
  6. {
  7. internal class RarCryptoWrapper : Stream
  8. {
  9. private readonly Stream _actualStream;
  10. private readonly byte[] _salt;
  11. private RarRijndael _rijndael;
  12. private readonly Queue<byte> _data = new Queue<byte>();
  13. public RarCryptoWrapper(Stream actualStream, string password, byte[] salt)
  14. {
  15. _actualStream = actualStream;
  16. _salt = salt;
  17. _rijndael = RarRijndael.InitializeFrom(password, salt);
  18. }
  19. public override void Flush()
  20. {
  21. throw new NotSupportedException();
  22. }
  23. public override long Seek(long offset, SeekOrigin origin)
  24. {
  25. throw new NotSupportedException();
  26. }
  27. public override void SetLength(long value)
  28. {
  29. throw new NotSupportedException();
  30. }
  31. public override int Read(byte[] buffer, int offset, int count)
  32. {
  33. if (_salt == null)
  34. {
  35. return _actualStream.Read(buffer, offset, count);
  36. }
  37. return ReadAndDecrypt(buffer, offset, count);
  38. }
  39. public int ReadAndDecrypt(byte[] buffer, int offset, int count)
  40. {
  41. int queueSize = _data.Count;
  42. int sizeToRead = count - queueSize;
  43. if (sizeToRead > 0)
  44. {
  45. int alignedSize = sizeToRead + ((~sizeToRead + 1) & 0xf);
  46. for (int i = 0; i < alignedSize / 16; i++)
  47. {
  48. //long ax = System.currentTimeMillis();
  49. byte[] cipherText = new byte[RarRijndael.CRYPTO_BLOCK_SIZE];
  50. _actualStream.Read(cipherText, 0, RarRijndael.CRYPTO_BLOCK_SIZE);
  51. var readBytes = _rijndael.ProcessBlock(cipherText);
  52. foreach (var readByte in readBytes)
  53. _data.Enqueue(readByte);
  54. }
  55. for (int i = 0; i < count; i++)
  56. buffer[offset + i] = _data.Dequeue();
  57. }
  58. return count;
  59. }
  60. public override void Write(byte[] buffer, int offset, int count)
  61. {
  62. throw new NotSupportedException();
  63. }
  64. public override bool CanRead => true;
  65. public override bool CanSeek => false;
  66. public override bool CanWrite => false;
  67. public override long Length => throw new NotSupportedException();
  68. public override long Position { get; set; }
  69. protected override void Dispose(bool disposing)
  70. {
  71. if (_rijndael != null)
  72. {
  73. _rijndael.Dispose();
  74. _rijndael = null;
  75. }
  76. base.Dispose(disposing);
  77. }
  78. }
  79. }
  80. #endif