如何用java实现文件(不只是txt文本)的整体加密解密?
如何用java实现文件(不只是txt文本)的整体加密解密?直接对整个文件进行加解密,不管其内容。比如xls表格文件,我需要对xls加密,而不是对xls每个单元格的字符串加...
如何用java实现文件(不只是txt文本)的整体加密解密?直接对整个文件进行加解密,不管其内容。比如xls表格文件,我需要对xls加密,而不是对xls每个单元格的字符串加密然后再生成新的xls。
提问最多只能悬赏100,搞定了后面我会追加的,谢谢。 展开
提问最多只能悬赏100,搞定了后面我会追加的,谢谢。 展开
2个回答
展开全部
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import javax.crypto.Cipher;
/**
* 文件加密解密
* 加解密需要依靠以下四个属性,
static KeyPairGenerator keyPairGen;
static KeyPair keyPair;
static RSAPrivateKey privateKey;
static RSAPublicKey publicKey;
* @author young
*
*/
public class RSAEncrypt {
static KeyPairGenerator keyPairGen;
static KeyPair keyPair;
static RSAPrivateKey privateKey;
static RSAPublicKey publicKey;
static {
try {
// 实例类型
keyPairGen = KeyPairGenerator.getInstance("RSA");
// 初始化长度
keyPairGen.initialize(512);
// 声场KeyPair
keyPair = keyPairGen.generateKeyPair();
// Generate keys
privateKey = (RSAPrivateKey) keyPair.getPrivate();
publicKey = (RSAPublicKey) keyPair.getPublic();
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
RSAEncrypt encrypt = new RSAEncrypt();
File file = new File(
"C:\\Documents and Settings\\Administrator.DCB5E0D91E0D436\\桌面\\sdf.txt");
File newFile = new File(
"C:\\Documents and Settings\\Administrator.DCB5E0D91E0D436\\桌面\\sdf1.txt");
encrypt.encryptFile(encrypt, file, newFile);
File file1 = new File(
"C:\\Documents and Settings\\Administrator.DCB5E0D91E0D436\\桌面\\sdf1.txt");
File newFile1 = new File(
"C:\\Documents and Settings\\Administrator.DCB5E0D91E0D436\\桌面\\sdf2.txt");
encrypt.decryptFile(encrypt, file1, newFile1);
}
/**
* 加密文件
* @param encrypt RSAEncrypt对象
* @param file 源文件
* @param newFile 目标文件
*/
public void encryptFile(RSAEncrypt encrypt, File file, File newFile) {
try {
InputStream is = new FileInputStream(file);
OutputStream os = new FileOutputStream(newFile);
byte[] bytes = new byte[53];
while (is.read(bytes) > 0) {
byte[] e = encrypt.encrypt(RSAEncrypt.publicKey, bytes);
bytes = new byte[53];
os.write(e, 0, e.length);
}
os.close();
is.close();
System.out.println("write success");
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 解密文件
* @param encrypt RSAEncrypt对象
* @param file
* @param newFile
*/
public void decryptFile(RSAEncrypt encrypt, File file, File newFile) {
try {
InputStream is = new FileInputStream(file);
OutputStream os = new FileOutputStream(newFile);
byte[] bytes1 = new byte[64];
while (is.read(bytes1) > 0) {
byte[] de = encrypt.decrypt(RSAEncrypt.privateKey, bytes1);
bytes1 = new byte[64];
os.write(de, 0, de.length);
}
os.close();
is.close();
System.out.println("write success");
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 加密实现
* * Encrypt String. *
*
* @return byte[] 加密后的字节数组
*/
protected byte[] encrypt(RSAPublicKey publicKey, byte[] obj) {
if (publicKey != null) {
try {
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
return cipher.doFinal(obj);
} catch (Exception e) {
e.printStackTrace();
}
}
return null;
}
/**
* 解密实现
* * Basic decrypt method *
*
* @return byte[] 解密后的字节数组
*/
protected byte[] decrypt(RSAPrivateKey privateKey, byte[] obj) {
if (privateKey != null) {
try {
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.DECRYPT_MODE, privateKey);
return cipher.doFinal(obj);
} catch (Exception e) {
e.printStackTrace();
}
}
return null;
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询