public static byte[] toByteArray(int in,int outSize) { byte[] out = new byte[outSize]; byte[] intArray = toByteArray(in); for( int i=0; i<intArray.length && i<outSize; i++ ) { out[i] = intArray[i]; } return out; }
public int readInt() throws IOException { byte[] b = new byte[4]; file.read(b, 0, 4); int out = toInt(b); return out; }
public long readLong() throws IOException { byte[] b = new byte[8]; file.read(b, 0, 8); long out = toLong(b); return out; }
public void init( String pwStr, int keySize, byte[] salt, byte[] pwVerification ) throws ZipException { byte[] pwBytes = pwStr.getBytes(); super.saltBytes = salt; PBEParametersGenerator generator = new PKCS5S2ParametersGenerator(); generator.init( pwBytes, salt, ITERATION_COUNT ); cipherParameters = generator.generateDerivedParameters(KEY_SIZE_BIT*2 + 16); byte[] keyBytes = ((KeyParameter)cipherParameters).getKey(); this.cryptoKeyBytes = new byte[ KEY_SIZE_BYTE ]; System.arraycopy( keyBytes, 0, cryptoKeyBytes, 0, KEY_SIZE_BYTE ); this.authenticationCodeBytes = new byte[ KEY_SIZE_BYTE ]; System.arraycopy( keyBytes, KEY_SIZE_BYTE, authenticationCodeBytes, 0, KEY_SIZE_BYTE ); // based on SALT + PASSWORD (password is probably correct) this.pwVerificationBytes = new byte[ 2 ]; System.arraycopy( keyBytes, KEY_SIZE_BYTE*2, this.pwVerificationBytes, 0, 2 ); if( !ByteArrayHelper.isEqual( this.pwVerificationBytes, pwVerification ) ) { throw new ZipException("wrong password - " + ByteArrayHelper.toString(this.pwVerificationBytes) + "/ " + ByteArrayHelper.toString(pwVerification)); } // create the first 16 bytes of the key sequence again (using pw+salt) generator.init( pwBytes, salt, ITERATION_COUNT ); cipherParameters = generator.generateDerivedParameters(KEY_SIZE_BIT); // checksum added to the end of the encrypted data, update on each encryption call this.mac = new HMac( new SHA1Digest() ); mac.init( new KeyParameter(authenticationCodeBytes) ); this.aesCipher = new SICBlockCipher(new AESEngine()); this.blockSize = aesCipher.getBlockSize(); // incremented on each 16 byte block and used as encryption NONCE (ivBytes) nonce = 1; }
public short readShort() throws IOException { byte[] b = new byte[2]; file.read(b, 0, 2); short out = toShort(b); return out; }
public void init( String pwStr, int keySize, byte[] salt, byte[] pwVerification ) throws ZipException { byte[] pwBytes = pwStr.getBytes(); super.saltBytes = salt; PBEParametersGenerator generator = new PKCS5S2ParametersGenerator(); generator.init( pwBytes, salt, ITERATION_COUNT ); cipherParameters = generator.generateDerivedParameters(KEY_SIZE_BIT*2 + 16); byte[] keyBytes = ((KeyParameter)cipherParameters).getKey(); this.cryptoKeyBytes = new byte[ KEY_SIZE_BYTE ]; System.arraycopy( keyBytes, 0, cryptoKeyBytes, 0, KEY_SIZE_BYTE ); this.authenticationCodeBytes = new byte[ KEY_SIZE_BYTE ]; System.arraycopy( keyBytes, KEY_SIZE_BYTE, authenticationCodeBytes, 0, KEY_SIZE_BYTE ); // based on SALT + PASSWORD (password is probably correct) this.pwVerificationBytes = new byte[ 2 ]; System.arraycopy( keyBytes, KEY_SIZE_BYTE*2, this.pwVerificationBytes, 0, 2 ); if( !ByteArrayHelper.isEqual( this.pwVerificationBytes, pwVerification ) ) { throw new ZipException("wrong password - " + ByteArrayHelper.toString(this.pwVerificationBytes) + "/ " + ByteArrayHelper.toString(pwVerification)); } // create the first 16 bytes of the key sequence again (using pw+salt) generator.init( pwBytes, salt, ITERATION_COUNT ); cipherParameters = generator.generateDerivedParameters(KEY_SIZE_BIT); // checksum added to the end of the encrypted data, update on each encryption call this.mac = new HMac( new SHA1Digest() ); mac.init( new KeyParameter(authenticationCodeBytes) ); this.aesCipher = new SICBlockCipher(new AESEngine()); this.blockSize = aesCipher.getBlockSize(); // incremented on each 16 byte block and used as encryption NONCE (ivBytes) nonce = 1; }
public short readShort() throws IOException { byte[] b = new byte[2]; file.read(b, 0, 2); short out = toShort(b); return out; }
public static byte[] toByteArray(int in,int outSize) { byte[] out = new byte[outSize]; byte[] intArray = toByteArray(in); for( int i=0; i<intArray.length && i<outSize; i++ ) { out[i] = intArray[i]; } return out; }
LOG.finest( "cryptoOff = " + cryptoHeaderOffset ); LOG.finest( "password = " + password + " - " + password.length() ); LOG.finest( "salt = " + ByteArrayHelper.toString(salt) + " - " + salt.length ); LOG.finest( "pwVerif = " + ByteArrayHelper.toString(pwVerification) + " - " + pwVerification.length );
public long readLong() throws IOException { byte[] b = new byte[8]; file.read(b, 0, 8); long out = toLong(b); return out; }
public int readInt() throws IOException { byte[] b = new byte[4]; file.read(b, 0, 4); int out = toInt(b); return out; }
protected void initDirOffsetPosAndComment() throws IOException { // zip files without a comment contain the offset/position of the central directory at this fixed position this.dirOffsetPos = zipFile.length() - 6; final int dirOffset = raFile.readInt( this.dirOffsetPos - 16 ); if( dirOffset!=ENDSIG ) { // if a comment is present, search the ENDSIG constant, starting at the end of the zip file byte[] endsig = ByteArrayHelper.toByteArray((int)ZipConstants.ENDSIG); long endsigPos = raFile.lastPosOf(endsig); if( endsigPos==-1 ) { throw new ZipException("expected ENDSIC not found (marks the beginning of the central directory at end of the zip file)"); } else { this.dirOffsetPos = endsigPos+16; short commentLength = raFile.readShort( this.dirOffsetPos + 4 ); this.comment = new String( raFile.readByteArray( this.dirOffsetPos+6, commentLength ) ); } } }
protected void initDirOffsetPosAndComment() throws IOException { // zip files without a comment contain the offset/position of the central directory at this fixed position this.dirOffsetPos = zipFile.length() - 6; final int dirOffset = raFile.readInt( this.dirOffsetPos - 16 ); if( dirOffset!=ENDSIG ) { // if a comment is present, search the ENDSIG constant, starting at the end of the zip file byte[] endsig = ByteArrayHelper.toByteArray((int)ZipConstants.ENDSIG); long endsigPos = raFile.lastPosOf(endsig); if( endsigPos==-1 ) { throw new ZipException("expected ENDSIC not found (marks the beginning of the central directory at end of the zip file)"); } else { this.dirOffsetPos = endsigPos+16; short commentLength = raFile.readShort( this.dirOffsetPos + 4 ); this.comment = new String( raFile.readByteArray( this.dirOffsetPos+6, commentLength ) ); } } }
LOG.finest( "cryptoOff = " + cryptoHeaderOffset ); LOG.finest( "password = " + password + " - " + password.length() ); LOG.finest( "salt = " + ByteArrayHelper.toString(salt) + " - " + salt.length ); LOG.finest( "pwVerif = " + ByteArrayHelper.toString(pwVerification) + " - " + pwVerification.length );
/** * encrypt 16 bytes (AES standard block size) or less * starting at "pos" within "in" byte[] */ protected void decryptBlock( byte[] in, int pos, int length ) { byte[] decryptedIn = new byte[blockSize]; byte[] ivBytes = ByteArrayHelper.toByteArray( nonce++, 16 ); ParametersWithIV ivParams = new ParametersWithIV(cipherParameters, ivBytes); aesCipher.init( false, ivParams ); int remainingCount = length-pos; if( remainingCount>=blockSize ) { mac.update( in, pos, blockSize ); aesCipher.processBlock( in, pos, decryptedIn, 0 ); System.arraycopy( decryptedIn, 0, in, pos, blockSize ); } else { mac.update( in, pos, remainingCount ); byte[] extendedIn = new byte[blockSize]; System.arraycopy( in, pos, extendedIn, 0, remainingCount ); aesCipher.processBlock( extendedIn, 0, decryptedIn, 0 ); System.arraycopy( decryptedIn, 0, in, pos, remainingCount ); } }