123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498 |
- #if !NETCF_1_0
- using System;
- using System.Security.Cryptography;
- using CommonMPQ.SharpZipLib.Checksums;
- namespace CommonMPQ.SharpZipLib.Encryption
- {
-
-
-
-
-
- public abstract class PkzipClassic : SymmetricAlgorithm
- {
-
-
-
-
-
- static public byte[] GenerateKeys(byte[] seed)
- {
- if ( seed == null ) {
- throw new ArgumentNullException("seed");
- }
- if ( seed.Length == 0 ) {
- throw new ArgumentException("Length is zero", "seed");
- }
- uint[] newKeys = new uint[] {
- 0x12345678,
- 0x23456789,
- 0x34567890
- };
-
- for (int i = 0; i < seed.Length; ++i) {
- newKeys[0] = Crc32.ComputeCrc32(newKeys[0], seed[i]);
- newKeys[1] = newKeys[1] + (byte)newKeys[0];
- newKeys[1] = newKeys[1] * 134775813 + 1;
- newKeys[2] = Crc32.ComputeCrc32(newKeys[2], (byte)(newKeys[1] >> 24));
- }
- byte[] result = new byte[12];
- result[0] = (byte)(newKeys[0] & 0xff);
- result[1] = (byte)((newKeys[0] >> 8) & 0xff);
- result[2] = (byte)((newKeys[0] >> 16) & 0xff);
- result[3] = (byte)((newKeys[0] >> 24) & 0xff);
- result[4] = (byte)(newKeys[1] & 0xff);
- result[5] = (byte)((newKeys[1] >> 8) & 0xff);
- result[6] = (byte)((newKeys[1] >> 16) & 0xff);
- result[7] = (byte)((newKeys[1] >> 24) & 0xff);
- result[8] = (byte)(newKeys[2] & 0xff);
- result[9] = (byte)((newKeys[2] >> 8) & 0xff);
- result[10] = (byte)((newKeys[2] >> 16) & 0xff);
- result[11] = (byte)((newKeys[2] >> 24) & 0xff);
- return result;
- }
- }
-
-
-
-
- class PkzipClassicCryptoBase
- {
-
-
-
-
-
-
- protected byte TransformByte()
- {
- uint temp = ((keys[2] & 0xFFFF) | 2);
- return (byte)((temp * (temp ^ 1)) >> 8);
- }
-
-
-
-
- protected void SetKeys(byte[] keyData)
- {
- if ( keyData == null ) {
- throw new ArgumentNullException("keyData");
- }
-
- if ( keyData.Length != 12 ) {
- throw new InvalidOperationException("Key length is not valid");
- }
-
- keys = new uint[3];
- keys[0] = (uint)((keyData[3] << 24) | (keyData[2] << 16) | (keyData[1] << 8) | keyData[0]);
- keys[1] = (uint)((keyData[7] << 24) | (keyData[6] << 16) | (keyData[5] << 8) | keyData[4]);
- keys[2] = (uint)((keyData[11] << 24) | (keyData[10] << 16) | (keyData[9] << 8) | keyData[8]);
- }
-
-
-
- protected void UpdateKeys(byte ch)
- {
- keys[0] = Crc32.ComputeCrc32(keys[0], ch);
- keys[1] = keys[1] + (byte)keys[0];
- keys[1] = keys[1] * 134775813 + 1;
- keys[2] = Crc32.ComputeCrc32(keys[2], (byte)(keys[1] >> 24));
- }
-
-
-
- protected void Reset()
- {
- keys[0] = 0;
- keys[1] = 0;
- keys[2] = 0;
- }
-
- #region Instance Fields
- uint[] keys;
- #endregion
- }
-
-
-
- class PkzipClassicEncryptCryptoTransform : PkzipClassicCryptoBase, ICryptoTransform
- {
-
-
-
-
- internal PkzipClassicEncryptCryptoTransform(byte[] keyBlock)
- {
- SetKeys(keyBlock);
- }
- #region ICryptoTransform Members
-
-
-
-
-
-
-
- public byte[] TransformFinalBlock(byte[] inputBuffer, int inputOffset, int inputCount)
- {
- byte[] result = new byte[inputCount];
- TransformBlock(inputBuffer, inputOffset, inputCount, result, 0);
- return result;
- }
-
-
-
-
-
-
-
-
-
-
- public int TransformBlock(byte[] inputBuffer, int inputOffset, int inputCount, byte[] outputBuffer, int outputOffset)
- {
- for (int i = inputOffset; i < inputOffset + inputCount; ++i) {
- byte oldbyte = inputBuffer[i];
- outputBuffer[outputOffset++] = (byte)(inputBuffer[i] ^ TransformByte());
- UpdateKeys(oldbyte);
- }
- return inputCount;
- }
-
-
-
- public bool CanReuseTransform
- {
- get {
- return true;
- }
- }
-
-
-
- public int InputBlockSize
- {
- get {
- return 1;
- }
- }
-
-
-
- public int OutputBlockSize
- {
- get {
- return 1;
- }
- }
-
-
-
- public bool CanTransformMultipleBlocks
- {
- get {
- return true;
- }
- }
- #endregion
- #region IDisposable Members
-
-
-
- public void Dispose()
- {
- Reset();
- }
- #endregion
- }
-
-
-
- class PkzipClassicDecryptCryptoTransform : PkzipClassicCryptoBase, ICryptoTransform
- {
-
-
-
-
- internal PkzipClassicDecryptCryptoTransform(byte[] keyBlock)
- {
- SetKeys(keyBlock);
- }
- #region ICryptoTransform Members
-
-
-
-
-
-
-
- public byte[] TransformFinalBlock(byte[] inputBuffer, int inputOffset, int inputCount)
- {
- byte[] result = new byte[inputCount];
- TransformBlock(inputBuffer, inputOffset, inputCount, result, 0);
- return result;
- }
-
-
-
-
-
-
-
-
-
-
- public int TransformBlock(byte[] inputBuffer, int inputOffset, int inputCount, byte[] outputBuffer, int outputOffset)
- {
- for (int i = inputOffset; i < inputOffset + inputCount; ++i) {
- byte newByte = (byte)(inputBuffer[i] ^ TransformByte());
- outputBuffer[outputOffset++] = newByte;
- UpdateKeys(newByte);
- }
- return inputCount;
- }
-
-
-
- public bool CanReuseTransform
- {
- get {
- return true;
- }
- }
-
-
-
- public int InputBlockSize
- {
- get {
- return 1;
- }
- }
-
-
-
- public int OutputBlockSize
- {
- get {
- return 1;
- }
- }
-
-
-
- public bool CanTransformMultipleBlocks
- {
- get {
- return true;
- }
- }
- #endregion
- #region IDisposable Members
-
-
-
- public void Dispose()
- {
- Reset();
- }
- #endregion
- }
-
-
-
-
- public sealed class PkzipClassicManaged : PkzipClassic
- {
-
-
-
-
- public override int BlockSize
- {
- get {
- return 8;
- }
- set {
- if (value != 8) {
- throw new CryptographicException("Block size is invalid");
- }
- }
- }
-
-
-
- public override KeySizes[] LegalKeySizes
- {
- get {
- KeySizes[] keySizes = new KeySizes[1];
- keySizes[0] = new KeySizes(12 * 8, 12 * 8, 0);
- return keySizes;
- }
- }
-
-
-
- public override void GenerateIV()
- {
-
- }
-
-
-
- public override KeySizes[] LegalBlockSizes
- {
- get {
- KeySizes[] keySizes = new KeySizes[1];
- keySizes[0] = new KeySizes(1 * 8, 1 * 8, 0);
- return keySizes;
- }
- }
-
-
-
- public override byte[] Key
- {
- get {
- if ( key_ == null ) {
- GenerateKey();
- }
-
- return (byte[]) key_.Clone();
- }
-
- set {
- if ( value == null ) {
- throw new ArgumentNullException("value");
- }
-
- if ( value.Length != 12 ) {
- throw new CryptographicException("Key size is illegal");
- }
-
- key_ = (byte[]) value.Clone();
- }
- }
-
-
-
- public override void GenerateKey()
- {
- key_ = new byte[12];
- Random rnd = new Random();
- rnd.NextBytes(key_);
- }
-
-
-
-
-
-
- public override ICryptoTransform CreateEncryptor(
- byte[] rgbKey,
- byte[] rgbIV)
- {
- key_ = rgbKey;
- return new PkzipClassicEncryptCryptoTransform(Key);
- }
-
-
-
-
-
-
- public override ICryptoTransform CreateDecryptor(
- byte[] rgbKey,
- byte[] rgbIV)
- {
- key_ = rgbKey;
- return new PkzipClassicDecryptCryptoTransform(Key);
- }
-
- #region Instance Fields
- byte[] key_;
- #endregion
- }
- }
- #endif
|