PkzipClassic.cs 12 KB

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