@ -1,5 +1,8 @@
package com.fr.privilege ;
package com.fr.privilege ;
import com.fr.third.org.bouncycastle.jce.provider.BouncyCastleProvider ;
import javax.crypto.Cipher ;
import java.io.ByteArrayOutputStream ;
import java.io.ByteArrayOutputStream ;
import java.io.FileInputStream ;
import java.io.FileInputStream ;
import java.io.FileOutputStream ;
import java.io.FileOutputStream ;
@ -19,8 +22,6 @@ import java.security.spec.InvalidKeySpecException;
import java.security.spec.RSAPrivateKeySpec ;
import java.security.spec.RSAPrivateKeySpec ;
import java.security.spec.RSAPublicKeySpec ;
import java.security.spec.RSAPublicKeySpec ;
import javax.crypto.Cipher ;
/ * *
/ * *
* RSA 工具类 。 提供加密 , 解密 , 生成密钥对等方法 。
* RSA 工具类 。 提供加密 , 解密 , 生成密钥对等方法 。
* 需要到http : //www.bouncycastle.org下载bcprov-jdk14-123.jar。
* 需要到http : //www.bouncycastle.org下载bcprov-jdk14-123.jar。
@ -36,7 +37,7 @@ public class RSAUtil {
public static KeyPair generateKeyPair ( ) throws Exception {
public static KeyPair generateKeyPair ( ) throws Exception {
try {
try {
KeyPairGenerator keyPairGen = KeyPairGenerator . getInstance ( "RSA" ,
KeyPairGenerator keyPairGen = KeyPairGenerator . getInstance ( "RSA" ,
new org . bouncycastle . jce . provider . BouncyCastleProvider ( ) ) ;
new BouncyCastleProvider ( ) ) ;
final int KEY_SIZE = 1024 ; // 没什么好说的了,这个值关系到块加密的大小,可以更改,但是不要太大,否则效率会低
final int KEY_SIZE = 1024 ; // 没什么好说的了,这个值关系到块加密的大小,可以更改,但是不要太大,否则效率会低
keyPairGen . initialize ( KEY_SIZE , new SecureRandom ( ) ) ;
keyPairGen . initialize ( KEY_SIZE , new SecureRandom ( ) ) ;
KeyPair keyPair = keyPairGen . generateKeyPair ( ) ;
KeyPair keyPair = keyPairGen . generateKeyPair ( ) ;
@ -79,7 +80,7 @@ public class RSAUtil {
KeyFactory keyFac = null ;
KeyFactory keyFac = null ;
try {
try {
keyFac = KeyFactory . getInstance ( "RSA" ,
keyFac = KeyFactory . getInstance ( "RSA" ,
new org . bouncycastle . jce . provider . BouncyCastleProvider ( ) ) ;
new BouncyCastleProvider ( ) ) ;
} catch ( NoSuchAlgorithmException ex ) {
} catch ( NoSuchAlgorithmException ex ) {
throw new Exception ( ex . getMessage ( ) ) ;
throw new Exception ( ex . getMessage ( ) ) ;
}
}
@ -106,7 +107,7 @@ public class RSAUtil {
KeyFactory keyFac = null ;
KeyFactory keyFac = null ;
try {
try {
keyFac = KeyFactory . getInstance ( "RSA" ,
keyFac = KeyFactory . getInstance ( "RSA" ,
new org . bouncycastle . jce . provider . BouncyCastleProvider ( ) ) ;
new BouncyCastleProvider ( ) ) ;
} catch ( NoSuchAlgorithmException ex ) {
} catch ( NoSuchAlgorithmException ex ) {
throw new Exception ( ex . getMessage ( ) ) ;
throw new Exception ( ex . getMessage ( ) ) ;
}
}
@ -133,7 +134,7 @@ public class RSAUtil {
public static byte [ ] encrypt ( PublicKey pk , byte [ ] data ) throws Exception {
public static byte [ ] encrypt ( PublicKey pk , byte [ ] data ) throws Exception {
try {
try {
Cipher cipher = Cipher . getInstance ( "RSA" ,
Cipher cipher = Cipher . getInstance ( "RSA" ,
new org . bouncycastle . jce . provider . BouncyCastleProvider ( ) ) ;
new BouncyCastleProvider ( ) ) ;
cipher . init ( Cipher . ENCRYPT_MODE , pk ) ;
cipher . init ( Cipher . ENCRYPT_MODE , pk ) ;
int blockSize = cipher . getBlockSize ( ) ; // 获得加密块大小,如:加密前数据为128个byte,而key_size=1024
int blockSize = cipher . getBlockSize ( ) ; // 获得加密块大小,如:加密前数据为128个byte,而key_size=1024
// 加密块大小为127
// 加密块大小为127
@ -177,7 +178,7 @@ public class RSAUtil {
public static byte [ ] decrypt ( PrivateKey pk , byte [ ] raw ) throws Exception {
public static byte [ ] decrypt ( PrivateKey pk , byte [ ] raw ) throws Exception {
try {
try {
Cipher cipher = Cipher . getInstance ( "RSA" ,
Cipher cipher = Cipher . getInstance ( "RSA" ,
new org . bouncycastle . jce . provider . BouncyCastleProvider ( ) ) ;
new BouncyCastleProvider ( ) ) ;
cipher . init ( cipher . DECRYPT_MODE , pk ) ;
cipher . init ( cipher . DECRYPT_MODE , pk ) ;
int blockSize = cipher . getBlockSize ( ) ;
int blockSize = cipher . getBlockSize ( ) ;
ByteArrayOutputStream bout = new ByteArrayOutputStream ( 64 ) ;
ByteArrayOutputStream bout = new ByteArrayOutputStream ( 64 ) ;