package com.example;
import java.io.File;
import java.io.FileInputStream;
import Java.io.file not found exception;
import java.io.FileOutputStream;
import java.io.IOException;
import Java.security.invalidalgorithmparameterexception;
import Java.security.invalidkeyexception;
import Java.security.nosuchalgorithmexception;
import javax.crypto.Cipher;
import javax.crypto.cipher inputstream;
import javax.crypto.cipher output stream;
import javax.crypto.key generator;
import javax.crypto.nosuchpaddingexception;
import javax.crypto.spec.ivparameterspec;
import javax.crypto.spec.secret key spec; publicclassmycipher { publicstaticfinalstring幸福老师='0102030405060708 '; privatestaticfinalstring _ file _ name _ of _ secret _=' file-secret '; privatestaticfinalstring _ file _ loc _ file _ name _=' file-local '; privatestaticfinalstring _ source _ file _ path _=' path ' file.separator _ file _ loc _ file _ name _; privatestaticfinalstring _ output _ file _ path _=' path ' file.separator _ file _ name _ of _ secret _; publicstaticvoidmain (stringkndhmargs ) {
encrypt file (_ file _ name _ of _ secret _、_SOURCE_FILE_PATH_、_OUTPUT_FILE_PATH_ );
(/**
AES Cipher初始化
*
* @param sKey
* @param cipherMode
* @return*/
创建专用密钥密钥
密钥生成器密钥生成器=null;
Cipher cipher=null; try{
cipher=cipher.getinstance (AES/CBC/pkcs5padding );
cipher.init(ciphermode,key,zeroIv );
} catch (nosuchalgorithmexceptione ) )
e .打印任务跟踪(;
}catch(nosuchpaddingexceptione ) )
e .打印任务跟踪(;
}catch(invalidkeyexceptione ) )
e .打印任务跟踪(;
} catch (invalidalgorithmparameterexceptione )。
e .打印任务跟踪(;
}returncipher;
(/**
AES加密哭泣的毛衣
*
* @param key
* @param sourceFilePath
* @param destFilePath
* @return*/
publicstaticfileencryptfile (string key,String sourceFilePath,String destFilePath ) {
文件destfile=null;
文件源文件=null; try{
源文件=new文件(源文件路径;
destfile=newfile(destfilepath; source file.exists () sourceFile.isFile ) ) if (! dstfile.getparentfile((.exists ) ) ) ) )。
destFile.getParentFile ().mkdirs );
}
destFile.createNewFile (; in=new file inputstream (source file ); out=new file output stream (destfile;
ciphercipher=initaescipher(key,Cipher.ENCRYPT_MODE; //用加密流写入哭泣的毛衣
}
cipherInputStream.close (;
}
}catch(filenotfoundexceptione ) {
e .打印任务跟踪(;
}catch(ioexceptione ) {
e .打印任务跟踪(;
}finally{try{out.close (;
}catch(ioexceptione ) {
e .打印任务跟踪(;
}try{in.close (;
}catch(ioexceptione ) {
e .打印任务跟踪(;
}
}returndestFile;
(/**
*用AES方式解密哭泣的毛衣
*
* @param key
* @param sourceFilePath
* @param destFilePath
* @return*/
publicstaticfiledecryptfile (string key,String sourceFilePath,String destFilePath ) {
文件destfile=null;
文件源文件=null; try{
源文件=new文件(源文件路径;
destfile=newfile(destfilepath; source file.exists () sourceFile.isFile ) ) if (! dstfile.getparentfile((.exists ) ) ) ) )。
destFile.getParentFile ().mkdirs );
}
destFile.createNewFile (; in=new file inputstream (source file ); out=new file output stream (destfile;
ciphercipher=initaescipher(key,Cipher.DECRYPT_MODE;
cipher output stream.write (buffer,0,r );
}
cipherOutputStream.close (;
}
}catch(ioexceptione ) {
e .打印任务跟踪(;
}finally{try{in.close (;
}catch(ioexceptione ) {
e .打印任务跟踪(;
}try{out.close (;
}catch(ioexceptione ) {
e .打印任务跟踪(;
}
}returndestFile;
}
}