鑒於rsa加密的重要性和相關源代碼的匮乏
import javax
import java
import java
import java
import java
import java
import java
import java
import java
/**
* RSA 工具類
* 需要到下載bcprov
*
*/
public class RSAUtil {
/**
* 生成密鑰對
* @return KeyPair
* @throws EncryptException
*/
public static KeyPair generateKeyPair() throws EncryptException {
try {
KeyPairGenerator keyPairGen = KeyPairGenerator
new org
final int KEY_SIZE =
keyPairGen
KeyPair keyPair = keyPairGen
return keyPair;
} catch (Exception e) {
throw new EncryptException(e
}
}
/**
* 生成公鑰
* @param modulus
* @param publicExponent
* @return RSAPublicKey
* @throws EncryptException
*/
public static RSAPublicKey generateRSAPublicKey(byte[] modulus
KeyFactory keyFac = null;
try {
keyFac = KeyFactory
} catch (NoSuchAlgorithmException ex) {
throw new EncryptException(ex
}
RSAPublicKeySpec pubKeySpec = new RSAPublicKeySpec(new BigInteger(modulus)
try {
return (RSAPublicKey) keyFac
} catch (InvalidKeySpecException ex) {
throw new EncryptException(ex
}
}
/**
* 生成私鑰
* @param modulus
* @param privateExponent
* @return RSAPrivateKey
* @throws EncryptException
*/
public static RSAPrivateKey generateRSAPrivateKey(byte[] modulus
KeyFactory keyFac = null;
try {
keyFac = KeyFactory
} catch (NoSuchAlgorithmException ex) {
throw new EncryptException(ex
}
RSAPrivateKeySpec priKeySpec = new RSAPrivateKeySpec(new BigInteger(modulus)
try {
return (RSAPrivateKey) keyFac
} catch (InvalidKeySpecException ex) {
throw new EncryptException(ex
}
}
/**
* 加密
* @param key 加密的密鑰
* @param data 待加密的明文數據
* @return 加密後的數據
* @throws EncryptException
*/
public static byte[] encrypt(Key key
try {
Cipher cipher = Cipher
cipher
int blockSize = cipher
int outputSize = cipher
int leavedSize = data
int blocksSize = leavedSize !=
byte[] raw = new byte[outputSize * blocksSize];
int i =
while (data
if (data
cipher
else
cipher
//這裡面doUpdate方法不可用
i++;
}
return raw;
} catch (Exception e) {
throw new EncryptException(e
}
}
/**
* 解密
* @param key 解密的密鑰
* @param raw 已經加密的數據
* @return 解密後的明文
* @throws EncryptException
*/
public static byte[] decrypt(Key key
try {
Cipher cipher = Cipher
cipher
int blockSize = cipher
ByteArrayOutputStream bout = new ByteArrayOutputStream(
int j =
while (raw
bout
j++;
}
return bout
} catch (Exception e) {
throw new EncryptException(e
}
}
/**
*
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
File file = new File(
FileInputStream in = new FileInputStream(file);
ByteArrayOutputStream bout = new ByteArrayOutputStream();
byte[] tmpbuf = new byte[
int count =
while ((count = in
bout
tmpbuf = new byte[
}
in
byte[] orgData = bout
KeyPair keyPair = RSAUtil
RSAPublicKey pubKey = (RSAPublicKey) keyPair
RSAPrivateKey priKey = (RSAPrivateKey) keyPair
byte[] pubModBytes = pubKey
byte[] pubPubExpBytes = pubKey
byte[] priModBytes = priKey
byte[] priPriExpBytes = priKey
RSAPublicKey recoveryPubKey = RSAUtil
RSAPrivateKey recoveryPriKey = RSAUtil
byte[] raw = RSAUtil
file = new File(
OutputStream out = new FileOutputStream(file);
out
out
byte[] data = RSAUtil
file = new File(
out = new FileOutputStream(file);
out
out
out
}
}
加密可以用公鑰
From:http://tw.wingwit.com/Article/program/Java/gj/201311/27391.html