Java实现RSA算法的方法详解
本文实例讲述了java实现rsa算法的方法。分享给大家供大家参考,具体如下:
一 介绍
唯一广泛接受并实现
用于数据加密和数字签名
公钥加密、私钥解密
私钥加密、公钥解密
二 rsa参数说明
三 实现
package com.imooc.security.rsa; import java.security.keyfactory; import java.security.keypair; import java.security.keypairgenerator; import java.security.privatekey; import java.security.publickey; import java.security.interfaces.rsaprivatekey; import java.security.interfaces.rsapublickey; import java.security.spec.pkcs8encodedkeyspec; import java.security.spec.x509encodedkeyspec; import javax.crypto.cipher; import org.apache.commons.codec.binary.base64; public class imoocrsa { private static string src = "cakin24 security rsa"; public static void main(string[] args) { jdkrsa(); } public static void jdkrsa() { try { //1.初始化密钥 keypairgenerator keypairgenerator = keypairgenerator.getinstance("rsa"); keypairgenerator.initialize(512); keypair keypair = keypairgenerator.generatekeypair(); rsapublickey rsapublickey = (rsapublickey)keypair.getpublic(); rsaprivatekey rsaprivatekey = (rsaprivatekey)keypair.getprivate(); system.out.println("public key : " + base64.encodebase64string(rsapublickey.getencoded())); system.out.println("private key : " + base64.encodebase64string(rsaprivatekey.getencoded())); //2.私钥加密、公钥解密——加密 pkcs8encodedkeyspec pkcs8encodedkeyspec = new pkcs8encodedkeyspec(rsaprivatekey.getencoded()); keyfactory keyfactory = keyfactory.getinstance("rsa"); privatekey privatekey = keyfactory.generateprivate(pkcs8encodedkeyspec); cipher cipher = cipher.getinstance("rsa"); cipher.init(cipher.encrypt_mode, privatekey); byte[] result = cipher.dofinal(src.getbytes()); system.out.println("私钥加密、公钥解密——加密 : " + base64.encodebase64string(result)); //3.私钥加密、公钥解密——解密 x509encodedkeyspec x509encodedkeyspec = new x509encodedkeyspec(rsapublickey.getencoded()); keyfactory = keyfactory.getinstance("rsa"); publickey publickey = keyfactory.generatepublic(x509encodedkeyspec); cipher = cipher.getinstance("rsa"); cipher.init(cipher.decrypt_mode, publickey); result = cipher.dofinal(result); system.out.println("私钥加密、公钥解密——解密:" + new string(result)); //4.公钥加密、私钥解密——加密 x509encodedkeyspec = new x509encodedkeyspec(rsapublickey.getencoded()); keyfactory = keyfactory.getinstance("rsa"); publickey = keyfactory.generatepublic(x509encodedkeyspec); cipher = cipher.getinstance("rsa"); cipher.init(cipher.encrypt_mode, publickey); result = cipher.dofinal(src.getbytes()); system.out.println("公钥加密、私钥解密——加密 : " + base64.encodebase64string(result)); //5.公钥加密、私钥解密——解密 pkcs8encodedkeyspec = new pkcs8encodedkeyspec(rsaprivatekey.getencoded()); keyfactory = keyfactory.getinstance("rsa"); privatekey = keyfactory.generateprivate(pkcs8encodedkeyspec); cipher = cipher.getinstance("rsa"); cipher.init(cipher.decrypt_mode, privatekey); result = cipher.dofinal(result); system.out.println("公钥加密、私钥解密——解密:" + new string(result)); } catch (exception e) { e.printstacktrace(); } } }
四 实现效果
public key : mfwwdqyjkozihvcnaqebbqadswawsajbajcbeob97idkkirbmx3moy5e4erwh0uvc2bcnly1rdo0lz8ibr1bl1rjxwkhv7u0aso/5dblnnngbqrtsjlscpmcaweaaq==
private key : miibvaibadanbgkqhkig9w0baqefaascat4wgge6ageaakealwf45v3sh2ssksgbhcw5jl7h5hchs68lyfw2vjwsojsvnyjthvuxveldaqe/ttqbi7/kmgweecztbg2wmwwi8widaqabakadskprsl+ew3s2n+cemizxfyyb0xhs1d84qapafpixkunvwl0a4ovrwsnwt4mejatwvtufnvtxizczdx+q5dbbaiea9tzzymgru+3mdlax0icf+niqwvlqyvedka4ksx55gvuciqcdoex6mqgrp78aqjykweogwliszju5fn/lfvkzrcgbjwihamvbblzzaykhy0ikw75kd/lksyouty+20bap+midrqgzaia6r36eerkzqubtcl8lxypb5f79htxd5dcvnib/zgp0uwigwtxi7ixhjycsnomsjdu1j3du9kqquw/eohxrk/oguye=
私钥加密、公钥解密——加密 : vjkfsoivelvkes5rprhsjk9tdtzohdells7yluidcyee7dkucm9srj8kwadynhi4m0olafjhk6447hp7ia8x7a==
私钥加密、公钥解密——解密:cakin24 security rsa
公钥加密、私钥解密——加密 : gawl73uxhto+efkkpmfmdhtk0vh7hb8n+30l1hp8aamiagd21h2x/q/ns+sogsoxnovunzasgzenmzzcjb4vea==
公钥加密、私钥解密——解密:cakin24 security rsa
五 应用场景
ps:关于加密解密感兴趣的朋友还可以参考本站在线工具:
在线rsa加密/解密工具:
文字在线加密解密工具(包含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程序设计有所帮助。
上一篇: java实现图像转码为字符画的方法
下一篇: 微信小程序 动态传参实例详解