Encrypt the destination file
The code is as follows:
Two encryption is not recommended, two encryption must be decrypted two times
PackageCom.xgt.util;ImportJava.io.*; Public classVideoencodeutil { Public Static voidMain (string[] args)throwsException {Encrypt ("D:\\py Trading \\11.mp4", "fuckyourself"); Decrypt ("D:\\py Trading \\11.mp4", "d:\\py trading \\22.mp4", 4); System.out.println (Readfilelastbyte ("D:\\py Trading \\11.mp4", 12)); } /*** file is encrypted *@paramfileUrl File path *@paramKey Password *@throwsException*/ Public Static voidEncrypt (string fileUrl, String key)throwsException {File file=NewFile (FILEURL); String Path=File.getpath (); if(!file.exists ()) { return; } intindex = path.lastindexof ("\ \"); String DestFile= path.substring (0, index) + "\ \" + "abc"; File dest=NewFile (destfile); //gets the input stream of the file to be encryptedInputStream in =NewFileInputStream (FILEURL); //Create a transit file output streamOutputStream out =NewFileOutputStream (destfile); //stream of files to encrypt byte[] buffer =New byte[1024]; intR; //The stream of files after encryption byte[] buffer2=New byte[1024]; while((r= in.read (buffer)) > 0) { for(inti=0;i<r;i++) { byteb=Buffer[i]; //buffer2[i]=b==255?0:++b; //plus 2 encryption per byteb+=2; Buffer2[i]=b; } out.write (Buffer2,0, R); Out.flush (); } in.close (); Out.close (); File.delete (); Dest.renameto (NewFile (FILEURL)); Appendmethoda (FILEURL, key); System.out.println ("Encryption succeeded"); } /** * * @paramFileName *@paramContent Key*/ Public Static voidAppendmethoda (String fileName, string content) {Try { //open a random Access file stream, read and writeRandomaccessfile Randomfile =NewRandomaccessfile (FileName, "RW"); //file length, number of bytes LongFilelength =randomfile.length (); //moves the write file pointer to the end of the file. Randomfile.seek (filelength); Randomfile.writebytes (content); Randomfile.close (); } Catch(IOException e) {e.printstacktrace (); } } /*** Decryption *@paramfileUrl Source file *@paramTempurl Temp File *@paramkeylength Password Length *@return * @throwsException*/ Public StaticString Decrypt (String fileUrl, String Tempurl,intKeylength)throwsexception{File File=NewFile (FILEURL); if(!file.exists ()) { return NULL; } File dest=NewFile (Tempurl); if(!Dest.getparentfile (). exists ()) {Dest.getparentfile (). Mkdirs (); } //get the file input stream to decryptInputStream is =NewFileInputStream (FILEURL); //Create a destination file output stream that is used to generate the decrypted fileOutputStream out =NewFileOutputStream (Tempurl); byte[] buffer =New byte[1024]; byte[] buffer2=New byte[1024]; byteBmax= (byte) 255; LongSize = File.length ()-keylength; intMoD = (int) (size%1024); intdiv = (int) (size>>10); intCount = Mod==0?div: (div+1); intK = 1, R; while((k <= count && (r = is.read (buffer)) > 0)) { if(mod! = 0 && k==count) {R=MoD; } for(inti = 0;i < r;i++) { byteb=Buffer[i]; //Buffer2[i]=b==0?bmax:--b; //minus 2 decoding per byteb-=2; Buffer2[i]=b; } out.write (Buffer2,0, R); K++; } out.close (); Is.close (); returnTempurl; } /*** Determine if the file is encrypted *@paramFileName *@return* * Encryption successfully returned key * Encryption failed to return a non-key string*/ Public StaticString Readfilelastbyte (String fileName,intkeylength) {File File=NewFile (fileName); if(!file.exists ())return"No Files"; StringBuffer Str=NewStringBuffer (); Try { //open a random Access file stream, read and writeRandomaccessfile Randomfile =NewRandomaccessfile (FileName, "RW"); //file length, number of bytes LongFilelength =randomfile.length (); //moves the write file pointer to the end of the file. for(inti = keylength; I>=1; i--) {Randomfile.seek (filelength-i); Str.append ((Char) Randomfile.read ()); } randomfile.close (); returnstr.tostring (); } Catch(IOException e) {e.printstacktrace (); } returnexception; }}
Java encryption of files via streams