📄 jeffile.java
字号:
if ( !srcFile.exists() || !srcFile.canRead() ){ JahiaConsole.println(CLASS_NAME+"::Constructor ","cannot access file " + path ); throw new JahiaException (MSG_INTERNAL_ERROR, "cannot access file " + path , JefFileConstants.IO_ERROR, JahiaException.ERROR); } filePath = path; boolean securityErr = false; boolean tryAgain = true; boolean goSleep = false; long fLength = 0; long storedChecksum = 0; long streamChecksum = -1; byte[] bytes = null; byte[] data = null; while ( tryAgain ){ // try again if there is an error // due to the fact that the file is still modified securityErr = false; fLength = srcFile.length(); goSleep = false; try { fstream = new FileInputStream (srcFile); stream = new DataInputStream (fstream); // create the raw byte stream from the file size by removing the // checksum bytes (long=8bytes), the Cipher secret key(int=4bytes) and the offset(short=4bytes) int streamSize = (new Long(srcFile.length() - 16)).intValue(); bos = new ByteArrayOutputStream(); byte[] buff = null; int crcOffset = stream.readInt (); //JahiaConsole.println (CLASS_NAME+"::Constructor ","offset " + crcOffset); buff = new byte[crcOffset]; stream.read(buff,0,crcOffset); bos.write(buff); // int the Cipher Security.addProvider( new com.sun.crypto.provider.SunJCE()); // get the secret key int keyLength = stream.readInt(); byte[] keyAsBytes = new byte[keyLength]; stream.read(keyAsBytes); DESKeySpec keySpec = new DESKeySpec(keyAsBytes); Key myKey = SecretKeyFactory.getInstance("DES").generateSecret(keySpec); keyAsBytes = myKey.getEncoded(); //System.out.println (" Cipher secret key = "+ new String(keyAsBytes) ); Cipher decCipher = Cipher.getInstance("DES"); decCipher.init(Cipher.DECRYPT_MODE, myKey); storedChecksum = stream.readLong (); /* JahiaConsole.println (CLASS_NAME+"::Constructor ", " stored checksum = "+Long.toHexString (storedChecksum)); */ buff = new byte[streamSize-(crcOffset+keyLength)]; stream.read(buff,0,buff.length); bos.write(buff); buff = null; bytes = bos.toByteArray(); // free memory of unused objects fstream.close(); stream = null; fstream = null; srcFile = null; bos.close(); bos = null; // compute the stream CRC CRC32 crc = new CRC32(); crc.update (bytes); streamChecksum = crc.getValue (); /* JahiaConsole.println (CLASS_NAME+"::Constructor ", " stream checksum = "+Long.toHexString (streamChecksum)); */ //JahiaConsole.println(CLASS_NAME," data stream length before Cipher decryption " + bytes.length ); byte[] decryptedBytes = decCipher.doFinal(bytes); bytes = null; //JahiaConsole.println(CLASS_NAME," data stream length after Cipher decryption " + decryptedBytes.length ); if ( srcFile == null ){ srcFile = new File(path); } if ( fLength == srcFile.length() ){ if ( (streamChecksum != storedChecksum) ){ JahiaConsole.println (CLASS_NAME," checksum not match " ); throw new InvalidJefFileException (); } byteStream = new ByteArrayInputStream (decryptedBytes); stream = new DataInputStream (byteStream); int offset = 0; int totalOffset = offset; //JahiaConsole.println (CLASS_NAME,"Jahia Encrypted File Info :"); // extract keys value offset = stream.readShort (); totalOffset += offset; //JahiaConsole.println (CLASS_NAME,"keys nb char: " + offset); byte[] chars = new byte[offset]; if ( offset>0 ){ stream.read(chars,0,offset); String keysString = new String(chars,CHAR_ENC); //JahiaConsole.println (CLASS_NAME,"keys : " + keys); keys = parseKeys(keysString); } else { //JahiaConsole.println (CLASS_NAME,"no keys values"); } //Extract the file content data = new byte[decryptedBytes.length-(totalOffset+2)]; stream.read(data,0,data.length); //JahiaConsole.println(CLASS_NAME,"files nb byte = " + data.length); // extract the encrypted files byteStream = new ByteArrayInputStream (data); stream = new DataInputStream (byteStream); int nbBytes = data.length; int nbBytesRead = 0; offset = 0; String fileName = ""; Long fSize = null; byte[] aFile = null; while( nbBytesRead<nbBytes ){ // extract the filename offset = stream.readShort(); nbBytesRead += 2; chars = new byte[offset]; nbBytesRead += offset; stream.read(chars,0,offset); fileName = new String(chars,CHAR_ENC); //JahiaConsole.println(CLASS_NAME," read encryted file " + fileName + "\n"); // extract the file size fSize = new Long(stream.readLong()); //JahiaConsole.println(CLASS_NAME," size " +fSize + "\n"); aFile = new byte[fSize.intValue()]; nbBytesRead += 8; stream.read(aFile,0,fSize.intValue()); nbBytesRead += fSize.intValue(); files.put(fileName,aFile); aFile = null; //JahiaConsole.println(CLASS_NAME," nb bytes read " + nbBytesRead + "\n"); } if ( fLength == srcFile.length() ){ tryAgain = false; } } else { goSleep = true; } bytes = null; data = null; } catch ( java.security.InvalidKeyException ike ){ securityErr = true; JahiaConsole.println (CLASS_NAME," exception occured " + ike.getMessage() ); ike.printStackTrace(); throw new JahiaException (MSG_INTERNAL_ERROR, "JefFile , security exception" + ike.getMessage(), JefFileConstants.JAVA_SECURITY_ERROR, JahiaException.ERROR); } catch ( java.security.NoSuchAlgorithmException nae ){ securityErr = true; JahiaConsole.println (CLASS_NAME," exception occured " + nae.getMessage() ); nae.printStackTrace(); throw new JahiaException (MSG_INTERNAL_ERROR, "JefFile , security exception" + nae.getMessage(), JefFileConstants.JAVA_SECURITY_ERROR, JahiaException.ERROR); } catch ( java.security.spec.InvalidKeySpecException iks ){ securityErr = true; JahiaConsole.println (CLASS_NAME," exception occured " + iks.getMessage() ); iks.printStackTrace(); throw new JahiaException (MSG_INTERNAL_ERROR, "JefFile , security exception" + iks.getMessage(), JefFileConstants.JAVA_SECURITY_ERROR, JahiaException.ERROR); } catch ( javax.crypto.NoSuchPaddingException npe ){ securityErr = true; JahiaConsole.println (CLASS_NAME," exception occured " + npe.getMessage() ); npe.printStackTrace(); throw new JahiaException (MSG_INTERNAL_ERROR, "JefFile , security exception" + npe.getMessage(), JefFileConstants.JAVA_SECURITY_ERROR, JahiaException.ERROR); } catch ( javax.crypto.IllegalBlockSizeException ibse ){ securityErr = true; JahiaConsole.println (CLASS_NAME," exception occured " + ibse.getMessage() ); ibse.printStackTrace(); throw new JahiaException (MSG_INTERNAL_ERROR, "JefFile , security exception" +ibse.getMessage(), JefFileConstants.JAVA_SECURITY_ERROR, JahiaException.ERROR); } catch ( javax.crypto.BadPaddingException bpe ){ securityErr = true; JahiaConsole.println (CLASS_NAME," exception occured " + bpe.getMessage() ); bpe.printStackTrace(); throw new JahiaException (MSG_INTERNAL_ERROR, "JefFile , security exception" +bpe.getMessage(), JefFileConstants.JAVA_SECURITY_ERROR, JahiaException.ERROR); } catch ( java.lang.ExceptionInInitializerError eiie ){ securityErr = true; JahiaConsole.println (CLASS_NAME," exception occured " + eiie.getMessage() ); eiie.printStackTrace(); throw new JahiaException (MSG_INTERNAL_ERROR, "JefFile , java.lang.ExceptionInInitializerError " +eiie.getMessage(), JefFileConstants.JAVA_SECURITY_ERROR, JahiaException.ERROR); } catch ( IOException ioe ){ JahiaConsole.println (CLASS_NAME," exception occured " + ioe.getMessage() ); ioe.printStackTrace(); if ( srcFile.isFile() && srcFile.canRead() && (fLength == srcFile.length()) ){ File tmpFile = new File(srcFile.getAbsolutePath() + "_error"); srcFile.renameTo(tmpFile); tryAgain = false; throw new JahiaException (MSG_INTERNAL_ERROR, "JefFile , IOException " +ioe.getMessage(), JefFileConstants.IO_ERROR, JahiaException.ERROR); } else { goSleep = true; } } catch ( Throwable t ){ JahiaConsole.println (CLASS_NAME," exception occured " + t.getMessage() ); t.printStackTrace(); if ( srcFile.isFile() && srcFile.canRead() && (fLength == srcFile.length()) ){ //File tmpFile = new File(srcFile.getAbsolutePath() + "_error"); //srcFile.renameTo(tmpFile); tryAgain = false; throw new JahiaException (MSG_INTERNAL_ERROR, "JefFile , Unknown Exception ", JefFileConstants.UNKNOWN_ERROR, JahiaException.ERROR); } else { goSleep = true; } } if ( goSleep ){ try { // wait while the file is still modified //JahiaConsole.println(CLASS_NAME," go to sleep1 , file length=" + fLength); Thread.sleep(500); while ( fLength < srcFile.length() ){ fLength = srcFile.length(); //JahiaConsole.println(CLASS_NAME," go to sleep2 , file length=" + fLength); Thread.sleep(500); } } catch ( Throwable tr ){ JahiaConsole.println(CLASS_NAME,tr.getMessage()); } } } } finally { if ( byteStream != null ){ byteStream.close(); } byteStream = null; if ( stream != null ){ stream.close(); } stream = null; if ( fstream != null ){ fstream.close(); } fstream = null; if ( bos != null ){ bos.close(); } bos = null; } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -