PkzipClassic.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  1. //
  2. // PkzipClassic encryption
  3. //
  4. // Copyright 2004 John Reilly
  5. //
  6. // This program is free software; you can redistribute it and/or
  7. // modify it under the terms of the GNU General Public License
  8. // as published by the Free Software Foundation; either version 2
  9. // of the License, or (at your option) any later version.
  10. //
  11. // This program is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. // GNU General Public License for more details.
  15. //
  16. // You should have received a copy of the GNU General Public License
  17. // along with this program; if not, write to the Free Software
  18. // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  19. //
  20. // Linking this library statically or dynamically with other modules is
  21. // making a combined work based on this library. Thus, the terms and
  22. // conditions of the GNU General Public License cover the whole
  23. // combination.
  24. //
  25. // As a special exception, the copyright holders of this library give you
  26. // permission to link this library with independent modules to produce an
  27. // executable, regardless of the license terms of these independent
  28. // modules, and to copy and distribute the resulting executable under
  29. // terms of your choice, provided that you also meet, for each linked
  30. // independent module, the terms and conditions of the license of that
  31. // module. An independent module is a module which is not derived from
  32. // or based on this library. If you modify this library, you may extend
  33. // this exception to your version of the library, but you are not
  34. // obligated to do so. If you do not wish to do so, delete this
  35. // exception statement from your version.
  36. //
  37. #if !NETCF_1_0
  38. using System;
  39. using System.Security.Cryptography;
  40. using CommonMPQ.SharpZipLib.Checksums;
  41. namespace CommonMPQ.SharpZipLib.Encryption
  42. {
  43. /// <summary>
  44. /// PkzipClassic embodies the classic or original encryption facilities used in Pkzip archives.
  45. /// While it has been superceded by more recent and more powerful algorithms, its still in use and
  46. /// is viable for preventing casual snooping
  47. /// </summary>
  48. public abstract class PkzipClassic : SymmetricAlgorithm
  49. {
  50. /// <summary>
  51. /// Generates new encryption keys based on given seed
  52. /// </summary>
  53. /// <param name="seed">The seed value to initialise keys with.</param>
  54. /// <returns>A new key value.</returns>
  55. static public byte[] GenerateKeys(byte[] seed)
  56. {
  57. if ( seed == null ) {
  58. throw new ArgumentNullException("seed");
  59. }
  60. if ( seed.Length == 0 ) {
  61. throw new ArgumentException("Length is zero", "seed");
  62. }
  63. uint[] newKeys = new uint[] {
  64. 0x12345678,
  65. 0x23456789,
  66. 0x34567890
  67. };
  68. for (int i = 0; i < seed.Length; ++i) {
  69. newKeys[0] = Crc32.ComputeCrc32(newKeys[0], seed[i]);
  70. newKeys[1] = newKeys[1] + (byte)newKeys[0];
  71. newKeys[1] = newKeys[1] * 134775813 + 1;
  72. newKeys[2] = Crc32.ComputeCrc32(newKeys[2], (byte)(newKeys[1] >> 24));
  73. }
  74. byte[] result = new byte[12];
  75. result[0] = (byte)(newKeys[0] & 0xff);
  76. result[1] = (byte)((newKeys[0] >> 8) & 0xff);
  77. result[2] = (byte)((newKeys[0] >> 16) & 0xff);
  78. result[3] = (byte)((newKeys[0] >> 24) & 0xff);
  79. result[4] = (byte)(newKeys[1] & 0xff);
  80. result[5] = (byte)((newKeys[1] >> 8) & 0xff);
  81. result[6] = (byte)((newKeys[1] >> 16) & 0xff);
  82. result[7] = (byte)((newKeys[1] >> 24) & 0xff);
  83. result[8] = (byte)(newKeys[2] & 0xff);
  84. result[9] = (byte)((newKeys[2] >> 8) & 0xff);
  85. result[10] = (byte)((newKeys[2] >> 16) & 0xff);
  86. result[11] = (byte)((newKeys[2] >> 24) & 0xff);
  87. return result;
  88. }
  89. }
  90. /// <summary>
  91. /// PkzipClassicCryptoBase provides the low level facilities for encryption
  92. /// and decryption using the PkzipClassic algorithm.
  93. /// </summary>
  94. class PkzipClassicCryptoBase
  95. {
  96. /// <summary>
  97. /// Transform a single byte
  98. /// </summary>
  99. /// <returns>
  100. /// The transformed value
  101. /// </returns>
  102. protected byte TransformByte()
  103. {
  104. uint temp = ((keys[2] & 0xFFFF) | 2);
  105. return (byte)((temp * (temp ^ 1)) >> 8);
  106. }
  107. /// <summary>
  108. /// Set the key schedule for encryption/decryption.
  109. /// </summary>
  110. /// <param name="keyData">The data use to set the keys from.</param>
  111. protected void SetKeys(byte[] keyData)
  112. {
  113. if ( keyData == null ) {
  114. throw new ArgumentNullException("keyData");
  115. }
  116. if ( keyData.Length != 12 ) {
  117. throw new InvalidOperationException("Key length is not valid");
  118. }
  119. keys = new uint[3];
  120. keys[0] = (uint)((keyData[3] << 24) | (keyData[2] << 16) | (keyData[1] << 8) | keyData[0]);
  121. keys[1] = (uint)((keyData[7] << 24) | (keyData[6] << 16) | (keyData[5] << 8) | keyData[4]);
  122. keys[2] = (uint)((keyData[11] << 24) | (keyData[10] << 16) | (keyData[9] << 8) | keyData[8]);
  123. }
  124. /// <summary>
  125. /// Update encryption keys
  126. /// </summary>
  127. protected void UpdateKeys(byte ch)
  128. {
  129. keys[0] = Crc32.ComputeCrc32(keys[0], ch);
  130. keys[1] = keys[1] + (byte)keys[0];
  131. keys[1] = keys[1] * 134775813 + 1;
  132. keys[2] = Crc32.ComputeCrc32(keys[2], (byte)(keys[1] >> 24));
  133. }
  134. /// <summary>
  135. /// Reset the internal state.
  136. /// </summary>
  137. protected void Reset()
  138. {
  139. keys[0] = 0;
  140. keys[1] = 0;
  141. keys[2] = 0;
  142. }
  143. #region Instance Fields
  144. uint[] keys;
  145. #endregion
  146. }
  147. /// <summary>
  148. /// PkzipClassic CryptoTransform for encryption.
  149. /// </summary>
  150. class PkzipClassicEncryptCryptoTransform : PkzipClassicCryptoBase, ICryptoTransform
  151. {
  152. /// <summary>
  153. /// Initialise a new instance of <see cref="PkzipClassicEncryptCryptoTransform"></see>
  154. /// </summary>
  155. /// <param name="keyBlock">The key block to use.</param>
  156. internal PkzipClassicEncryptCryptoTransform(byte[] keyBlock)
  157. {
  158. SetKeys(keyBlock);
  159. }
  160. #region ICryptoTransform Members
  161. /// <summary>
  162. /// Transforms the specified region of the specified byte array.
  163. /// </summary>
  164. /// <param name="inputBuffer">The input for which to compute the transform.</param>
  165. /// <param name="inputOffset">The offset into the byte array from which to begin using data.</param>
  166. /// <param name="inputCount">The number of bytes in the byte array to use as data.</param>
  167. /// <returns>The computed transform.</returns>
  168. public byte[] TransformFinalBlock(byte[] inputBuffer, int inputOffset, int inputCount)
  169. {
  170. byte[] result = new byte[inputCount];
  171. TransformBlock(inputBuffer, inputOffset, inputCount, result, 0);
  172. return result;
  173. }
  174. /// <summary>
  175. /// Transforms the specified region of the input byte array and copies
  176. /// the resulting transform to the specified region of the output byte array.
  177. /// </summary>
  178. /// <param name="inputBuffer">The input for which to compute the transform.</param>
  179. /// <param name="inputOffset">The offset into the input byte array from which to begin using data.</param>
  180. /// <param name="inputCount">The number of bytes in the input byte array to use as data.</param>
  181. /// <param name="outputBuffer">The output to which to write the transform.</param>
  182. /// <param name="outputOffset">The offset into the output byte array from which to begin writing data.</param>
  183. /// <returns>The number of bytes written.</returns>
  184. public int TransformBlock(byte[] inputBuffer, int inputOffset, int inputCount, byte[] outputBuffer, int outputOffset)
  185. {
  186. for (int i = inputOffset; i < inputOffset + inputCount; ++i) {
  187. byte oldbyte = inputBuffer[i];
  188. outputBuffer[outputOffset++] = (byte)(inputBuffer[i] ^ TransformByte());
  189. UpdateKeys(oldbyte);
  190. }
  191. return inputCount;
  192. }
  193. /// <summary>
  194. /// Gets a value indicating whether the current transform can be reused.
  195. /// </summary>
  196. public bool CanReuseTransform
  197. {
  198. get {
  199. return true;
  200. }
  201. }
  202. /// <summary>
  203. /// Gets the size of the input data blocks in bytes.
  204. /// </summary>
  205. public int InputBlockSize
  206. {
  207. get {
  208. return 1;
  209. }
  210. }
  211. /// <summary>
  212. /// Gets the size of the output data blocks in bytes.
  213. /// </summary>
  214. public int OutputBlockSize
  215. {
  216. get {
  217. return 1;
  218. }
  219. }
  220. /// <summary>
  221. /// Gets a value indicating whether multiple blocks can be transformed.
  222. /// </summary>
  223. public bool CanTransformMultipleBlocks
  224. {
  225. get {
  226. return true;
  227. }
  228. }
  229. #endregion
  230. #region IDisposable Members
  231. /// <summary>
  232. /// Cleanup internal state.
  233. /// </summary>
  234. public void Dispose()
  235. {
  236. Reset();
  237. }
  238. #endregion
  239. }
  240. /// <summary>
  241. /// PkzipClassic CryptoTransform for decryption.
  242. /// </summary>
  243. class PkzipClassicDecryptCryptoTransform : PkzipClassicCryptoBase, ICryptoTransform
  244. {
  245. /// <summary>
  246. /// Initialise a new instance of <see cref="PkzipClassicDecryptCryptoTransform"></see>.
  247. /// </summary>
  248. /// <param name="keyBlock">The key block to decrypt with.</param>
  249. internal PkzipClassicDecryptCryptoTransform(byte[] keyBlock)
  250. {
  251. SetKeys(keyBlock);
  252. }
  253. #region ICryptoTransform Members
  254. /// <summary>
  255. /// Transforms the specified region of the specified byte array.
  256. /// </summary>
  257. /// <param name="inputBuffer">The input for which to compute the transform.</param>
  258. /// <param name="inputOffset">The offset into the byte array from which to begin using data.</param>
  259. /// <param name="inputCount">The number of bytes in the byte array to use as data.</param>
  260. /// <returns>The computed transform.</returns>
  261. public byte[] TransformFinalBlock(byte[] inputBuffer, int inputOffset, int inputCount)
  262. {
  263. byte[] result = new byte[inputCount];
  264. TransformBlock(inputBuffer, inputOffset, inputCount, result, 0);
  265. return result;
  266. }
  267. /// <summary>
  268. /// Transforms the specified region of the input byte array and copies
  269. /// the resulting transform to the specified region of the output byte array.
  270. /// </summary>
  271. /// <param name="inputBuffer">The input for which to compute the transform.</param>
  272. /// <param name="inputOffset">The offset into the input byte array from which to begin using data.</param>
  273. /// <param name="inputCount">The number of bytes in the input byte array to use as data.</param>
  274. /// <param name="outputBuffer">The output to which to write the transform.</param>
  275. /// <param name="outputOffset">The offset into the output byte array from which to begin writing data.</param>
  276. /// <returns>The number of bytes written.</returns>
  277. public int TransformBlock(byte[] inputBuffer, int inputOffset, int inputCount, byte[] outputBuffer, int outputOffset)
  278. {
  279. for (int i = inputOffset; i < inputOffset + inputCount; ++i) {
  280. byte newByte = (byte)(inputBuffer[i] ^ TransformByte());
  281. outputBuffer[outputOffset++] = newByte;
  282. UpdateKeys(newByte);
  283. }
  284. return inputCount;
  285. }
  286. /// <summary>
  287. /// Gets a value indicating whether the current transform can be reused.
  288. /// </summary>
  289. public bool CanReuseTransform
  290. {
  291. get {
  292. return true;
  293. }
  294. }
  295. /// <summary>
  296. /// Gets the size of the input data blocks in bytes.
  297. /// </summary>
  298. public int InputBlockSize
  299. {
  300. get {
  301. return 1;
  302. }
  303. }
  304. /// <summary>
  305. /// Gets the size of the output data blocks in bytes.
  306. /// </summary>
  307. public int OutputBlockSize
  308. {
  309. get {
  310. return 1;
  311. }
  312. }
  313. /// <summary>
  314. /// Gets a value indicating whether multiple blocks can be transformed.
  315. /// </summary>
  316. public bool CanTransformMultipleBlocks
  317. {
  318. get {
  319. return true;
  320. }
  321. }
  322. #endregion
  323. #region IDisposable Members
  324. /// <summary>
  325. /// Cleanup internal state.
  326. /// </summary>
  327. public void Dispose()
  328. {
  329. Reset();
  330. }
  331. #endregion
  332. }
  333. /// <summary>
  334. /// Defines a wrapper object to access the Pkzip algorithm.
  335. /// This class cannot be inherited.
  336. /// </summary>
  337. public sealed class PkzipClassicManaged : PkzipClassic
  338. {
  339. /// <summary>
  340. /// Get / set the applicable block size in bits.
  341. /// </summary>
  342. /// <remarks>The only valid block size is 8.</remarks>
  343. public override int BlockSize
  344. {
  345. get {
  346. return 8;
  347. }
  348. set {
  349. if (value != 8) {
  350. throw new CryptographicException("Block size is invalid");
  351. }
  352. }
  353. }
  354. /// <summary>
  355. /// Get an array of legal <see cref="KeySizes">key sizes.</see>
  356. /// </summary>
  357. public override KeySizes[] LegalKeySizes
  358. {
  359. get {
  360. KeySizes[] keySizes = new KeySizes[1];
  361. keySizes[0] = new KeySizes(12 * 8, 12 * 8, 0);
  362. return keySizes;
  363. }
  364. }
  365. /// <summary>
  366. /// Generate an initial vector.
  367. /// </summary>
  368. public override void GenerateIV()
  369. {
  370. // Do nothing.
  371. }
  372. /// <summary>
  373. /// Get an array of legal <see cref="KeySizes">block sizes</see>.
  374. /// </summary>
  375. public override KeySizes[] LegalBlockSizes
  376. {
  377. get {
  378. KeySizes[] keySizes = new KeySizes[1];
  379. keySizes[0] = new KeySizes(1 * 8, 1 * 8, 0);
  380. return keySizes;
  381. }
  382. }
  383. /// <summary>
  384. /// Get / set the key value applicable.
  385. /// </summary>
  386. public override byte[] Key
  387. {
  388. get {
  389. if ( key_ == null ) {
  390. GenerateKey();
  391. }
  392. return (byte[]) key_.Clone();
  393. }
  394. set {
  395. if ( value == null ) {
  396. throw new ArgumentNullException("value");
  397. }
  398. if ( value.Length != 12 ) {
  399. throw new CryptographicException("Key size is illegal");
  400. }
  401. key_ = (byte[]) value.Clone();
  402. }
  403. }
  404. /// <summary>
  405. /// Generate a new random key.
  406. /// </summary>
  407. public override void GenerateKey()
  408. {
  409. key_ = new byte[12];
  410. Random rnd = new Random();
  411. rnd.NextBytes(key_);
  412. }
  413. /// <summary>
  414. /// Create an encryptor.
  415. /// </summary>
  416. /// <param name="rgbKey">The key to use for this encryptor.</param>
  417. /// <param name="rgbIV">Initialisation vector for the new encryptor.</param>
  418. /// <returns>Returns a new PkzipClassic encryptor</returns>
  419. public override ICryptoTransform CreateEncryptor(
  420. byte[] rgbKey,
  421. byte[] rgbIV)
  422. {
  423. key_ = rgbKey;
  424. return new PkzipClassicEncryptCryptoTransform(Key);
  425. }
  426. /// <summary>
  427. /// Create a decryptor.
  428. /// </summary>
  429. /// <param name="rgbKey">Keys to use for this new decryptor.</param>
  430. /// <param name="rgbIV">Initialisation vector for the new decryptor.</param>
  431. /// <returns>Returns a new decryptor.</returns>
  432. public override ICryptoTransform CreateDecryptor(
  433. byte[] rgbKey,
  434. byte[] rgbIV)
  435. {
  436. key_ = rgbKey;
  437. return new PkzipClassicDecryptCryptoTransform(Key);
  438. }
  439. #region Instance Fields
  440. byte[] key_;
  441. #endregion
  442. }
  443. }
  444. #endif