RSA加密算法(java版)
算法简介
RSA加密算法是⼀种⾮对称加密算法,在公开密钥加密和电⼦商业中RSA被⼴泛使⽤。
算法实现
1、RSAUtils.java
dec;
pto.BadPaddingException;
pto.Cipher;
pto.IllegalBlockSizeException;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.security.*;
import java.security.interfaces.RSAKey;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
/**
* @author Ricky Fung
*/
public abstract class RSAUtils {
private static final String RSA_ALGORITHM = "RSA";
/
/--------------------------------------------
/** 公钥加密 **/
public static byte[] encryptWithPublicKey(final byte[] data, RSAPublicKey publicKey) {
return doRSA(publicKey, Cipher.ENCRYPT_MODE, data);
}java源代码加密
/** 公钥解密 **/
public static byte[] decryptByPublicKey(final byte[] data, RSAPublicKey publicKey) {
return doRSA(publicKey, Cipher.DECRYPT_MODE, data);
}
//---------------------------------------------------
/** 私钥解密 **/
public static byte[] decryptWithPrivateKey(byte[] data, RSAPrivateKey privateKey) {
return doRSA(privateKey, Cipher.DECRYPT_MODE, data);
}
/** 私钥加密 **/
public static byte[] encryptWithPrivateKey(byte[] data, RSAPrivateKey privateKey){
return doRSA(privateKey, Cipher.ENCRYPT_MODE, data);
}
private static byte[] doRSA(RSAKey key, int mode, byte[] data) {
try{
Cipher cipher = Instance(RSA_ALGORITHM);
cipher.init(mode, (Key) key);
return doRSA(cipher, mode, data, Modulus().bitLength());
}catch(Exception e){
throw new IllegalArgumentException(e);
}
}
private static byte[] doRSA(Cipher cipher, int mode, byte[] data, int keySize) throws IOException, BadPaddingException, IllegalBlockSizeException {        int maxBlock;
if(mode == Cipher.DECRYPT_MODE){
maxBlock = keySize / 8;
}else{
maxBlock = keySize / 8 - 11;
}
int length = data.length;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
int offset = 0;
byte[] buf;
int i = 0;
// 对数据分段加密
while (length - offset > 0) {
if (length - offset > maxBlock) {
buf = cipher.doFinal(data, offset, maxBlock);
} else {
buf = cipher.doFinal(data, offset, length - offset);
}
baos.write(buf, 0, buf.length);
i++;
offset = i * maxBlock;
}
baos.flush();
ByteArray();
} finally {
baos.close();
}
}
//------------------------------
/**
* generate publicKey
* @param publicKey
* @return
*/
public static RSAPublicKey generatePublicKey(String publicKey) {
try {
KeyFactory keyFactory = Instance(RSA_ALGORITHM);
X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(Base64Utils.decode(publicKey));
RSAPublicKey key = (RSAPublicKey) atePublic(x509KeySpec);
return key;
} catch (Exception e) {
throw new IllegalArgumentException("generate publicKey caught error", e);
}
}
/**
* generate privateKey
* @param privateKey
* @return
*/
public static RSAPrivateKey generatePrivateKey(String privateKey) {
try {
KeyFactory keyFactory = Instance(RSA_ALGORITHM);
PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(Base64Utils.decode(privateKey));            RSAPrivateKey key = (RSAPrivateKey) atePrivate(pkcs8KeySpec);
return key;
} catch (Exception e) {
throw new IllegalArgumentException("generate privateKey caught error", e);
}
}
//------------------------------
public static String getPublicKey(PublicKey publicKey) {
Encoded());
}
public static String getPrivateKey(PrivateKey privateKey) {
Encoded());
}
//------------------------------
/**

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。