首页>代码>jsp网上数字签名管理系统>/rsa/src/com/util/ABchatRSA.java
package com.util;

import java.security.*;
import javax.crypto.*;
import javax.crypto.spec.*;
/**
*
* @author GuoXiaoHe
*/
public class ABchatRSA {
    
    /** Creates a new instance of ABchatRSA */
    /*构建需要的函数变量*/
    private KeyPairGenerator kePaGen=null;                //秘密钥匙生成器;
    private KeyPair          keyPair=null;                //钥匙对,公尺 和米尺;
    private PublicKey        publicKey=null;              //共匙;
    private PrivateKey       privateKey=null;             //密匙;
    private int             keySize    =512;               //密匙长
    
    public ABchatRSA(int keysize) {
        this.keySize= keysize;
        try{
        this.kePaGen= KeyPairGenerator.getInstance("RSA"); //
        this.kePaGen.initialize(this.keySize);   
        //
        this.keyPair=this.kePaGen.genKeyPair();
        this.privateKey=this.keyPair.getPrivate();
        this.publicKey=this.keyPair.getPublic();
         //this.abcharRsaCipher=Cipher.getInstance("RSA/ECB/PKCS1Padding");
        }catch( Exception err){
            err.printStackTrace();
        }
       
    }
    public PublicKey getPublicKey()
    {
        return this.publicKey;
    }
    public PrivateKey getPrivateKey()
    {
        return this.privateKey;
    }
    public static String encripyRSA(String platxt,PublicKey publickey)
    {
        String cipherStr=null;                              //返回的加密后的字符串;      
        byte[]plainByte=null;                              //获得明文的byte数组; 
        byte[]cipherByte;                                    //产生秘闻的byte数组;                          
        Cipher cipher =null;
        try{          
        plainByte=platxt.getBytes("ISO-8859-1"); 
        cipher=Cipher.getInstance("RSA/ECB/PKCS1Padding");
        cipher.init(Cipher.ENCRYPT_MODE,publickey); 
        cipherByte=cipher.doFinal(plainByte);        
        cipherStr=new String(cipherByte,"ISO-8859-1"); 
         }catch(Exception err){
            err.printStackTrace();
            System.out.println("error in en: "+err.toString());
        }
        return cipherStr;
    }
    
    public static String decripyRSA(String cphtxt,PrivateKey privateKey)
    {
         byte[] cipherByte =null;                             //获得秘闻的byte数组;        
         byte[] plainByte   =null;                             //解密后的明文数组;
         String   plainStr    =null;                            //解密后的明文数组;
         Cipher   cipher      =null;                            //加密用;
        try{
            cipherByte       =cphtxt.getBytes("ISO-8859-1");    //统一使用该种编码方式;
            cipher =Cipher.getInstance("RSA/ECB/PKCS1Padding");
            cipher.init(Cipher.DECRYPT_MODE,privateKey);
            plainByte=cipher.doFinal(cipherByte);
            plainStr=new String(plainByte,"ISO-8859-1");         
        }catch(Exception err)
        {
            err.printStackTrace();
        }
        return plainStr;
    }
    
    
    public static void main(String []args)
    {
        ABchatRSA arsa=new ABchatRSA(512);
        String str="afdadf";
        
        try{
      
        String en=ABchatRSA.encripyRSA(str,arsa.getPublicKey()); 
        System.out.println("en="+en);
        String de=ABchatRSA.decripyRSA(en,arsa.getPrivateKey());
        System.out.println(de);   
      
        }catch(Exception e){
        	e.printStackTrace();
        }
    }
}
最近下载更多
except I  LV2 2023年11月5日
2036495585  LV9 2023年9月25日
yymmdm  LV6 2022年8月30日
novice2  LV1 2022年8月28日
bai620123  LV16 2022年8月9日
liu2022  LV14 2022年7月31日
dasdascccf  LV10 2022年6月16日
小丶无奈  LV10 2022年6月14日
wanglinddad  LV54 2022年3月13日
lijun1314  LV6 2021年9月13日
最近浏览更多
吞吞吐吐她  LV5 3月28日
17693282606  LV11 2023年12月20日
1222222222222222222  LV2 2023年12月17日
2749263182 2023年11月12日
暂无贡献等级
except I  LV2 2023年11月5日
pangzhihui  LV12 2023年10月23日
微信网友_6680567232876544  LV8 2023年10月10日
2036495585  LV9 2023年9月25日
1WQAQW1  LV2 2023年6月8日
jsp10086 2023年6月2日
暂无贡献等级
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友