RSA非对称加密和解密方法

来源:互联网 发布:大数据变革哪些方面 编辑:程序博客网 时间:2024/06/05 00:50

RSA公匙和密匙生成方法

package org.markerking;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
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;

public abstract class RSAKey
{

    
/**
     * 生成2048位的RSA公匙和私匙
     
*/

    
public static void generator(
            String privateKeyFileName) 
throws Exception
    
{
        KeyPairGenerator kpg 
= KeyPairGenerator.getInstance("RSA");
        kpg.initialize(
2048);
        KeyPair kp 
= kpg.generateKeyPair();
        PublicKey puk 
= kp.getPublic();
        PrivateKey prk 
= kp.getPrivate();
        FileOutputStream fos1 
= new FileOutputStream("publicRSAKey.dat");
        FileOutputStream fos2 
= new FileOutputStream(privateKeyFileName);
        ObjectOutputStream oos1 
= new ObjectOutputStream(fos1);
        ObjectOutputStream oos2 
= new ObjectOutputStream(fos2);
        oos1.writeObject(puk);
        oos2.writeObject(prk);
        oos1.close();
        oos2.close();
        fos1.close();
        fos2.close();
        System.out.println(
"公匙生成成功!公匙文件为publicRSAKey.dat");
        System.out.println(
"私匙生成成功!私匙文件为" + privateKeyFileName);
    }


    
/**
     * 获取已经生成的公匙
     * 
     * 
@return RSAPublicKey
     
*/

    
protected static RSAPublicKey getPublicKey(String publicKeyFileName)
            
throws Exception
    
{
        FileInputStream fis 
= new FileInputStream(publicKeyFileName);
        ObjectInputStream ois 
= new ObjectInputStream(fis);
        RSAPublicKey rsapuk 
= (RSAPublicKey) ois.readObject();
        
return rsapuk;
    }


    
/**
     * 获取已经生成的私匙
     * 
     * 
@return RSAPrivateKey
     
*/

    
protected static RSAPrivateKey getPrivateKey(String privateKeyFileName)
            
throws Exception
    
{
        FileInputStream fis 
= new FileInputStream(privateKeyFileName);
        ObjectInputStream ois 
= new ObjectInputStream(fis);
        RSAPrivateKey psaprk 
= (RSAPrivateKey) ois.readObject();
        
return psaprk;
    }


}

调用方法

package org.markerking;

import java.math.BigInteger;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;

/**
 * RSA非对称式加密与解密
 * 
 * 
@author MarkerKing
 * 
@version v1.0
 * @time 2007-2-23 21:19
 
*/

public class RSAUtil
{
    
/**
     * 加密方法
     * 
     * 
@param String
     *            需要加密的数据
     * 
@return String 加密后的数据
     
*/

    
public String S2RSA(String str) throws Exception
    
{
        
// 获得公匙
        RSAPublicKey rsapuk = RSAKey.getPublicKey("publicRSAKey.dat");
        
// 获得公匙参数e,n
        BigInteger e = rsapuk.getPublicExponent();
        BigInteger n 
= rsapuk.getModulus();
        
// 转换明文m
        byte[] btext = str.getBytes("GB2312");
        BigInteger m 
= new BigInteger(btext);
        
// 计算密文
        BigInteger c = m.modPow(e, n);
        
// 返回
        return c.toString();
    }


    
/**
     * 解密方法
     * 
     * 
@param String
     *            私匙文件
     * 
@param String
     *            加密后的字符串
     * 
@return String 解密后的字符串
     
*/

    
public String RSA2S(String fileName, String rsaStr) throws Exception
    
{
        
// 获取私匙
        RSAPrivateKey rsaprk = RSAKey.getPrivateKey(fileName);
        
// 获取私匙参数d,n
        BigInteger d = rsaprk.getPrivateExponent();
        BigInteger n 
= rsaprk.getModulus();
        
// 获取密文
        BigInteger c = new BigInteger(rsaStr);
        
// 解密计算
        BigInteger m = c.modPow(d, n);
        
// 转换成String
        byte[] ctext = m.toByteArray();
        
return new String(ctext);
    }


}

原创粉丝点击