Ejemplo n.º 1
0
        int InitialRead(byte[] destination, int offset, int count)
        {
            if (entry.IsCrypted) {
            #if NETCF_1_0
                throw new ZipException("Encryption not supported for Compact Framework 1.0");
            #else

                PkzipClassicManaged managed = new PkzipClassicManaged();
                byte[] key = PkzipClassic.GenerateKeys(ZipConstants.ConvertToArray(password));

                inputBuffer.CryptoTransform = managed.CreateDecryptor(key, null);

                byte[] cryptbuffer = new byte[ZipConstants.CryptoHeaderSize];
                inputBuffer.ReadClearTextBuffer(cryptbuffer, 0, ZipConstants.CryptoHeaderSize);

                if (csize >= ZipConstants.CryptoHeaderSize) {
                    csize -= ZipConstants.CryptoHeaderSize;
                }

            #endif
            } else {
            #if !NETCF_1_0
                inputBuffer.CryptoTransform = null;
            #endif
            }

            if ((csize > 0) || ((flags & (int)GeneralBitFlags.Descriptor) != 0)) {
                if ((method == (int)CompressionMethod.Deflated) && (inputBuffer.Available > 0)) {
                    inputBuffer.SetInflaterInput(inf);
                }

                internalReader = new ReadDataHandler(BodyRead);
                return BodyRead(destination, offset, count);
            }
            else {
                internalReader = new ReadDataHandler(ReadingNotAvailable);
                return 0;
            }
        }
Ejemplo n.º 2
0
        Stream CreateAndInitEncryptionStream(Stream baseStream, ZipEntry entry)
        {
            CryptoStream result = null;
            if ( (entry.Version < ZipConstants.VersionStrongEncryption)
                || (entry.Flags & (int)GeneralBitFlags.StrongEncryption) == 0) {
                PkzipClassicManaged classicManaged = new PkzipClassicManaged();

                OnKeysRequired(entry.Name);

                // Closing a CryptoStream will close the base stream as well so wrap it in an UncompressedStream
                // which doesnt do this.
                result = new CryptoStream(new UncompressedStream(baseStream),
                    classicManaged.CreateEncryptor(key, null), CryptoStreamMode.Write);

                if ( (entry.Crc < 0) || (entry.Flags & 8) != 0) {
                    WriteEncryptionHeader(result, entry.DosTime << 16);
                }
                else {
                    WriteEncryptionHeader(result, entry.Crc);
                }
            }
            return result;
        }
Ejemplo n.º 3
0
		/// <summary>
		/// Initializes encryption keys based on given <paramref name="password"/>.
		/// </summary>
		/// <param name="password">The password.</param>
		protected void InitializePassword(string password)
		{
#if NETCF_1_0
			keys = new uint[] {
				0x12345678,
				0x23456789,
				0x34567890
			};
			
			byte[] rawPassword = ZipConstants.ConvertToArray(password);
			
			for (int i = 0; i < rawPassword.Length; ++i) {
				UpdateKeys((byte)rawPassword[i]);
			}
			
#else			
			PkzipClassicManaged pkManaged = new PkzipClassicManaged();
			byte[] key = PkzipClassic.GenerateKeys(ZipConstants.ConvertToArray(password));
			cryptoTransform_ = pkManaged.CreateEncryptor(key, null);
#endif
		}
Ejemplo n.º 4
0
        Stream CreateAndInitDecryptionStream(Stream baseStream, ZipEntry entry)
        {
            CryptoStream result = null;

            if ( (entry.Version < ZipConstants.VersionStrongEncryption)
                || (entry.Flags & (int)GeneralBitFlags.StrongEncryption) == 0) {
                PkzipClassicManaged classicManaged = new PkzipClassicManaged();

                OnKeysRequired(entry.Name);

                result = new CryptoStream(baseStream, classicManaged.CreateDecryptor(key, null), CryptoStreamMode.Read);
                CheckClassicPassword(result, entry);
            }
            else {
            #if !NET_1_1 && !NETCF_2_0
                if (entry.Version == ZipConstants.VERSION_AES) {
                    //
                    OnKeysRequired(entry.Name);
                    int saltLen = entry.AESSaltLen;
                    byte[] saltBytes = new byte[saltLen];
                    int saltIn = baseStream.Read(saltBytes, 0, saltLen);
                    byte[] pwdVerifyRead = new byte[2];
                    baseStream.Read(pwdVerifyRead, 0, 2);
                    int blockSize = entry.AESKeySize / 8;	// bits to bytes

                    ZipAESTransform decryptor = new ZipAESTransform(rawPassword_, saltBytes, blockSize, false);
                    byte[] pwdVerifyCalc = decryptor.PwdVerifier;
                    if (pwdVerifyCalc[0] != pwdVerifyRead[0] || pwdVerifyCalc[1] != pwdVerifyRead[1])
                        throw new Exception("Invalid password for AES");
                    result = new ZipAESStream(baseStream, decryptor, CryptoStreamMode.Read);
                }
                else
            #endif
                {
                }
            }

            return result;
        }