一个使用bouncycastle进行安全操作的实用类
来源:互联网 发布:苹果mac重装系统教程 编辑:程序博客网 时间:2024/06/07 22:54
import java.io.*;
import java.security.*;
import java.security.interfaces.*;
import java.math.*;
import java.util.Enumeration;
import java.util.Vector;
import org.bouncycastle.crypto.AsymmetricBlockCipher;
import org.bouncycastle.crypto.encodings.PKCS1Encoding;
import org.bouncycastle.crypto.engines.RSAEngine;
import org.bouncycastle.crypto.params.RSAKeyParameters;
import org.bouncycastle.crypto.params.RSAPrivateCrtKeyParameters;
import org.bouncycastle.util.encoders.Hex;
public class RSAUtil {
final public static int RAW = 1;
final public static int PKCS1 = 2;
/*
* 产生RSA公私钥对
*/
public static KeyPair genRSAKeyPair() {
KeyPairGenerator rsaKeyGen = null;
KeyPair rsaKeyPair = null;
try {
System.out.println("Generating a pair of RSA key ... ");
rsaKeyGen = KeyPairGenerator.getInstance("RSA");
SecureRandom random = new SecureRandom();
random.nextBytes(new byte[1]);
rsaKeyGen.initialize(1024, new SecureRandom());
rsaKeyPair = rsaKeyGen.genKeyPair();
PublicKey rsaPublic = rsaKeyPair.getPublic();
PrivateKey rsaPrivate = rsaKeyPair.getPrivate();
System.out.println("1024-bit RSA key GENERATED.");
} catch (Exception e) {
System.out.println("Exception in keypair generation. Reason: " + e);
}
return rsaKeyPair;
}
/*
* 列出密钥库中指定的条目
*/
public static void showAllEntry(String filename, String pass) {
try {
FileInputStream inKeyStoreFile = new FileInputStream(filename);
char[] password = pass.toCharArray();
KeyStore from = KeyStore.getInstance("JKS", "SUN");
from.load(null, null);
from.load(inKeyStoreFile, password);
Enumeration e = from.aliases();
System.out.println("Entry List:");
while (e.hasMoreElements()) {
System.out.println((String) e.nextElement());
}
inKeyStoreFile.close();
} catch (Exception e) {
System.out.println(e);
}
}
/*
* 列出密钥库中所有的条目
*/
public static Vector getAllEntry(String filename, String pass) {
Vector v = new Vector();
try {
FileInputStream inKeyStoreFile = new FileInputStream(filename);
char[] password = pass.toCharArray();
KeyStore from = KeyStore.getInstance("JKS", "SUN");
from.load(null, null);
from.load(inKeyStoreFile, password);
Enumeration e = from.aliases();
System.out.println("Entry List:");
while (e.hasMoreElements()) {
v.addElement((String) e.nextElement());
}
inKeyStoreFile.close();
return v;
} catch (Exception e) {
System.out.println(e);
return null;
}
}
/*
* 获得私钥
*/
public static RSAPrivateKey loadPrivateKey(String filename, String keyName,
String pass) throws Exception {
FileInputStream inKeyStoreFile = new FileInputStream(filename);
char[] password = pass.toCharArray();
KeyStore from = KeyStore.getInstance("JKS", "SUN");
from.load(null, null);
from.load(inKeyStoreFile, password);
Key testkey = from.getKey(keyName, password);
RSAPrivateKey pvtKey = (RSAPrivateKey) testkey;
System.out.println("Private key exponent =/r/n"
+ pvtKey.getPrivateExponent().toString(16) + "/r/n");
inKeyStoreFile.close();
return pvtKey;
}
/*
* 获得公钥
*/
public static RSAPublicKey loadPublicKey(String filename, String keyName,
String pass) throws Exception {
FileInputStream inKeyStoreFile = new FileInputStream(filename);
char[] password = pass.toCharArray();
KeyStore from = KeyStore.getInstance("JKS", "SUN");
from.load(null, null);
from.load(inKeyStoreFile, password);
java.security.cert.Certificate c = from.getCertificate(keyName);
RSAPublicKey pubKey = (RSAPublicKey) c.getPublicKey();
System.out.println("Public key exponent =/r/n"
+ pubKey.getPublicExponent().toString(16) + "/r/n");
inKeyStoreFile.close();
return pubKey;
}
/*
* 使用公钥加密
*/
public static byte[] rsaPubEncrypt(RSAPublicKey PubKey, byte[] clearBytes,
int type) {
BigInteger mod = PubKey.getModulus();
BigInteger pubExp = PubKey.getPublicExponent();
RSAKeyParameters pubParameters = new RSAKeyParameters(false, mod,
pubExp);
System.out.println("mod:/r/n" + mod.toString(16));
System.out.println("pubExp:/r/n" + pubExp.toString(16));
AsymmetricBlockCipher eng = new RSAEngine();
if (type == PKCS1)
eng = new PKCS1Encoding(eng);
eng.init(true, pubParameters);
byte[] data = null;
try {
System.out.println("clearBytes:/r/n"
+ new String(Hex.encode(clearBytes)));
data = eng.processBlock(clearBytes, 0, clearBytes.length);
System.out.println("EncBytes:/r/n" + new String(Hex.encode(data)));
return data;
} catch (Exception e) {
System.out.println(e);
return null;
}
}
/*
* 公钥解密
*/
public static byte[] rsaPubDecrypt(RSAPublicKey PubKey, byte[] clearBytes,
int type) {
BigInteger mod = PubKey.getModulus();
BigInteger pubExp = PubKey.getPublicExponent();
RSAKeyParameters pubParameters = new RSAKeyParameters(false, mod,
pubExp);
System.out.println("mod:/r/n" + mod.toString(16));
System.out.println("pubExp:/r/n" + pubExp.toString(16));
AsymmetricBlockCipher eng = new RSAEngine();
if (type == PKCS1)
eng = new PKCS1Encoding(eng);
eng.init(false, pubParameters);
byte[] data = null;
try {
System.out.println("clearBytes:/r/n"
+ new String(Hex.encode(clearBytes)));
data = eng.processBlock(clearBytes, 0, clearBytes.length);
System.out.println("EncBytes:/r/n" + new String(Hex.encode(data)));
return data;
} catch (Exception e) {
System.out.println(e);
return null;
}
}
/*
* 私钥解密
*/
public static byte[] rsaPriDecrypt(RSAPrivateKey prvKeyIn,
byte[] encodedBytes, int type) {
RSAPrivateCrtKey prvKey = (RSAPrivateCrtKey) prvKeyIn;
BigInteger mod = prvKey.getModulus();
BigInteger pubExp = prvKey.getPublicExponent();
BigInteger privExp = prvKey.getPrivateExponent();
BigInteger pExp = prvKey.getPrimeExponentP();
BigInteger qExp = prvKey.getPrimeExponentQ();
BigInteger p = prvKey.getPrimeP();
BigInteger q = prvKey.getPrimeQ();
BigInteger crtCoef = prvKey.getCrtCoefficient();
RSAKeyParameters privParameters = new RSAPrivateCrtKeyParameters(mod,
pubExp, privExp, p, q, pExp, qExp, crtCoef);
AsymmetricBlockCipher eng = new RSAEngine();
if (type == PKCS1)
eng = new PKCS1Encoding(eng);
eng.init(false, privParameters);
byte[] data = null;
try {
data = eng.processBlock(encodedBytes, 0, encodedBytes.length);
return data;
} catch (Exception e) {
System.out.println(e);
return null;
}
}
/*
* 使用私钥加密
*/
public static byte[] rsaPriEncrypt(RSAPrivateKey prvKeyIn,
byte[] encodedBytes, int type) {
RSAPrivateCrtKey prvKey = (RSAPrivateCrtKey) prvKeyIn;
BigInteger mod = prvKey.getModulus();
BigInteger pubExp = prvKey.getPublicExponent();
BigInteger privExp = prvKey.getPrivateExponent();
BigInteger pExp = prvKey.getPrimeExponentP();
BigInteger qExp = prvKey.getPrimeExponentQ();
BigInteger p = prvKey.getPrimeP();
BigInteger q = prvKey.getPrimeQ();
BigInteger crtCoef = prvKey.getCrtCoefficient();
RSAKeyParameters privParameters = new RSAPrivateCrtKeyParameters(mod,
pubExp, privExp, p, q, pExp, qExp, crtCoef);
AsymmetricBlockCipher eng = new RSAEngine();
if (type == PKCS1)
eng = new PKCS1Encoding(eng);
eng.init(true, privParameters);
byte[] data = null;
try {
data = eng.processBlock(encodedBytes, 0, encodedBytes.length);
return data;
} catch (Exception e) {
System.out.println(e);
return null;
}
}
}
- 一个使用bouncycastle进行安全操作的实用类
- BouncyCastle使用
- 安全的进行new 操作
- 网上搜集的一个比较实用的图片操作类
- 网上搜集的一个比较实用的图片操作类
- 自己写的一个注册表操作类----相当实用
- 一个php操作数据库的封装类,超实用.
- 一个实用的类
- 一个使用POI进行Excel多线程写操作的demo
- 一个简单实用的数据库操作框架
- 一个简单实用的数据库操作框架
- 一个简单实用的数据库操作框架
- 一个简单实用的数据库操作框架
- 一个简单实用的数据库操作框架
- 一个简单实用的数据库操作框架
- 一个简单实用的数据库操作框架
- 一个简单实用的数据库操作框架
- 一个简单实用的数据库操作框架
- 自旋锁和信号量 介绍
- 可用在JavaME上的加密包Bouncy Castle的使用
- Oracle使用技巧之分析函数篇
- Unix命令使用小荟萃(补充中):
- IC封装术语大全
- 一个使用bouncycastle进行安全操作的实用类
- Java3D理解初级教程
- GridVew数据导出到Wrod、Excl
- 移植dm9000ae驱动到u-boot
- Symbian PKG 文档
- 浅析C++中的this指针
- Linux中部分命令
- PING命令实现及知识
- STL------通用工具