jiaqidianbo
2015-09-23 09:38:43 +08:00
/**
* 解密算法
* cryptograph:密文
*/
public static String decrypt(String cryptograph) throws Exception{
/** 将文件中的私钥对象读出 */
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(PRIVATE_KEY_FILE));
Key key = (Key) ois.readObject();
/** 得到 Cipher 对象对已用公钥加密的数据进行 RSA 解密 */
Cipher cipher = Cipher.getInstance(ALGORITHM);
cipher.init(Cipher.DECRYPT_MODE, key);
BASE64Decoder decoder = new BASE64Decoder();
byte[] b1 = decoder.decodeBuffer(cryptograph);
/** 执行解密操作 */
byte[] b = cipher.doFinal(b1);
return new String(b);
}
public static void main(String[] args) throws Exception {
String source = "火车";//要加密的字符串
System.out.println("要加密的字符串:"+source);
String cryptograph = encrypt(source);//生成的密文
System.out.println("生成的密文:"+cryptograph+"==================");
String target = decrypt(cryptograph);//解密密文
System.out.println("解密密文:"+target+"------------------------");
}
}