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;
}
}
}
分享到:
相关推荐
Bouncy Castle 还包含了一些实用工具类,如Base64编码/解码、哈希函数(如MD5、SHA-1、SHA-256等)、随机数生成器等,这些工具在各种加密操作中都有重要作用。 7. **安全性和合规性** 随着密码学标准和法规的不断...
BouncyCastle是一个开源的Java加密库,提供了广泛的加密算法、协议和实用工具。在Java开发中,特别是涉及安全和加密的领域,BouncyCastle是非常重要的一环。标题提到的"bouncycastle两个完整包"指的是BouncyCastle库...
对于需要在Java应用中进行高级加密操作的开发者来说,Bouncy Castle是一个不可或缺的工具。然而,使用时也需要注意选择合适的版本以适应不同的Java环境,并确保遵循当地的法规,因为某些加密算法可能在特定地区是...
标题提到的"BouncyCastle.Cryptoe.1.0.0.zip"是一个用于解决这类问题的工具,它允许Java RSA签名与C#之间进行无缝交互。BouncyCastle是一个强大的开源密码学库,它为Java和.NET平台提供了丰富的加密功能,包括对RSA...
例如,创建一个新的RSA密钥对,可以使用`org.bouncycastle.jce.provider.BouncyCastleProvider`作为安全提供商,并调用相应的KeyPairGenerator类进行生成。 然而,随着Java版本的更新和加密技术的进步,bcprov-jdk...
Bouncy Castle是一个广泛使用的开源安全库,尤其在Java和.NET平台上,它提供了丰富的加密算法、密钥管理和证书处理功能。 描述中提到,这是一个“java加密工具”,这意味着它提供了各种加密操作,包括但不限于对称...
在这个特定的情况下,我们只看到一个文件“BouncyCastle.Crypto.dll”,这正是Bouncy Castle库的C#实现的DLL,可以被其他C#应用程序引用和使用。 BouncyCastle.Crypto.dll包含了大量的加密算法,包括但不限于: 1....
这个文件是Bouncy Castle库的一个版本,Bouncy Castle是一个广泛使用的开源Java加密库,它提供了大量的加密算法、协议和实用工具。"jdk15on"意味着这个版本支持Java 1.5及更高版本。"1.66"是该库的特定版本号。 ...
Bouncy Castle是一个开源的Java加密库,提供了广泛的加密算法、协议实现和实用工具,广泛用于Java应用中的加密和解密操作。 描述中提到的"bcprov-jdk16-140.jar"是Bouncy Castle库的JAR文件,它是Java应用程序中...
你可以通过这个类创建、保存和加载密钥对,然后使用`Encrypt`和`Decrypt`方法进行加解密操作。 3. **Java格式的公钥私钥**:Java的`java.security.KeyPairGenerator`和`java.security.KeyFactory`类通常用于生成和...
Bouncy Castle是一个开源的Java加密库,提供了广泛的加密算法、协议和实用工具类,适用于各种加密任务。在Android平台上,由于默认的Java Cryptography Extension (JCE)有一些限制,如不能处理高强度的加密算法,...
BouncyCastle是一个开放源代码的Java密码学API,提供了大量的加密算法、协议和实用工具类。对于处理PKCS#12和X.509证书,BouncyCastle提供了丰富的API支持。 要创建一个PKCS#12格式的数字签名证书,我们需要遵循...
Bouncy Castle是一个开放源码的密码学API,为Java和.NET平台提供了广泛的加密算法、协议和实用工具。这个库被广泛用于处理加密、数字签名、SSL/TLS连接等任务。 描述中提到的"javax.net.ssl.SSLException: java....
Bouncy Castle是一个广泛使用的开源Java安全库,提供了一整套加密算法、协议和实用工具。bcprov-jdk15是Bouncy Castle针对Java 1.5及更高版本的一个组件,它包含了各种加密功能,如RSA、AES、DES等,以及PKCS#7、X....
3. **Bouncy Castle库**:Bouncy Castle是Java的一个加密库,提供了更广泛的加密算法支持,包括一些标准JCE不包含的算法。对于旧版本的JDK,可能需要Bouncy Castle来实现某些高级加密功能,如RSA。 4. **jar文件...
在使用BouncyCastle进行OpenPGP操作时,可以参考其提供的`bcpg-jdk15on.jar`包内的例子目录,这些示例代码可以帮助理解如何正确地集成和使用OpenPGP功能。 总的来说,OpenPGP在Java中的实现涉及到复杂的加密流程和...
AES256加密工具类是信息安全领域中一个重要的实用组件,它主要负责对数据进行加密保护,确保数据在传输或存储过程中的安全。AES(Advanced Encryption Standard)是一种广泛使用的对称加密算法,提供了强大的安全性...
【bcprov.jar】是一个在Java平台上广泛使用的加密库,它由Bouncy Castle组织提供,是Bouncy Castle Provider的组成部分。Bouncy Castle是一个开源项目,旨在为Java和.NET平台提供强大的加密、签名和证书处理功能。这...
- **bcprov-jdk16-1.4**:这是Bouncy Castle库的一个版本,这是一个广泛使用的Java加密库,提供了对多种加密算法的支持,包括AES和非对称加密。 综合来看,这个压缩包提供了一个适用于Java环境的AES加密工具类...