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
public class RSAEncrypt {
private KeyPairGenerator keyPairGen
private KeyPair keyPair
private RSAPrivateKey privateKey
private RSAPublicKey publicKey
public RSAEncrypt() {
try {
keyPairGen = KeyPairGenerator.getInstance("RSA")
keyPairGen.initialize(512)
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\\桌面\\test.txt")
File newFile = new File(
"C:\\Documents and Settings\\Administrator.DCB5E0D91E0D436\\桌面\\test1.txt")
encrypt.encryptFile(encrypt, file, newFile)
File file1 = new File(
"C:\\Documents and Settings\\Administrator.DCB5E0D91E0D436\\桌面\\test1.txt")
File newFile1 = new File(
"C:\\Documents and Settings\\Administrator.DCB5E0D91E0D436\\桌面\\test2.txt")
encrypt.decryptFile(encrypt, file1, newFile1)
}
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(encrypt.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()
}
}
public void decryptFile(RSAEncrypt encrypt, File file, File newFile) {
try {
InputStream is = new FileInputStream(
new File(
"C:\\Documents and Settings\\Administrator.DCB5E0D91E0D436\\桌面\\test1.txt"))
OutputStream os = new FileOutputStream(
"C:\\Documents and Settings\\Administrator.DCB5E0D91E0D436\\桌面\\test2.txt")
byte[] bytes1 = new byte[64]
while (is.read(bytes1) >0) {
byte[] de = encrypt.decrypt(encrypt.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
}
}
注意,RSAPrivateKey与RSAPublicKey在加密和解密中需保持一致,如果加密与解密需要分开进行,则需要把RSAPrivateKey与RSAPublicKey写入文件或其他方式保存进行解密
按照你的要求编写简单加密(把每个英文字母向后移动3个字母)的Java程序如下:
import java.io.BufferedReaderimport java.io.BufferedWriter
import java.io.File
import java.io.FileReader
import java.io.FileWriter
public class CA {
public static void main(String[] args) {
BufferedReader br = null//定义一个缓存读取类
BufferedWriter bw = null//定义一个缓存写入类
try {
File f_Source=new File("source.txt")//原始文件
br=new BufferedReader(new FileReader(f_Source))//从字符输入流中读取文本,缓冲各个字符,从而实现字符、数组和行的高效读取。
File f_Target=new File("target.txt")//目标文件
bw=new BufferedWriter(new FileWriter(f_Target))//将文本写入字符输出流,缓冲各个字符,从而提供单个字符、数组和字符串的高效写入。
String s=""//定义一个临时变量,临时存储每行的文本
while((s=br.readLine())!=null){//读取文件中的每一行并存入临时变量s,直到文件末尾.
s=s.trim()//去掉每行前后的空格
String str=""//定义一个临时变量,准备写入的一行文本
for(int i=0i<s.length()i++){//遍历这一行文本的每个字符
char ch=s.charAt(i)//把这一行文本的中一个字符赋给一个字符变量
if(Character.isLetter(ch)){//如果这个字符是字母
if(Character.isUpperCase(ch)){//如果这个字符是大写字母
if(ch-'A'+3>25){//如果这个字符向后移3个字符大于Z
ch=(char) ('A'+(ch-'A'+3)%26)//这个字符从A向后移相应的字符
}else{//如果这个字符向后移3个字符不大于Z
ch=(char) (ch+3)//这个字符向后移3个字符
}
}
if(Character.isLowerCase(ch)){//如果这个字符是小写字母
if(ch-'a'+3>25){//如果这个字符向后移3个字符大于z
ch=(char) ('a'+(ch-'a'+3)%26)//这个字符从a向后移相应的字符
}else{//如果这个字符向后移3个字符不大于z
ch=(char) (ch+3)//这个字符向后移3个字符
}
}
}
str=str+ch//把每个加密或没加密的字符重新拼装成一行字符串
}
bw.write(str)//把这一行字符串写入目标文件
bw.newLine()//目标文件写入换行符
}
} catch (Exception e) {//捕获异常
e.printStackTrace()//输出异常信息
}finally{
try {
bw.close()//关闭写入缓冲流
br.close()//关闭读取缓冲流
} catch (Exception e) {//捕获异常
e.printStackTrace()//输出异常信息
}
}
System.out.println("文件加密完毕!")
}
}
运行结果:
source.txt文件内容
we are the world.
abc xyz.
target.txt文件加密内容
zh duh wkh zruog.
def abc.
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)