欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2023-12-04 10:50:34
本文实例讲述了java实现的rsa加密解密算法。分享给大家供大家参考,具体如下: import java.awt.alphacomposite; import...

本文实例讲述了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等):

md5在线加密工具:
http://tools.jb51.net/password/createmd5password

在线散列/哈希算法加密工具:

在线md5/hash/sha-1/sha-2/sha-256/sha-512/sha-3/ripemd-160加密工具:

在线sha1/sha224/sha256/sha384/sha512加密工具:

更多关于java相关内容感兴趣的读者可查看本站专题:《java数学运算技巧总结》、《java数据结构与算法教程》、《java字符与字符串操作技巧总结》、《java操作dom节点技巧总结》和《java缓存操作技巧汇总

希望本文所述对大家java程序设计有所帮助。