国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > Java > 正文

Java實現的RSA加密解密算法示例

2024-07-13 10:17:01
字體:
來源:轉載
供稿:網友

本文實例講述了Java實現的RSA加密解密算法。分享給大家供大家參考,具體如下:

import java.awt.AlphaComposite;import java.awt.Color;import java.awt.Font;import java.awt.Graphics2D;import java.awt.Image;import java.awt.RenderingHints;import java.awt.image.BufferedImage;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.ObjectInputStream;import java.io.ObjectOutputStream;import java.security.Key;import java.security.KeyPair;import java.security.KeyPairGenerator;import java.security.SecureRandom;import java.security.interfaces.RSAPrivateKey;import java.security.interfaces.RSAPublicKey;import javax.crypto.Cipher;public class RSAUtils{ public static String makekeyfile(String pubkeyfile, String prikeyfile) {  String result = "生成公私鑰文件失敗";  try{    // KeyPairGenerator用于生成公私鑰對,基于RSA算法生成對象    KeyPairGenerator gen = KeyPairGenerator.getInstance("RSA");    // 初始化密鑰對生成器,密鑰大小為1024位    gen.initialize(1024);//    //生成強隨機數//    SecureRandom random = new SecureRandom();//    gen.initialize(1024,random);    // 生成一個密鑰對,保存在pair中    KeyPair pair = gen.generateKeyPair();    // 得到私鑰    RSAPrivateKey priKey = (RSAPrivateKey) pair.getPrivate();    // 得到公鑰    RSAPublicKey pubKey = (RSAPublicKey) pair.getPublic();    // 生成私鑰文件    ObjectOutputStream os = new ObjectOutputStream(new FileOutputStream(prikeyfile));    os.writeObject(priKey);    os.flush();    os.close();    //生成公鑰文件    os = new ObjectOutputStream(new FileOutputStream(pubkeyfile));    os.writeObject(pubKey);    os.flush();    os.close();    result = "生成公鑰文件【"+pubkeyfile+"】生成私鑰文件【"+prikeyfile+"】";  }catch(Exception e){  e.printStackTrace();  }  return result;  }  public static void main(String[] args) {  try{    String pubfile = "F:/images/pub.key";    String prifile = "F:/images/pri.key";    String result = null;    //result = makekeyfile(pubfile, prifile);    result = markPuPra(pubfile, prifile);    System.out.println(result);  }catch(Exception e){  e.printStackTrace();  }  }  public static String markPuPra(String pubfile,String prifile){  String results = "加解密出錯";  try{    ObjectInputStream os = new ObjectInputStream(new FileInputStream(pubfile));    RSAPublicKey pubkey = (RSAPublicKey) os.readObject();    os.close();    os = new ObjectInputStream(new FileInputStream(prifile));    RSAPrivateKey prikey = (RSAPrivateKey) os.readObject();    os.close();    String utf = "UTF-8";    String msg = "##中國%%的)人@+_";    // 使用公鑰加密私鑰解密    System.out.println("原文: " + msg);    byte[] puk = handleData(pubkey, msg.getBytes(utf), 1);    System.out.println("加密后文件數據: " + new String(puk, utf));    byte[] dpuk = handleData(prikey, puk, 0);    System.out.println("解密后文件數據: " + new String(dpuk, utf));    msg = "jd#我0們的¥人+=#新";    // 使用私鑰加密公鑰解密    System.out.println("原文: " + msg);    byte[] prk = handleData(prikey, msg.getBytes(utf), 1);    System.out.println("加密后文件數據: " + new String(prk, utf));    byte[] dprk = handleData(pubkey, prk, 0);    System.out.println("解密后文件數據: " + new String(dprk, utf));    results = "加解密完成";  }catch(Exception e){  e.printStackTrace();  }  return results;  }  /**   *   * @param k   * @param data   * @param encrypt 1 加密 0解密   * @return   * @throws Exception   */  public static byte[] handleData(Key key, byte[] data, int type) throws Exception {    if (key != null) {      Cipher ci = Cipher.getInstance("RSA");      if (type == 1) {        ci.init(Cipher.ENCRYPT_MODE, key);        byte[] res = ci.doFinal(data);        return res;      }      if (type == 0) {        ci.init(Cipher.DECRYPT_MODE, key);        byte[] res = ci.doFinal(data);        return res;      }    }    return null;  }}

希望本文所述對大家java程序設計有所幫助。


注:相關教程知識閱讀請移步到JAVA教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 清徐县| 邯郸县| 沙洋县| 石狮市| 天长市| 阿坝县| 宜城市| 灵山县| 兴海县| 大埔区| 象州县| 平邑县| 安泽县| 沅陵县| 三台县| 栾城县| 临安市| 蓝田县| 岚皋县| 防城港市| 曲周县| 武宣县| 新平| 静乐县| 许昌市| 石景山区| 榆树市| 宁城县| 临沧市| 四川省| 阜宁县| 寿阳县| 三亚市| 宜川县| 田林县| 南宁市| 平顺县| 仲巴县| 山阳县| 长泰县| 淮滨县|