PkzipClassic.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. using System;
  2. using System.Security.Cryptography;
  3. using ICSharpCode.SharpZipLib.Checksum;
  4. namespace ICSharpCode.SharpZipLib.Encryption
  5. {
  6. /// <summary>
  7. /// PkzipClassic embodies the classic or original encryption facilities used in Pkzip archives.
  8. /// While it has been superceded by more recent and more powerful algorithms, its still in use and
  9. /// is viable for preventing casual snooping
  10. /// </summary>
  11. public abstract class PkzipClassic : SymmetricAlgorithm
  12. {
  13. /// <summary>
  14. /// Generates new encryption keys based on given seed
  15. /// </summary>
  16. /// <param name="seed">The seed value to initialise keys with.</param>
  17. /// <returns>A new key value.</returns>
  18. static public byte[] GenerateKeys(byte[] seed)
  19. {
  20. if (seed == null) {
  21. throw new ArgumentNullException("nameof(seed)");
  22. }
  23. if (seed.Length == 0) {
  24. throw new ArgumentException("Length is zero", "nameof(seed)");
  25. }
  26. uint[] newKeys = {
  27. 0x12345678,
  28. 0x23456789,
  29. 0x34567890
  30. };
  31. for (int i = 0; i < seed.Length; ++i) {
  32. newKeys[0] = Crc32.ComputeCrc32(newKeys[0], seed[i]);
  33. newKeys[1] = newKeys[1] + (byte)newKeys[0];
  34. newKeys[1] = newKeys[1] * 134775813 + 1;
  35. newKeys[2] = Crc32.ComputeCrc32(newKeys[2], (byte)(newKeys[1] >> 24));
  36. }
  37. byte[] result = new byte[12];
  38. result[0] = (byte)(newKeys[0] & 0xff);
  39. result[1] = (byte)((newKeys[0] >> 8) & 0xff);
  40. result[2] = (byte)((newKeys[0] >> 16) & 0xff);
  41. result[3] = (byte)((newKeys[0] >> 24) & 0xff);
  42. result[4] = (byte)(newKeys[1] & 0xff);
  43. result[5] = (byte)((newKeys[1] >> 8) & 0xff);
  44. result[6] = (byte)((newKeys[1] >> 16) & 0xff);
  45. result[7] = (byte)((newKeys[1] >> 24) & 0xff);
  46. result[8] = (byte)(newKeys[2] & 0xff);
  47. result[9] = (byte)((newKeys[2] >> 8) & 0xff);
  48. result[10] = (byte)((newKeys[2] >> 16) & 0xff);
  49. result[11] = (byte)((newKeys[2] >> 24) & 0xff);
  50. return result;
  51. }
  52. }
  53. /// <summary>
  54. /// PkzipClassicCryptoBase provides the low level facilities for encryption
  55. /// and decryption using the PkzipClassic algorithm.
  56. /// </summary>
  57. class PkzipClassicCryptoBase
  58. {
  59. /// <summary>
  60. /// Transform a single byte
  61. /// </summary>
  62. /// <returns>
  63. /// The transformed value
  64. /// </returns>
  65. protected byte TransformByte()
  66. {
  67. uint temp = ((keys[2] & 0xFFFF) | 2);
  68. return (byte)((temp * (temp ^ 1)) >> 8);
  69. }
  70. /// <summary>
  71. /// Set the key schedule for encryption/decryption.
  72. /// </summary>
  73. /// <param name="keyData">The data use to set the keys from.</param>
  74. protected void SetKeys(byte[] keyData)
  75. {
  76. if (keyData == null) {
  77. throw new ArgumentNullException("nameof(keyData)");
  78. }
  79. if (keyData.Length != 12) {
  80. throw new InvalidOperationException("Key length is not valid");
  81. }
  82. keys = new uint[3];
  83. keys[0] = (uint)((keyData[3] << 24) | (keyData[2] << 16) | (keyData[1] << 8) | keyData[0]);
  84. keys[1] = (uint)((keyData[7] << 24) | (keyData[6] << 16) | (keyData[5] << 8) | keyData[4]);
  85. keys[2] = (uint)((keyData[11] << 24) | (keyData[10] << 16) | (keyData[9] << 8) | keyData[8]);
  86. }
  87. /// <summary>
  88. /// Update encryption keys
  89. /// </summary>
  90. protected void UpdateKeys(byte ch)
  91. {
  92. keys[0] = Crc32.ComputeCrc32(keys[0], ch);
  93. keys[1] = keys[1] + (byte)keys[0];
  94. keys[1] = keys[1] * 134775813 + 1;
  95. keys[2] = Crc32.ComputeCrc32(keys[2], (byte)(keys[1] >> 24));
  96. }
  97. /// <summary>
  98. /// Reset the internal state.
  99. /// </summary>
  100. protected void Reset()
  101. {
  102. keys[0] = 0;
  103. keys[1] = 0;
  104. keys[2] = 0;
  105. }
  106. #region Instance Fields
  107. uint[] keys;
  108. #endregion
  109. }
  110. /// <summary>
  111. /// PkzipClassic CryptoTransform for encryption.
  112. /// </summary>
  113. class PkzipClassicEncryptCryptoTransform : PkzipClassicCryptoBase, ICryptoTransform
  114. {
  115. /// <summary>
  116. /// Initialise a new instance of <see cref="PkzipClassicEncryptCryptoTransform"></see>
  117. /// </summary>
  118. /// <param name="keyBlock">The key block to use.</param>
  119. internal PkzipClassicEncryptCryptoTransform(byte[] keyBlock)
  120. {
  121. SetKeys(keyBlock);
  122. }
  123. #region ICryptoTransform Members
  124. /// <summary>
  125. /// Transforms the specified region of the specified byte array.
  126. /// </summary>
  127. /// <param name="inputBuffer">The input for which to compute the transform.</param>
  128. /// <param name="inputOffset">The offset into the byte array from which to begin using data.</param>
  129. /// <param name="inputCount">The number of bytes in the byte array to use as data.</param>
  130. /// <returns>The computed transform.</returns>
  131. public byte[] TransformFinalBlock(byte[] inputBuffer, int inputOffset, int inputCount)
  132. {
  133. byte[] result = new byte[inputCount];
  134. TransformBlock(inputBuffer, inputOffset, inputCount, result, 0);
  135. return result;
  136. }
  137. /// <summary>
  138. /// Transforms the specified region of the input byte array and copies
  139. /// the resulting transform to the specified region of the output byte array.
  140. /// </summary>
  141. /// <param name="inputBuffer">The input for which to compute the transform.</param>
  142. /// <param name="inputOffset">The offset into the input byte array from which to begin using data.</param>
  143. /// <param name="inputCount">The number of bytes in the input byte array to use as data.</param>
  144. /// <param name="outputBuffer">The output to which to write the transform.</param>
  145. /// <param name="outputOffset">The offset into the output byte array from which to begin writing data.</param>
  146. /// <returns>The number of bytes written.</returns>
  147. public int TransformBlock(byte[] inputBuffer, int inputOffset, int inputCount, byte[] outputBuffer, int outputOffset)
  148. {
  149. for (int i = inputOffset; i < inputOffset + inputCount; ++i) {
  150. byte oldbyte = inputBuffer[i];
  151. outputBuffer[outputOffset++] = (byte)(inputBuffer[i] ^ TransformByte());
  152. UpdateKeys(oldbyte);
  153. }
  154. return inputCount;
  155. }
  156. /// <summary>
  157. /// Gets a value indicating whether the current transform can be reused.
  158. /// </summary>
  159. public bool CanReuseTransform {
  160. get {
  161. return true;
  162. }
  163. }
  164. /// <summary>
  165. /// Gets the size of the input data blocks in bytes.
  166. /// </summary>
  167. public int InputBlockSize {
  168. get {
  169. return 1;
  170. }
  171. }
  172. /// <summary>
  173. /// Gets the size of the output data blocks in bytes.
  174. /// </summary>
  175. public int OutputBlockSize {
  176. get {
  177. return 1;
  178. }
  179. }
  180. /// <summary>
  181. /// Gets a value indicating whether multiple blocks can be transformed.
  182. /// </summary>
  183. public bool CanTransformMultipleBlocks {
  184. get {
  185. return true;
  186. }
  187. }
  188. #endregion
  189. #region IDisposable Members
  190. /// <summary>
  191. /// Cleanup internal state.
  192. /// </summary>
  193. public void Dispose()
  194. {
  195. Reset();
  196. }
  197. #endregion
  198. }
  199. /// <summary>
  200. /// PkzipClassic CryptoTransform for decryption.
  201. /// </summary>
  202. class PkzipClassicDecryptCryptoTransform : PkzipClassicCryptoBase, ICryptoTransform
  203. {
  204. /// <summary>
  205. /// Initialise a new instance of <see cref="PkzipClassicDecryptCryptoTransform"></see>.
  206. /// </summary>
  207. /// <param name="keyBlock">The key block to decrypt with.</param>
  208. internal PkzipClassicDecryptCryptoTransform(byte[] keyBlock)
  209. {
  210. SetKeys(keyBlock);
  211. }
  212. #region ICryptoTransform Members
  213. /// <summary>
  214. /// Transforms the specified region of the specified byte array.
  215. /// </summary>
  216. /// <param name="inputBuffer">The input for which to compute the transform.</param>
  217. /// <param name="inputOffset">The offset into the byte array from which to begin using data.</param>
  218. /// <param name="inputCount">The number of bytes in the byte array to use as data.</param>
  219. /// <returns>The computed transform.</returns>
  220. public byte[] TransformFinalBlock(byte[] inputBuffer, int inputOffset, int inputCount)
  221. {
  222. byte[] result = new byte[inputCount];
  223. TransformBlock(inputBuffer, inputOffset, inputCount, result, 0);
  224. return result;
  225. }
  226. /// <summary>
  227. /// Transforms the specified region of the input byte array and copies
  228. /// the resulting transform to the specified region of the output byte array.
  229. /// </summary>
  230. /// <param name="inputBuffer">The input for which to compute the transform.</param>
  231. /// <param name="inputOffset">The offset into the input byte array from which to begin using data.</param>
  232. /// <param name="inputCount">The number of bytes in the input byte array to use as data.</param>
  233. /// <param name="outputBuffer">The output to which to write the transform.</param>
  234. /// <param name="outputOffset">The offset into the output byte array from which to begin writing data.</param>
  235. /// <returns>The number of bytes written.</returns>
  236. public int TransformBlock(byte[] inputBuffer, int inputOffset, int inputCount, byte[] outputBuffer, int outputOffset)
  237. {
  238. for (int i = inputOffset; i < inputOffset + inputCount; ++i) {
  239. var newByte = (byte)(inputBuffer[i] ^ TransformByte());
  240. outputBuffer[outputOffset++] = newByte;
  241. UpdateKeys(newByte);
  242. }
  243. return inputCount;
  244. }
  245. /// <summary>
  246. /// Gets a value indicating whether the current transform can be reused.
  247. /// </summary>
  248. public bool CanReuseTransform {
  249. get {
  250. return true;
  251. }
  252. }
  253. /// <summary>
  254. /// Gets the size of the input data blocks in bytes.
  255. /// </summary>
  256. public int InputBlockSize {
  257. get {
  258. return 1;
  259. }
  260. }
  261. /// <summary>
  262. /// Gets the size of the output data blocks in bytes.
  263. /// </summary>
  264. public int OutputBlockSize {
  265. get {
  266. return 1;
  267. }
  268. }
  269. /// <summary>
  270. /// Gets a value indicating whether multiple blocks can be transformed.
  271. /// </summary>
  272. public bool CanTransformMultipleBlocks {
  273. get {
  274. return true;
  275. }
  276. }
  277. #endregion
  278. #region IDisposable Members
  279. /// <summary>
  280. /// Cleanup internal state.
  281. /// </summary>
  282. public void Dispose()
  283. {
  284. Reset();
  285. }
  286. #endregion
  287. }
  288. /// <summary>
  289. /// Defines a wrapper object to access the Pkzip algorithm.
  290. /// This class cannot be inherited.
  291. /// </summary>
  292. public sealed class PkzipClassicManaged : PkzipClassic
  293. {
  294. /// <summary>
  295. /// Get / set the applicable block size in bits.
  296. /// </summary>
  297. /// <remarks>The only valid block size is 8.</remarks>
  298. public override int BlockSize {
  299. get {
  300. return 8;
  301. }
  302. set {
  303. if (value != 8) {
  304. throw new CryptographicException("Block size is invalid");
  305. }
  306. }
  307. }
  308. /// <summary>
  309. /// Get an array of legal <see cref="KeySizes">key sizes.</see>
  310. /// </summary>
  311. public override KeySizes[] LegalKeySizes {
  312. get {
  313. KeySizes[] keySizes = new KeySizes[1];
  314. keySizes[0] = new KeySizes(12 * 8, 12 * 8, 0);
  315. return keySizes;
  316. }
  317. }
  318. /// <summary>
  319. /// Generate an initial vector.
  320. /// </summary>
  321. public override void GenerateIV()
  322. {
  323. // Do nothing.
  324. }
  325. /// <summary>
  326. /// Get an array of legal <see cref="KeySizes">block sizes</see>.
  327. /// </summary>
  328. public override KeySizes[] LegalBlockSizes {
  329. get {
  330. KeySizes[] keySizes = new KeySizes[1];
  331. keySizes[0] = new KeySizes(1 * 8, 1 * 8, 0);
  332. return keySizes;
  333. }
  334. }
  335. /// <summary>
  336. /// Get / set the key value applicable.
  337. /// </summary>
  338. public override byte[] Key {
  339. get {
  340. if (key_ == null) {
  341. GenerateKey();
  342. }
  343. return (byte[])key_.Clone();
  344. }
  345. set {
  346. if (value == null) {
  347. throw new ArgumentNullException("nameof(value)");
  348. }
  349. if (value.Length != 12) {
  350. throw new CryptographicException("Key size is illegal");
  351. }
  352. key_ = (byte[])value.Clone();
  353. }
  354. }
  355. /// <summary>
  356. /// Generate a new random key.
  357. /// </summary>
  358. public override void GenerateKey()
  359. {
  360. key_ = new byte[12];
  361. var rnd = new Random();
  362. rnd.NextBytes(key_);
  363. }
  364. /// <summary>
  365. /// Create an encryptor.
  366. /// </summary>
  367. /// <param name="rgbKey">The key to use for this encryptor.</param>
  368. /// <param name="rgbIV">Initialisation vector for the new encryptor.</param>
  369. /// <returns>Returns a new PkzipClassic encryptor</returns>
  370. public override ICryptoTransform CreateEncryptor(
  371. byte[] rgbKey,
  372. byte[] rgbIV)
  373. {
  374. key_ = rgbKey;
  375. return new PkzipClassicEncryptCryptoTransform(Key);
  376. }
  377. /// <summary>
  378. /// Create a decryptor.
  379. /// </summary>
  380. /// <param name="rgbKey">Keys to use for this new decryptor.</param>
  381. /// <param name="rgbIV">Initialisation vector for the new decryptor.</param>
  382. /// <returns>Returns a new decryptor.</returns>
  383. public override ICryptoTransform CreateDecryptor(
  384. byte[] rgbKey,
  385. byte[] rgbIV)
  386. {
  387. key_ = rgbKey;
  388. return new PkzipClassicDecryptCryptoTransform(Key);
  389. }
  390. #region Instance Fields
  391. byte[] key_;
  392. #endregion
  393. }
  394. }