对文件加密解密
import java.io.File;
import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException;
import java.security.Key;
import java.security.SecureRandom;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator; import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
/**
* Ê?ÓÃAES?ÔÎÄ?þ?øÐÐ?ÓÃܺÍ?âÃÜ
*
*/
public class CipherUtil {
/**
* Ê?ÓÃAES?ÔÎÄ?þ?øÐÐ?ÓÃܺÍ?âÃÜ
*
*/
private static String type = "AES";
/**
* ?ÑÎÄ?þsrcFile?ÓÃܺó?æ??ΪdestFile
* @param srcFile ?ÓÃÜÇ?µÄÎÄ?þ
* @param destFile ?ÓÃܺóµÄÎÄ?þ
* @param privateKey ÃÜÔ?
* @throws GeneralSecurityException
* @throws IOException
*/
public void encrypt(String srcFile, String destFile, String
privateKey) throws GeneralSecurityException, IOException {
Key key = getKey(privateKey);
Cipher cipher = Cipher.getInstance(type + "/ECB/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, key);
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream(srcFile);
fos = new FileOutputStream(mkdirFiles(destFile));
crypt(fis, fos, cipher);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fis != null) {
fis.close();
}
if (fos != null) {
fos.close();
}
}
}
/**
* ?ÑÎÄ?þsrcFile?âÃܺó?æ??ΪdestFile
* @param srcFile ?âÃÜÇ?µÄÎÄ?þ
* @param destFile ?âÃܺóµÄÎÄ?þ
* @param privateKey ÃÜÔ?
* @throws GeneralSecurityException
* @throws IOException
*/
public void decrypt(String srcFile, String destFile, String
privateKey) throws GeneralSecurityException, IOException {
Key key = getKey(privateKey);
Cipher cipher = Cipher.getInstance(type + "/ECB/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, key);
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream(srcFile);
fos = new FileOutputStream(mkdirFiles(destFile));
crypt(fis, fos, cipher);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fis != null) {
fis.close();
}
if (fos != null) {
fos.close();
}
}
}
/**
* ?ù?ÝfilePath????ÏàÓ?µÄÄ?Â?
* @param filePath Òª????µÄÎÄ?þÂ??-
* @return file ÎÄ?þ
* @throws IOException
*/
private File mkdirFiles(String filePath) throws IOException {
File file = new File(filePath);
if (!file.getParentFile().exists()) {
file.getParentFile().mkdirs();
}
file.createNewFile();
return file;
}
/**
* Éú?ÉÖ???×Ö?û??µÄÃÜÔ?
* @param secret ÒªÉú?ÉÃÜÔ?µÄ×Ö?û??
* @return secretKey Éú?ɺóµÄÃÜÔ?
* @throws GeneralSecurityException
*/
private static Key getKey(String secret) throws
GeneralSecurityException {
KeyGenerator kgen = KeyGenerator.getInstance(type);
kgen.init(128, new SecureRandom(secret.getBytes()));
SecretKey secretKey = kgen.generateKey();
return secretKey;
}
/**
* ?ÓÃÜ?âÃÜÁ?
* @param in ?ÓÃÜ?âÃÜÇ?µÄÁ?
* @param out ?ÓÃÜ?âÃܺóµÄÁ?
* @param cipher ?ÓÃÜ?âÃÜ
* @throws IOException
* @throws GeneralSecurityException
*/
private static void crypt(InputStream in, OutputStream out, Cipher
cipher) throws IOException, GeneralSecurityException {
int blockSize = cipher.getBlockSize() * 1000;
int outputSize = cipher.getOutputSize(blockSize);
byte[] inBytes = new byte[blockSize];
byte[] outBytes = new byte[outputSize];
int inLength = 0;
boolean more = true;
while (more) {
inLength = in.read(inBytes);
if (inLength == blockSize) {
int outLength = cipher.update(inBytes, 0, blockSize, outBytes);
out.write(outBytes, 0, outLength);
} else {
more = false;
}
}
if (inLength > 0)
outBytes = cipher.doFinal(inBytes, 0, inLength);
else
outBytes = cipher.doFinal();
out.write(outBytes);
}
}
本文档为【对文件加密解密】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑,
图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
该文档来自用户分享,如有侵权行为请发邮件ishare@vip.sina.com联系网站客服,我们会及时删除。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。
本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。
网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。