java安全架构____RSA加密解密

来源:互联网 发布:编程语言 使用范围 编辑:程序博客网 时间:2024/05/19 16:47
import java.io.ByteArrayOutputStream;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.ObjectInputStream;import java.io.ObjectOutputStream;import java.security.KeyFactory;import java.security.KeyPair;import java.security.KeyPairGenerator;import java.security.PrivateKey;import java.security.PublicKey;import java.security.Signature;import javax.crypto.Cipher;/** * @author God * RSA 参考IBM社区,参考WIKI百科 */public class RSA {//rsa算法    private static final String ALGORITHM_RSA = "RSA";    private static final String ALGORITHM_SIGNTURE = "MD5withRSA";    //明文加密涉及模和指数    private static final int MODEL_ENCRYPT_MAX=117;/** * 生成秘钥对写入到文件 * @return */public static boolean getKeyPairs() {try {//初始化秘钥管理器KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(ALGORITHM_RSA);keyPairGenerator.initialize(512);KeyPair keyPair = keyPairGenerator.genKeyPair();//获取秘钥对PublicKey publicKey = keyPair.getPublic();PrivateKey privateKey = keyPair.getPrivate();//直接写入公钥ObjectOutputStream out_pub = new ObjectOutputStream(new FileOutputStream("publicKey.key"));out_pub.writeObject(publicKey);out_pub.close();System.out.println("生成的公钥内容为_____:\n "+publicKey);//直接写入私钥ObjectOutputStream out_pri = new ObjectOutputStream(new FileOutputStream("privateKey.key"));out_pri.writeObject(privateKey);out_pri.close();System.out.println("生成的私钥内容为_____:\n "+privateKey);System.out.println("\n生成密钥对成功...");return true;} catch (java.lang.Exception e) {e.printStackTrace();return false;}}  /** * 使用私钥进行签名 * @return */public static byte[] SignatureData(String  info){byte[] signedbytes=null;try {//1.读取生成的私钥对明文进行签名ObjectInputStream in_pri = new ObjectInputStream(new FileInputStream("privateKey.key"));PrivateKey privateKey = (PrivateKey) in_pri.readObject();in_pri.close();//初始化签名 对明文开始签名Signature signature = Signature.getInstance(ALGORITHM_SIGNTURE);signature.initSign(privateKey);signature.update(info.getBytes());// 对信息的数字签名signedbytes = signature.sign();System.out.println("签名为_____:"+new String(signedbytes));} catch (Exception e) {e.printStackTrace();System.out.println("私钥签名失败....");}return signedbytes;}/** * 用公钥进行校验 * @return */public static boolean checkSignature(String info,byte[] signedbytes){try {//读取公钥ObjectInputStream in_pub=new ObjectInputStream(new FileInputStream("publicKey.key"));PublicKey publicKey = (PublicKey) in_pub.readObject();Signature signature = Signature.getInstance(ALGORITHM_SIGNTURE);signature.initVerify(publicKey);signature.update(info.getBytes());//签名信息校验if (signature.verify(signedbytes)) {System.out.println("签名的内容为____:" + info);System.out.println("签名文件校验正常....");return true;} else{System.out.println("签名校验失败");return false;}} catch (Exception e) {e.printStackTrace();return false;}}/** * 私钥加密数据 */public static byte[] PriEncode(String info){byte[] cipherBytes=null;try {KeyFactory keyFactory = KeyFactory.getInstance(ALGORITHM_RSA);//读取公钥ObjectInputStream in_pri = new ObjectInputStream(new FileInputStream("privateKey.key"));PrivateKey privateKey=(PrivateKey) in_pri.readObject();Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());cipher.init(Cipher.ENCRYPT_MODE, privateKey);//byte[] infoBytes = info.getBytes();int infolength = infoBytes.length;ByteArrayOutputStream out = new ByteArrayOutputStream();int offSet = 0;byte[] temp=null;int iter = 0;// 对数据分段加密while (infolength - offSet > 0) {if (infolength - offSet > MODEL_ENCRYPT_MAX) {temp = cipher.doFinal(infoBytes, offSet, MODEL_ENCRYPT_MAX);} else {temp = cipher.doFinal(infoBytes, offSet, infolength - offSet);}out.write(temp, 0, temp.length);iter++;offSet = iter * MODEL_ENCRYPT_MAX;}cipherBytes = out.toByteArray();out.close();} catch (Exception e) {e.printStackTrace();}return cipherBytes;}/** * 公钥解密数据 */public static byte[] PubDecode(byte[]cipherBytes){byte[]clearBytes=null;try {KeyFactory keyFactory = KeyFactory.getInstance(ALGORITHM_RSA);//读取公钥ObjectInputStream in_pub = new ObjectInputStream(new FileInputStream("publicKey.key"));PublicKey publicKey = (PublicKey) in_pub.readObject();Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());cipher.init(Cipher.DECRYPT_MODE, publicKey);int cipherBytesLength = cipherBytes.length;ByteArrayOutputStream out = new ByteArrayOutputStream();int offSet = 0;byte[] temp;int iter = 0;// 对数据分段解密while (cipherBytesLength - offSet > 0) {if (cipherBytesLength - offSet > MODEL_ENCRYPT_MAX) {temp = cipher.doFinal(cipherBytes, offSet,MODEL_ENCRYPT_MAX);} else {temp = cipher.doFinal(cipherBytes, offSet, cipherBytesLength- offSet);}out.write(temp, 0, temp.length);iter++;offSet = iter * MODEL_ENCRYPT_MAX;}clearBytes = out.toByteArray();out.close();} catch (Exception e) {e.printStackTrace();}return clearBytes;}public static void main(String[] args) {//生成RSA秘钥对getKeyPairs();//私钥进行签名String info="老司机开车了";SignatureData(info);//用公钥进行校验签名checkSignature(info, SignatureData(info));//私钥加密PriEncode(info);//公钥解密PubDecode(PriEncode(info));//解密的信息System.out.println(new String(PubDecode(PriEncode(info))));}}

//运行结果


//


//


1 0