软考程序员教程重点提炼之JAVA实现RSA算法

程序员 责任编辑:小狐狸 2016-08-11

添加老师微信

备考咨询

加我微信

摘要:下面是希赛软考学院为大家提供的软考程序员教程重点提炼之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算法执行速度要非常非常慢。


       返回目录:软考程序员教程重点提炼之算法实例汇总


       希赛软考网拥有十四年软考培训经验希赛网一直坚持自主研发,将丰富的软考培训经验有效融入教程研发过程,自成体系的软考在线题库软考历年真题)、软考培训教材软考视频教程多样的培训方式包括在线辅导面授使考生的学习更具系统性,辅导更具针对性。采用全程督学机制,软考平均通过率在全国。

更多资料
更多课程
更多真题
温馨提示:因考试政策、内容不断变化与调整,本网站提供的以上信息仅供参考,如有异议,请考生以权威部门公布的内容为准!

软考备考资料免费领取

去领取

!
咨询在线老师!