RSA 加密小例子

加密类:
package com.test.Utils;

import it.sauronsoftware.base64.Base64;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.security.KeyFactory;
import java.security.PublicKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.X509EncodedKeySpec;

import javax.crypto.Cipher;

public class RSAJiaMi {
public static String src = "hello world";
/**
* 加密
*/
public void pubENpriDE() {

try {
FileInputStream f = new FileInputStream("pubkey2.dat");
ObjectInputStream b = new ObjectInputStream(f);
RSAPublicKey rsaPublicKey = (RSAPublicKey) b.readObject();
// 2.公钥加密,私钥解密----加密
X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(
rsaPublicKey.getEncoded());
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PublicKey publicKey = keyFactory.generatePublic(x509EncodedKeySpec);
// 初始化加密
// Cipher类为加密和解密提供密码功能,通过getinstance实例化对象
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
// 加密字符串
byte[] result = cipher.doFinal(src.getBytes());
System.out.println("公钥加密,私钥解密----加密:"
+ new String(Base64.encode(result), "utf-8"));
File file = new File("encrypt.dat");
FileOutputStream fos = new FileOutputStream(file);
fos.write(result);
b.close();
f.close();
fos.close();
} catch (Exception e) {
e.printStackTrace();
}

}

}

解密类:
package com.test.Utils;

import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.ObjectInputStream;
import java.security.KeyFactory;
import java.security.PrivateKey;
import java.security.interfaces.RSAPrivateKey;
import java.security.spec.PKCS8EncodedKeySpec;

import javax.crypto.Cipher;

public class RSAJieMi {
/**
* 解密
*
* @throws Exception
*/
public String pubENpriDE2()  {
try{
File f = new File("encrypt.dat");
if (!f.exists()) {
throw new FileNotFoundException("encrypt.dat");
}

ByteArrayOutputStream bos = new ByteArrayOutputStream((int) f.length());
BufferedInputStream in = null;
in = new BufferedInputStream(new FileInputStream(f));
int buf_size = 1024;
byte[] buffer = new byte[buf_size];
int len = 0;
while (-1 != (len = in.read(buffer, 0, buf_size))) {
bos.write(buffer, 0, len);
}


KeyFactory keyFactory = KeyFactory.getInstance("RSA");
Cipher cipher = Cipher.getInstance("RSA");
// 3.公钥加密,私钥解密-----解密
FileInputStream f3 = new FileInputStream("privatekey2.dat");
ObjectInputStream b3 = new ObjectInputStream(f3);
RSAPrivateKey rsaPrivateKey = (RSAPrivateKey) b3.readObject();
PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(
rsaPrivateKey.getEncoded());
keyFactory = KeyFactory.getInstance("RSA");
PrivateKey privateKey = keyFactory.generatePrivate(pkcs8EncodedKeySpec);
// 初始化解密
cipher.init(Cipher.DECRYPT_MODE, privateKey);
// 解密字符串
byte[] result2 = cipher.doFinal(bos.toByteArray());
bos.close();
in.close();
f3.close();
b3.close();
System.out.println("公钥加密,私钥解密-----解密:" + new String(result2));
return new String(result2,"utf-8");
}catch(Exception e){
e.printStackTrace();
return null;
}

}
}
生成密钥类:
package com.test.Utils;

import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;

public class RSAPubPri {

public static void generateKey() {

try {
// 1.初始化秘钥
KeyPairGenerator keyPairGenerator = KeyPairGenerator
.getInstance("RSA");
// 秘钥长度
keyPairGenerator.initialize(2048);
// 初始化秘钥对
KeyPair keyPair = keyPairGenerator.generateKeyPair();
// 公钥
RSAPublicKey rsaPublicKey = (RSAPublicKey) keyPair.getPublic();
// 私钥
RSAPrivateKey rsaPrivateKey = (RSAPrivateKey) keyPair.getPrivate();
// 保存公钥
FileOutputStream f1 = new FileOutputStream("pubkey2.dat");
ObjectOutputStream b1 = new ObjectOutputStream(f1);
b1.writeObject(rsaPublicKey);
// 保存私钥
FileOutputStream f2 = new FileOutputStream("privatekey2.dat");
ObjectOutputStream b2 = new ObjectOutputStream(f2);
b2.writeObject(rsaPrivateKey);
f1.close();
b1.close();
f2.close();
b2.close();
} catch (Exception e) {
e.printStackTrace();
}

}

}
测试类:
package com.test.test;

import com.test.Utils.RSAJiaMi;
import com.test.Utils.RSAJieMi;
import com.test.Utils.RSAPubPri;



public class RSAtest {
public static void main(String[] args) { 
RSAPubPri pubPri = new RSAPubPri();
//生成密钥对
//pubPri.generateKey();
RSAJiaMi jiaMi = new RSAJiaMi();
//jiaMi.pubENpriDE();
RSAJieMi jieMi = new RSAJieMi();
jieMi.pubENpriDE2();
}
}

猜你喜欢

转载自201609032307.iteye.com/blog/2341180