Upload and download file encryption

Source: Internet
Author: User

When files are uploaded and downloaded, the files are encrypted.

1. Define Key

// encrypt the required key object Private Static Key key;

2, initialize the key (encryption and decryption method Getkey ("XX") in the same xx)

/*** Generate key based on parameters*/ Public Static voidGetKey (String strkey) {Try{keygenerator Generator= Keygenerator.getinstance ("DES"); Generator.init (NewSecureRandom (Strkey.getbytes ())); Key=Generator.generatekey (); } Catch(Exception e) {Throw NewRuntimeException ("Error Initializing Sqlmap class. Cause: "+e); }}

3. Encryption

/*** Encryption of files *@paramSrcfile *@throwsException*/ Public Static voidEncfile (File srcfile)throwsException {if(!srcfile.exists ()) {        Throw NewWarnexception ("file does not exist!) "); } String fileName=Srcfile.getabsolutepath (); inti = Filename.lastindexof (".")); if(i>0) {FileName= filename.substring (0, i); } File encfile=NewFile (fileName); GetKey ( "AAAA"); Cipher Cipher= Cipher.getinstance ("DES");    Cipher.init (Cipher.encrypt_mode,key); InputStream is=NewFileInputStream (srcfile); CipherOutputStream out=NewCipherOutputStream (NewFileOutputStream (encfile), cipher);    Ioutils.copylarge (is, out);    Is.close ();    Out.flush ();       Out.close (); Srcfile.delete ();}

4. Decryption

/*** Decryption *@paramSrcfile *@paramsuffix *@return * @throwsException*/ Public StaticFileInputStream decfile (File srcfile,string suffix)throwsException {fileinputstream fis=NULL; if(!srcfile.exists ()) {        Throw NewWarnexception ("file does not exist!) "); } String fileName= Srcfile.getabsolutepath () + "." +suffix; File Decfile=NewFile (fileName); GetKey ( "AAAA"); Cipher Cipher= Cipher.getinstance ("DES");    Cipher.init (Cipher.decrypt_mode, key); InputStream is=NewFileInputStream (srcfile); OutputStream out=NewFileOutputStream (Decfile); CipherOutputStream Cos=NewCipherOutputStream (out, cipher); byte[] buffer =New byte[1024]; intlength;  while(length = is.read (buffer)) >= 0) {cos.write (buffer,0, length); } FIS=NewFileInputStream (Decfile);    Cos.close ();    Out.close ();    Is.close (); returnfis;}

Upload and download file encryption

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.