RSAUtils加解密使用

来源:互联网 发布:哪个软件听音乐最好 编辑:程序博客网 时间:2024/06/02 09:01

RSA加密最使用公私密码加密,客户端使用公钥加密,服务器使用私钥加密


公钥是公开的,但是私钥是不公开的,所以就算有公钥的算法,也无法获取正确的私钥内容 

现在RSA可以被破解的位数为768,所以只要加密方法大于768就可以了

一般情况下是用1024加密的,所以安全级别还是比较高的

再加上混淆技术的话,级别会更高

缺点是加密效率不是太高

下面是RSA生成公私钥的JAVA实现,供参考:


package test;



import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;


import javax.crypto.Cipher;
import javax.crypto.NoSuchPaddingException;


import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;


public class RSAUtils {
private KeyPairGenerator kpg = null;
private KeyPair kp = null;
private PublicKey public_key = null;
private PrivateKey private_key = null;


/**
* 构造函数

* @param in
*            指定密匙长度(取值范围:512~2048)
* @throws NoSuchAlgorithmException
*             异常
* @throws NoSuchPaddingException 
* @throws NoSuchProviderException 
*/
public RSAUtils(int in, String address) throws NoSuchAlgorithmException,
FileNotFoundException, IOException, NoSuchProviderException, NoSuchPaddingException {

kpg = KeyPairGenerator.getInstance("RSA"); // 创建‘密匙对’生成器
kpg.initialize(in); // 指定密匙长度(取值范围:512~2048)
// kpg.initialize(in,new SecureRandom()); // 指定密匙长度(取值范围:512~2048)
kp = kpg.genKeyPair(); // 生成‘密匙对’,其中包含着一个公匙和一个私匙的信息
public_key = kp.getPublic(); // 获得公匙
private_key = kp.getPrivate(); // 获得私匙


BASE64Encoder b64 = new BASE64Encoder();
String pkStr = b64.encode(public_key.getEncoded());
System.out.println("pub_key:"  + pkStr);
String prStr = b64.encode(private_key.getEncoded());
System.out.println("pri_key:"  + prStr);


FileWriter fw = new FileWriter(address + "/private_key.dat");
fw.write(prStr);
fw.close();
FileWriter fw2 = new FileWriter(address + "/public_key.dat");
fw2.write(pkStr);
fw2.close();
}


/**
* 加密的方法
*/
private static String encrypt(String source) throws Exception {


/** 将文件中的公钥对象读出 */
FileReader fr = new FileReader("d://public_key.dat");
BufferedReader br = new BufferedReader(fr);// 建立BufferedReader对象,并实例化为br
String getPbKey = "";
while (true) {
String aLine = br.readLine();
if (aLine == null)
break;
getPbKey += aLine;
}
BASE64Decoder b64d = new BASE64Decoder();
byte[] keyByte = b64d.decodeBuffer(getPbKey);
X509EncodedKeySpec x509ek = new X509EncodedKeySpec(keyByte);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PublicKey publicKey = keyFactory.generatePublic(x509ek);


Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
byte[] sbt = source.getBytes();
byte[] epByte = cipher.doFinal(sbt);
BASE64Encoder encoder = new BASE64Encoder();
String epStr = encoder.encode(epByte);
return epStr;


}


/**
* 解密的方法
*/
public static String decrypt(String cryptograph) throws Exception {


FileReader fr = new FileReader("D://private_key.dat");
BufferedReader br = new BufferedReader(fr);// 建立BufferedReader对象,并实例化为


String getPvKey = "";
while (true) {
String aLine = br.readLine();
if (aLine == null)
break;
getPvKey += aLine;
}
BASE64Decoder b64d = new BASE64Decoder();
byte[] keyByte = b64d.decodeBuffer(getPvKey);
PKCS8EncodedKeySpec s8ek = new PKCS8EncodedKeySpec(keyByte);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PrivateKey privateKey = keyFactory.generatePrivate(s8ek);


/** 得到Cipher对象对已用公钥加密的数据进行RSA解密 */
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.DECRYPT_MODE, privateKey);
BASE64Decoder decoder = new BASE64Decoder();
byte[] b1 = decoder.decodeBuffer(cryptograph);
/** 执行解密操作 */
byte[] b = cipher.doFinal(b1);
return new String(b);
}


public static void main(String[] args) {
try {
new RSAUtils(1024, "D:/"); // 私匙和公匙保存到D盘下的文件中.
System.out.println("");
String getEptStr = encrypt("this is a test");
System.out.println("getEptStr:" + getEptStr);
String drpStr = decrypt(getEptStr);
System.out.println("drpStr:" + drpStr);
} catch (IOException ex) {
ex.printStackTrace();
} catch (NoSuchAlgorithmException ex) {
ex.printStackTrace();
} catch (Exception ex) {
ex.printStackTrace();
}


}
}
0 0