rsa java 加密 解密算法,java加密算法分享(rsa解密、对称加密、md5加密)

import java.io.UnsupportedEncodingException;

import java.security.InvalidKeyException;

import java.security.MessageDigest;

import java.security.NoSuchAlgorithmException;

import java.security.PrivateKey;

import java.security.PublicKey;

import java.security.SecureRandom;

import javax.crypto.BadPaddingException;

import javax.crypto.Cipher;

import javax.crypto.IllegalBlockSizeException;

import javax.crypto.KeyGenerator;

import javax.crypto.NoSuchPaddingException;

import javax.crypto.SecretKey;

import com.sun.mail.util.BASE64DecoderStream;

import com.sun.mail.util.BASE64EncoderStream;

public class util {

/**

* 传入名文和公钥钥对数据进行RSA解密

*
返回值:String

*
@param src

*
@param pubkey

*
@return

*/

public static String rsaEncoding(String src,PublicKey pubkey){

try {

Cipher cip = Cipher.getInstance("RSA");

cip.init(cip.ENCRYPT_MODE, pubkey);

byte[] by = cip.doFinal(src.getBytes());

return new String(BASE64EncoderStream.encode(by));

} catch (NoSuchAlgorithmException e) {

throw new RuntimeException(e);

} catch (NoSuchPaddingException e) {

throw new RuntimeException(e);

} catch (InvalidKeyException e) {

throw new RuntimeException(e);

} catch (IllegalBlockSizeException e) {

throw new RuntimeException(e);

} catch (BadPaddingException e) {

throw new RuntimeException(e);

}

}

/**

* 传入RSA密文和私钥对数据进行解密

*
返回值:String

*
@param sec

*
@param privkey

*
@return

*/

public static String rsaDeEncoding(String sec,PrivateKey privkey){

try {

Cipher cip = Cipher.getInstance("RSA");

cip.init(cip.DECRYPT_MODE, privkey);

http://www.cppcns.comring doubKeyEncoding(String src,String keysrc,String method) {

SecretKey key;

try {

//生成密钥

KeyGenerator kg =  KeyGenerator.getInstance(method);

//初始化此密钥生成器。

kg.ihttp://www.cppcns.comnit(new SecureRandom(keysrc.getBytes("utf-8")));

key = kg.generateKey();

//加密

Cipher ciph =  Cipher.getInstance(method);

ciph.init(Cipher.ENCRYPT_MODE, key);

ciph.update(src.getBytes("utf-8"));

//使用64进行编码,一避免出现丢数据情景

byte[] by = BASE64EncoderStream.encode(ciph.doFinal());

return new String(by);

} catch (NoSuchAlgorithmException e) {

throw new RuntimeException(e);

} catch (NoSuchPaddingException e) {

throw new RuntimeException(e);

} catch (InvalidKeyException e) {

throw new RuntimeException(e);

} catch (IllegalBlockSizeException e) {

throw new RuntimeException(e);

} catch (BadPaddingException e) {

throw new RuntimeException(e);

} catch (UnsupportedEncodingException e) {

throw new RuntimeException(e);

}

}

/**

* 传入字符串、密钥、加密方式,并解密字符串(对称加密解密密),支持:DES、AES、DESede(3DES)

*
生成时间:2014年5月2日  下午1:12:13

*
返回值:String 密钥原文

*
@param sec

*
@param key

*
@param method(DES、AES、DESede)

*
@return

*/

public static String doubKeyDencoding(String sec,String keysrc,String method) {

SecretKey key;

try {

//生成密钥

KeyGenerator kg =  KeyGenerator.getInstance(method);

//初始化此密钥生成器。

kg.init(new SecureRandom(keysrc.getBytes("utf-8")));

key = kg.generateKey();

//加密

Cipher ciph =  Cipher.getInstance(method);

ciph.init(ciph.DECRYPT_MODE, key);

//使用64进行解码,一避免出现丢数据情景

byte[] by = BASE64DecoderStream.decode(sec.getBytes());

ciph.update(by);

return new String(ciph.doFinal());

} catch (NoSuchAlgorithmException e) {

throw new RuntimeException(e);

} catch (NoSuchPaddingException e) {

throw new RuntimeException(e);

} catch (InvalidKeyException e) {

throw new RuntimeException(e);

} catch (IllegalBlockSizeException e) {

throw new RuntimeException(e);

} catch (BadPaddingException e) {

throw new RuntimeException(e);

} catch (UnsupportedEncodingException e) {

throw new RuntimeException(e);

}

}

/**

* 单向信息加密(信息摘要),支持:md5、md2、SHA(SHA-1,SHA1)、SHA-256、SHA-384、SHA-512,

*
返回值:String         加密后的密文

*
@param src     传入加密字符串(明文)

*
@param method  指定算法(md5、md2、SHA(SHA-1,SHAfBBbH1)、SHA-256、SHA-384、SHA-512)

*
@return

*/

public static String ecodingPasswd(String src,String method){编程客栈

try {

//信息摘要算法

MessageDigest md5 = MessageDigest.getInstance(method);

md5.update(src.getBytes());

byte[] encoding = md5.digest();

//使用64进行编码,一避免出现丢数据情景

return new String(BASE64EncoderStream.encode(encoding));

} catch (NoSuchAlgorithmException e) {

throw new RuntimeException(e+"加密失败!!");

}

}

}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值