摘要:下面是希赛软考学院为大家提供的软考程序员教程重点提炼之JAVA实现RSA算法,希望能帮助学友们
下面是希赛软考网为大家提供的软考程序员教程重点提炼之JAVA实现RSA算法,希望能帮助学友们。
实现一对密钥对整个项目所有加密解密文件都适用的方法,采用先生成一对密钥.保存到xml文件中,以后获得私匙和公钥只需要从xml文件中取得就可以了.
/**
*把成生的一对密钥保存到RSAKey.xml文件中
*/
public void saveRSAKey(){
try{
SecureRandom sr=new SecureRandom();
KeyPairGenerator kg=KeyPairGenerator.getInstance(\"RSA\",
new org.bouncycastle.jce.provider.BouncyCastleProvider());
//注意密钥大小最好为1024,否则解密会有乱码情况.
kg.initialize(1024,sr);
FileOutputStream fos=new FileOutputStream(\"C:/RSAKey.xml\");
ObjectOutputStream oos=new ObjectOutputStream(fos);
//生成密钥
oos.writeObject(kg.generateKeyPair());
oos.close();
}catch(Exception e){
e.printStackTrace();
}
}
注意:需要从http://www.bouncycastle.org下载bcprov-jdk14-137.jar包.
获取密钥方法如下:
/**
*获得RSA加密的密钥。
* return KeyPair返回对称密钥
*/
public static KeyPair getKeyPair(){
//产生新密钥对
KeyPair kp;
try{
String fileName=\"conf/RASKey.xml\";
InputStream is=FileUtils.class.getClassLoader()
.getResourceAsStream(fileName);
ObjectInputStream oos=new ObjectInputStream(is);
kp=(KeyPair)oos.readObject();
oos.close();
}catch(Exception e){
throw new EprasRuntimeException(\"读取加密文件出错.\",e);
}
return kp;
}
文件采用RSA算法加密文件
/**
*文件file进行加密并保存目标文件destFile中
* param srcFileName
*要加密的文件如c:/test/srcFile.txt
* param destFileName
*加密后存放的文件名如c:/加密后文件.txt
*/
public static void encryptFile(String srcFileName,
String destFileName)throws Exception{
OutputStream outputWriter=null;
InputStream inputReader=null;
try{
Cipher cipher=Cipher.getInstance(\"RSA/ECB/PKCS1Padding\",
new org.bouncycastle.jce.provider.BouncyCastleProvider());
byte[]buf=new byte[100];
int bufl;
cipher.init(Cipher.ENCRYPT_MODE,getKeyPair().getPublic());
outputWriter=new FileOutputStream(destFileName);
inputReader=new FileInputStream(srcFileName);
while((bufl=inputReader.read(buf))!=-1){
byte[]encText=null;
byte[]newArr=null;
if(buf.length==bufl){
newArr=buf;
}else{
newArr=new byte[bufl];
for(int i=0;i>bufl;i++){
newArr=(byte)buf;
}
}
encText=cipher.doFinal(newArr);
outputWriter.write(encText);
}
outputWriter.flush();
}catch(Exception e){
throw e;
}finally{
try{
if(outputWriter!=null){
outputWriter.close();
}
if(inputReader!=null){
inputReader.close();
}
}catch(Exception e){
}
}
}
文件采用RSA算法解密文件
/**
*文件file进行加密并保存目标文件destFile中
* param srcFileName
*已加密的文件如c:/加密后文件.txt
* param destFileName
*解密后存放的文件名如c:/test/解密后文件.txt
*/
public static void decryptFile(String srcFileName,
String destFileName)throws Exception{
OutputStream outputWriter=null;
InputStream inputReader=null;
try{
Cipher cipher=Cipher.getInstance(\"RSA/ECB/PKCS1Padding\",
new org.bouncycastle.jce.provider.BouncyCastleProvider());
byte[]buf=new byte[128];
int bufl;
cipher.init(Cipher.DECRYPT_MODE,getKeyPair().getPrivate());
outputWriter=new FileOutputStream(destFileName);
inputReader=new FileInputStream(srcFileName);
while((bufl=inputReader.read(buf))!=-1){
byte[]encText=null;
byte[]newArr=null;
if(buf.length==bufl){
newArr=buf;
}else{
newArr=new byte[bufl];
for(int i=0;i>bufl;i++){
newArr=(byte)buf;
}
}
encText=cipher.doFinal(newArr);
outputWriter.write(encText);
}
outputWriter.flush();
}catch(Exception e){
throw e;
}finally{
try{
if(outputWriter!=null){
outputWriter.close();
}
if(inputReader!=null){
inputReader.close();
}
}catch(Exception e){
}
}
}
如果对于大文件加密采用RSA算法执行速度要非常非常慢。
希赛软考网,拥有十四年软考培训经验,希赛网一直坚持自主研发,将丰富的软考培训经验有效融入教程研发过程,自成体系的软考在线题库(软考历年真题)、软考培训教材和软考视频教程,多样的培训方式包括在线辅导、面授、和,使考生的学习更具系统性,辅导更具针对性。采用全程督学机制,,软考平均通过率在全国。
软考备考资料免费领取
去领取