原文地址:http://wlh.iteye.com/blog/134796
生成RSA密钥、保存到文件、从文件读取、加密、解密等操作。
import java.security.Key;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.SecureRandom;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import javax.crypto.Cipher;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
public class RSATest {
public static void main(String[] args) {
try {
RSATest encrypt = new RSATest();
String encryptText = "encryptText";
// Generate keys
KeyPair keyPair = encrypt.generateKey();
RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();
RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();
byte[] e = encrypt.encrypt(publicKey, encryptText.getBytes());
byte[] de = encrypt.decrypt(privateKey, e);
System.out.println(toHexString(e));
System.out.println(toHexString(de));
} catch (Exception e) {
e.printStackTrace();
}
}
public KeyPair generateKey() throws NoSuchAlgorithmException {
KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("RSA");
keyPairGen.initialize(1024, new SecureRandom());
KeyPair keyPair = keyPairGen.generateKeyPair();
return keyPair;
}
public void saveKey(KeyPair keyPair, String publicKeyFile,
String privateKeyFile) throws ConfigurationException {
PublicKey pubkey = keyPair.getPublic();
PrivateKey prikey = keyPair.getPrivate();
// save public key
PropertiesConfiguration publicConfig = new PropertiesConfiguration(
publicKeyFile);
publicConfig.setProperty("PULIICKEY", toHexString(pubkey.getEncoded()));
publicConfig.save();
// save private key
PropertiesConfiguration privateConfig = new PropertiesConfiguration(
privateKeyFile);
privateConfig.setProperty("PRIVATEKEY",
toHexString(prikey.getEncoded()));
privateConfig.save();
}
/**
* @param filename
* @param type:
* 1-public 0-private
* @return
* @throws ConfigurationException
* @throws NoSuchAlgorithmException
* @throws InvalidKeySpecException
*/
public Key loadKey(String filename, int type)
throws ConfigurationException, NoSuchAlgorithmException,
InvalidKeySpecException {
PropertiesConfiguration config = new PropertiesConfiguration(filename);
KeyFactory keyFactory = KeyFactory.getInstance("RSA",
new BouncyCastleProvider());
if (type == 0) {
// privateKey
String privateKeyValue = config.getString("PULIICKEY");
PKCS8EncodedKeySpec priPKCS8 = new PKCS8EncodedKeySpec(
toBytes(privateKeyValue));
PrivateKey privateKey = keyFactory.generatePrivate(priPKCS8);
return privateKey;
} else {
// publicKey
String privateKeyValue = config.getString("PRIVATEKEY");
X509EncodedKeySpec bobPubKeySpec = new X509EncodedKeySpec(
toBytes(privateKeyValue));
PublicKey publicKey = keyFactory.generatePublic(bobPubKeySpec);
return publicKey;
}
}
/**
* Encrypt String.
*
* @return byte[]
*/
protected byte[] encrypt(RSAPublicKey publicKey, byte[] data) {
if (publicKey != null) {
try {
Cipher cipher = Cipher.getInstance("RSA",
new BouncyCastleProvider());
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
return cipher.doFinal(data);
} catch (Exception e) {
e.printStackTrace();
}
}
return null;
}
/**
* Basic decrypt method
*
* @return byte[]
*/
protected byte[] decrypt(RSAPrivateKey privateKey, byte[] raw) {
if (privateKey != null) {
try {
Cipher cipher = Cipher.getInstance("RSA",
new BouncyCastleProvider());
cipher.init(Cipher.DECRYPT_MODE, privateKey);
return cipher.doFinal(raw);
} catch (Exception e) {
e.printStackTrace();
}
}
return null;
}
public static String toHexString(byte[] b) {
StringBuilder sb = new StringBuilder(b.length * 2);
for (int i = 0; i < b.length; i++) {
sb.append(HEXCHAR[(b[i] & 0xf0) >>> 4]);
sb.append(HEXCHAR[b[i] & 0x0f]);
}
return sb.toString();
}
public static final byte[] toBytes(String s) {
byte[] bytes;
bytes = new byte[s.length() / 2];
for (int i = 0; i < bytes.length; i++) {
bytes[i] = (byte) Integer.parseInt(s.substring(2 * i, 2 * i + 2),
16);
}
return bytes;
}
private static char[] HEXCHAR = { '0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
}
分享到:
相关推荐
C#做的RSA加密解密, 带winform界面, 测试通过, 代码完整。
RSA加密解密C#实现调用实例 public string RSAEncrypt(string xmlPublicKey, string m_strEncryptString) { try { byte[] PlainTextBArray; byte[] CypherTextBArray; string Result; System.Security....
RSA加密解密算法 C++ 密码学 RSA加密解密算法 C++ 密码学 RSA加密解密算法 C++ 密码学
RSA加密解密工具,用于各类敏感信息文件的加密和解密,压缩包内含操作说明
易语言调用JSEncrypt实现RSA加密解密。Tags:JSEncryptRSA加密解密。
RSA加密解密算法源码,功能齐全,没有密码,使用方便,学习用.
前端RSA加密解密的使用,含jsencrypt.js文件(uni-app也可用)
Android RSA加密解密demo,详情请参看博客:http://blog.csdn.net/bbld_/article/details/38777491
采用OpenSSL进行RSA加密解密,对Delphi-OpenSSL进行重构。 1、支持公钥加密、解密,私钥加密解密。 2、支持PKCS1和PKCS8两种密钥格式。 3、支持PKCS#1 v1.5、PKCS#1 OAEP、SSL v2等填充方式。 4、支持生成密钥对。 5...
rsa加密解密算法C语言代码 #include #include #include <stdlib.h> #include <time.h> #include #include #define MAX 100 #define LEN sizeof(struct slink) void sub(int a[MAX],int b[MAX] ,int c[MAX] ); ...
unity工具类RSA加密和解密
RSA加密解密算法,相当不错的例子,适合初学者参考
经过本人修改,简化并完善了别人的代码,使其更加的容易理解和学习! 此为一个完整的项目,...功能:服务端随机生成密钥,JS用公钥加密,服务端用私钥解密。用到的JS加密文件是从官网下载的最新版,速度快,稳定性好!
实现了完整的RSA加密解密算法的使用,使用公钥加密解密和使用私钥加密解密,数字认证等等,直观清晰的演示RSA的用法,让不懂得人一看就会用
JAVA+RSA的DEMO工程,适合初学者学习RSA加密解密 文档参看 http://blog.csdn.net/u013400939/article/details/44490441
RSA解密加密JAVA
RSA 加密解密算法C语言 (含linux)内含代码加实例
RSA加密解密C++代码 是RSA加密及其解密的具体代码。有利于参考
JS用RSA加密解密技术对密码进行加密解密,再在后台使用对应公钥私钥参数进行解密,提升数据安全 jsencrypt.min.js
* RSA加密解密:私钥解密,公钥加密。 * RSA数字签名-俗称加签验签:私钥加签,公钥验签。 * RSA加密解密:私钥解密,公钥加密。 * RSA数字签名-俗称加签验签:私钥加签,公钥验签。 * RSA加密解密:私钥...