rsa java服务器_Java Web项目RSA加密

/*** 生成公钥和私钥

*@throwsNoSuchAlgorithmException

**/

public static HashMap getKeys() throwsNoSuchAlgorithmException{

HashMap map = new HashMap();

KeyPairGenerator keyPairGen= KeyPairGenerator.getInstance("RSA");

keyPairGen.initialize(1024);

KeyPair keyPair=keyPairGen.generateKeyPair();

RSAPublicKey publicKey=(RSAPublicKey) keyPair.getPublic();

RSAPrivateKey privateKey=(RSAPrivateKey) keyPair.getPrivate();

map.put("public", publicKey);

map.put("private", privateKey);returnmap;

}/*** 使用模和指数生成RSA公钥

* 注意:【此代码用了默认补位方式,为RSA/None/PKCS1Padding,不同JDK默认的补位方式可能不同,如Android默认是RSA

* /None/NoPadding】

*

*@parammodulus

* 模

*@paramexponent

* 指数

*@return

*/

public staticRSAPublicKey getPublicKey(String modulus, String exponent) {try{

BigInteger b1= newBigInteger(modulus);

BigInteger b2= newBigInteger(exponent);

KeyFactory keyFactory= KeyFactory.getInstance("RSA");

RSAPublicKeySpec keySpec= newRSAPublicKeySpec(b1, b2);return(RSAPublicKey) keyFactory.generatePublic(keySpec);

}catch(Exception e) {

e.printStackTrace();return null;

}

}/*** 使用模和指数生成RSA私钥

* 注意:【此代码用了默认补位方式,为RSA/None/PKCS1Padding,不同JDK默认的补位方式可能不同,如Android默认是RSA

* /None/NoPadding】

*

*@parammodulus

* 模

*@paramexponent

* 指数

*@return

*/

public staticRSAPrivateKey getPrivateKey(String modulus, String exponent) {try{

BigInteger b1= newBigInteger(modulus);

BigInteger b2= newBigInteger(exponent);

KeyFactory keyFactory= KeyFactory.getInstance("RSA");

RSAPrivateKeySpec keySpec= newRSAPrivateKeySpec(b1, b2);return(RSAPrivateKey) keyFactory.generatePrivate(keySpec);

}catch(Exception e) {

e.printStackTrace();return null;

}

}/*** 公钥加密

*

*@paramdata

*@parampublicKey

*@return*@throwsException*/

public staticString encryptByPublicKey(String data, RSAPublicKey publicKey)throwsException {

Cipher cipher= Cipher.getInstance("RSA/ECB/NoPadding");

cipher.init(Cipher.ENCRYPT_MODE, publicKey);//模长

int key_len = publicKey.getModulus().bitLength() / 8;//加密数据长度 <= 模长-11

String[] datas = splitString(data, key_len - 11);

String mi= "";//如果明文长度大于模长-11则要分组加密

for(String s : datas) {

mi+=bcd2Str(cipher.doFinal(s.getBytes()));

}returnmi;

}/*** 私钥解密

*

*@paramdata

*@paramprivateKey

*@return*@throwsException*/

public staticString decryptByPrivateKey(String data, RSAPrivateKey privateKey)throwsException {

Cipher cipher= Cipher.getInstance("RSA/ECB/NoPadding");

cipher.init(Cipher.DECRYPT_MODE, privateKey);//模长

int key_len = privateKey.getModulus().bitLength() / 8;byte[] bytes =data.getBytes();byte[] bcd =ASCII_To_BCD(bytes, bytes.length);

System.err.println(bcd.length);//如果密文长度大于模长则要分组解密

String ming = "";byte[][] arrays =splitArray(bcd, key_len);for(byte[] arr : arrays){

ming+= newString(cipher.doFinal(arr));

}returnming;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值