Java实现的RSA加密解密算法示例
本文实例讲述了Java实现的RSA加密解密算法。分享给大家供大家参考,具体如下:
importjava.awt.AlphaComposite;
importjava.awt.Color;
importjava.awt.Font;
importjava.awt.Graphics2D;
importjava.awt.Image;
importjava.awt.RenderingHints;
importjava.awt.image.BufferedImage;
importjava.io.File;
importjava.io.FileInputStream;
importjava.io.FileOutputStream;
importjava.io.ObjectInputStream;
importjava.io.ObjectOutputStream;
importjava.security.Key;
importjava.security.KeyPair;
importjava.security.KeyPairGenerator;
importjava.security.SecureRandom;
importjava.security.interfaces.RSAPrivateKey;
importjava.security.interfaces.RSAPublicKey;
importjavax.crypto.Cipher;
publicclassRSAUtils{
publicstaticStringmakekeyfile(Stringpubkeyfile,Stringprikeyfile){
Stringresult="生成公私钥文件失败";
try{
//KeyPairGenerator用于生成公私钥对,基于RSA算法生成对象
KeyPairGeneratorgen=KeyPairGenerator.getInstance("RSA");
//初始化密钥对生成器,密钥大小为1024位
gen.initialize(1024);
////生成强随机数
//SecureRandomrandom=newSecureRandom();
//gen.initialize(1024,random);
//生成一个密钥对,保存在pair中
KeyPairpair=gen.generateKeyPair();
//得到私钥
RSAPrivateKeypriKey=(RSAPrivateKey)pair.getPrivate();
//得到公钥
RSAPublicKeypubKey=(RSAPublicKey)pair.getPublic();
//生成私钥文件
ObjectOutputStreamos=newObjectOutputStream(newFileOutputStream(prikeyfile));
os.writeObject(priKey);
os.flush();
os.close();
//生成公钥文件
os=newObjectOutputStream(newFileOutputStream(pubkeyfile));
os.writeObject(pubKey);
os.flush();
os.close();
result="生成公钥文件【"+pubkeyfile+"】生成私钥文件【"+prikeyfile+"】";
}catch(Exceptione){
e.printStackTrace();
}
returnresult;
}
publicstaticvoidmain(String[]args){
try{
Stringpubfile="F:/images/pub.key";
Stringprifile="F:/images/pri.key";
Stringresult=null;
//result=makekeyfile(pubfile,prifile);
result=markPuPra(pubfile,prifile);
System.out.println(result);
}catch(Exceptione){
e.printStackTrace();
}
}
publicstaticStringmarkPuPra(Stringpubfile,Stringprifile){
Stringresults="加解密出错";
try{
ObjectInputStreamos=newObjectInputStream(newFileInputStream(pubfile));
RSAPublicKeypubkey=(RSAPublicKey)os.readObject();
os.close();
os=newObjectInputStream(newFileInputStream(prifile));
RSAPrivateKeyprikey=(RSAPrivateKey)os.readObject();
os.close();
Stringutf="UTF-8";
Stringmsg="##中国%%的)人@+_";
//使用公钥加密私钥解密
System.out.println("原文:"+msg);
byte[]puk=handleData(pubkey,msg.getBytes(utf),1);
System.out.println("加密后文件数据:"+newString(puk,utf));
byte[]dpuk=handleData(prikey,puk,0);
System.out.println("解密后文件数据:"+newString(dpuk,utf));
msg="jd#我0们的¥人+=#新";
//使用私钥加密公钥解密
System.out.println("原文:"+msg);
byte[]prk=handleData(prikey,msg.getBytes(utf),1);
System.out.println("加密后文件数据:"+newString(prk,utf));
byte[]dprk=handleData(pubkey,prk,0);
System.out.println("解密后文件数据:"+newString(dprk,utf));
results="加解密完成";
}catch(Exceptione){
e.printStackTrace();
}
returnresults;
}
/**
*
*@paramk
*@paramdata
*@paramencrypt1加密0解密
*@return
*@throwsException
*/
publicstaticbyte[]handleData(Keykey,byte[]data,inttype)throwsException{
if(key!=null){
Cipherci=Cipher.getInstance("RSA");
if(type==1){
ci.init(Cipher.ENCRYPT_MODE,key);
byte[]res=ci.doFinal(data);
returnres;
}
if(type==0){
ci.init(Cipher.DECRYPT_MODE,key);
byte[]res=ci.doFinal(data);
returnres;
}
}
returnnull;
}
}
PS:关于加密解密感兴趣的朋友还可以参考本站在线工具:
文字在线加密解密工具(包含AES、DES、RC4等):
http://tools.jb51.net/password/txt_encode
MD5在线加密工具:
http://tools.jb51.net/password/CreateMD5Password
在线散列/哈希算法加密工具:
http://tools.jb51.net/password/hash_encrypt
在线MD5/hash/SHA-1/SHA-2/SHA-256/SHA-512/SHA-3/RIPEMD-160加密工具:
http://tools.jb51.net/password/hash_md5_sha
在线sha1/sha224/sha256/sha384/sha512加密工具:
http://tools.jb51.net/password/sha_encode
更多关于java相关内容感兴趣的读者可查看本站专题:《Java数学运算技巧总结》、《Java数据结构与算法教程》、《Java字符与字符串操作技巧总结》、《Java操作DOM节点技巧总结》和《Java缓存操作技巧汇总》
希望本文所述对大家java程序设计有所帮助。