Java encryption of files via streams

Source: Internet
Author: User
Tags decrypt key string

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.