温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Java实现的RSA加密解密算法示例

发布时间:2020-09-09 09:40:30 来源:脚本之家 阅读:212 作者:csdn_liuliu 栏目:编程语言

本文实例讲述了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;
  }
}

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程序设计有所帮助。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI