发布网友 发布时间:2022-04-24 19:31
共2个回答
热心网友 时间:2023-10-08 18:52
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[];
while (is.read(bytes1) > 0) {
byte[] de = encrypt.decrypt(RSAEncrypt.privateKey, bytes1);
bytes1 = new byte[];
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;
}
}
热心网友 时间:2023-10-08 18:53
随便一种加密方式都可以,只需要用base编码封装一下,保证传输的字节流的完整性,你可以去下载一个base的工具包