论坛首页 入门技术论坛

Java RSA 加解密算法实现

浏览 3334 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2009-06-09   最后修改:2009-09-14
需要下载bcprov-jdk15-135.jar包
import java.math.BigInteger;
import java.security.Key;
import java.security.KeyFactory;
import java.security.KeyPairGenerator;
import java.security.SecureRandom;
import java.security.Security;
import java.security.spec.RSAPrivateCrtKeySpec;
import java.security.spec.RSAPublicKeySpec;

import javax.crypto.Cipher;

public class RSAEncrypt {
	
    private String nprime;
    private String eprime;
    private String dprime;
    private String pprime;
    private String qprime;
    private String dmp;
    private String dmq;
    private String qmp;
    
    public void setNprime(String nvalue){
    	this.nprime=nvalue;
    }
    
    public void setEprime(String evalue){
    	this.eprime=evalue;
    }
    public void setDprime(String dvalue){
    	this.dprime=dvalue;
    }
    public void setPprime(String pvalue){
    	this.pprime=pvalue;
    }
    public void setQprime(String qvalue){
    	this.qprime=qvalue;
    }
    public void setDmp(String dmpvalue){
    	this.dmp=dmpvalue;
    }
    public void setDmq(String dmqvalue){
    	this.dmq=dmqvalue;
    }
    public void setQmp(String qmpvalue){
    	this.qmp=qmpvalue;
    }
    

	public String BytesToHex(byte[] in){
	      String hs="";
	      String stmp="";
	      for (int n=0;n<in.length;n++) {
	          stmp=(Integer.toHexString(in[n] & 0XFF));
	          if (stmp.length()==1) hs=hs+"0"+stmp;
	          else hs=hs+stmp;
	      }
	      return hs.toLowerCase();
	 }
	
	public byte[] hexToBytes(String src)
    {
        int m = 0, n = 0;
        int l = src.length() / 2;
        String str;
        byte[] ret = new byte[l];
        
        for (int i = 0; i < l; i++)
        {
            m = i * 2 + 1;
            n = m + 1;
            str = src.substring(i * 2, i * 2 + 2);
            ret[i] = (byte) (0xff & Integer.parseInt(str, 16));
        }
        return ret;
    }
	
	public String Encrypt(String str){
		try{
			
		Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
		
		RSAPublicKeySpec pubKeySpec = new RSAPublicKeySpec(new BigInteger(nprime,16),new BigInteger(eprime,16));
		
		Cipher cipher = Cipher.getInstance("RSA");
		
		SecureRandom random = new SecureRandom();
		
	    KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA");

	    generator.initialize(512,random);
		
		KeyFactory fact = KeyFactory.getInstance("RSA");
		
		Key publicKey =(Key) fact.generatePublic(pubKeySpec);
		
		cipher.init(Cipher.ENCRYPT_MODE, publicKey,random);
		
	    byte[] cipherText = cipher.doFinal(str.getBytes());
	    
	    return BytesToHex(cipherText);
	    
		}
		catch(Exception ex){
			return "";
		}
		
	}
	
    public String de_Encrypt(String encryptText){
    	
    	try{
    		
    	 Cipher cipher = Cipher.getInstance("RSA");
         
         RSAPrivateCrtKeySpec privKeySpec = new RSAPrivateCrtKeySpec(
        		 new BigInteger(nprime,16), 
        		 new BigInteger(eprime,16),
        		 new BigInteger(dprime,16),
        		 new BigInteger(pprime,16),
        		 new BigInteger(qprime,16),
        		 new BigInteger(dmp,16),
        		 new BigInteger(dmq,16),
        		 new BigInteger(qmp,16));
         
         KeyFactory fact = KeyFactory.getInstance("RSA");
         
         Key privKey = (Key) fact.generatePrivate(privKeySpec);
             
         cipher.init(Cipher.DECRYPT_MODE, privKey);
           
         byte[] forumcookie = hexToBytes(encryptText);
         
         byte[] plainText = cipher.doFinal(forumcookie);
         
         return new String(plainText);
         
    	}
    	catch(Exception ex){
    		System.out.println(ex.toString());
    		return "";
    	}
    }
     
}
   发表时间:2009-11-10  
javax.crypto.Cipher类有这个功能
0 请登录后投票
论坛首页 入门技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics