package rsa;
import java.io.IOException;
import java.security.InvalidKeyException;
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.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
@SuppressWarnings({ "restriction" })
public class RSAUtils {
/** 私有构造方法 */
private RSAUtils() {
super();
}
private static final Cipher CIPHER;
static {
try {
CIPHER = Cipher.getInstance("RSA");
} catch (Throwable e) {
throw new InternalError(e.getMessage());
}
}
//~ -----------------------------------------------------------------------------
/**
* 生成私钥和公钥
*
* @return KeyPair
*/
public static KeyPair generateKeyPair() {
return generateKeyPair(1024);
}
/**
* 生成私钥和公钥
*
* @param keySize 密钥长度
* @return KeyPair
*/
public static KeyPair generateKeyPair(int keySize) {
KeyPairGenerator keyPairGen = null;
try {
keyPairGen = KeyPairGenerator.getInstance("RSA");
} catch (NoSuchAlgorithmException e) {
throw new InternalError(e.getMessage());
}
keyPairGen.initialize(keySize);
return keyPairGen.generateKeyPair();
}
/**
* 从KeyPair中获得公钥
*
* @param keyPair KeyPair
* @return 公钥
*/
public static PublicKey getPublicKey(KeyPair keyPair) {
if (keyPair == null) {
return null;
}
return keyPair.getPublic();
}
/**
* 从KeyPair中获得私钥
*
* @param keyPair KeyPair
* @return 私钥
*/
public static PrivateKey getPrivateKey(KeyPair keyPair) {
if (keyPair == null) {
return null;
}
return keyPair.getPrivate();
}
/**
* 讲公钥或私钥转换为String
*
* @param key 公钥/私钥
* @return 字符串
*/
public static String getKeyString(Key key) {
if (key == null) {
return null;
}
byte[] keyBytes = key.getEncoded();
String s = (new BASE64Encoder()).encode(keyBytes);
return s;
}
/**
* 得到公钥
*
* @param key
* 密钥字符串(经过base64编码)
* @throws Exception
*/
public static PublicKey getPublicKey(String key) {
byte[] keyBytes = null;
try {
keyBytes = (new BASE64Decoder()).decodeBuffer(key);
} catch (IOException e) {
throw new IllegalArgumentException(e.getMessage(), e);
}
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes);
KeyFactory keyFactory = null;
try {
keyFactory = KeyFactory.getInstance("RSA");
} catch (NoSuchAlgorithmException e) {
throw new InternalError(e.getMessage());
}
PublicKey publicKey = null;
try {
publicKey = keyFactory.generatePublic(keySpec);
} catch (InvalidKeySpecException e) {
throw new IllegalArgumentException(e.getMessage(), e);
}
return publicKey;
}
/**
* 得到私钥
*
* @param key
* 密钥字符串(经过base64编码)
* @throws Exception
*/
public static PrivateKey getPrivateKey(String key) {
byte[] keyBytes;
try {
keyBytes = (new BASE64Decoder()).decodeBuffer(key);
} catch (IOException e) {
throw new IllegalArgumentException(e.getMessage(), e);
}
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyBytes);
KeyFactory keyFactory = null;
try {
keyFactory = KeyFactory.getInstance("RSA");
} catch (NoSuchAlgorithmException e) {
throw new InternalError(e.getMessage());
}
PrivateKey privateKey = null;
try {
privateKey = keyFactory.generatePrivate(keySpec);
} catch (InvalidKeySpecException e) {
throw new IllegalArgumentException(e.getMessage(), e);
}
return privateKey;
}
/**
* 加密数据
*
* @param data 原文
* @param key 公钥/私钥
* @return 密文
*/
public static byte[] encrypt(byte[] data, Key key) {
try {
CIPHER.init(Cipher.ENCRYPT_MODE, key);
} catch (InvalidKeyException e) {
throw new IllegalArgumentException(e.getMessage(), e);
}
byte[] result = null;
try {
result = CIPHER.doFinal(data);
} catch (IllegalBlockSizeException e) {
throw new IllegalArgumentException(e.getMessage(), e);
} catch (BadPaddingException e) {
throw new IllegalArgumentException(e.getMessage(), e);
}
return result;
}
/**
* 解密数据
*
* @param data 密文
* @param key 公钥/私钥
* @return 原文
*/
public static byte[] decrypt(byte[] data, Key key) {
try {
CIPHER.init(Cipher.DECRYPT_MODE, key);
} catch (InvalidKeyException e) {
throw new IllegalArgumentException(e.getMessage(), e);
}
byte[] result = null;
try {
result = CIPHER.doFinal(data);
} catch (IllegalBlockSizeException e) {
throw new IllegalArgumentException(e.getMessage(), e);
} catch (BadPaddingException e) {
throw new IllegalArgumentException(e.getMessage(), e);
}
return result;
}
}
用法
@Override
public String encryptPassword(String password) {
if (password == null) {
return null;
}
byte[] data = RSAUtils.encrypt(password.getBytes(), publicKey);
return Base64.encodeBase64URLSafeString(data);
}
@Override
public String decryptPassword(String encryptedPassword) {
if (encryptedPassword == null) {
return null;
}
byte[] data = Base64.decodeBase64(encryptedPassword);
return new String(RSAUtils.decrypt(data, privateKey));
}
分享到:
相关推荐
用JAVA实现Rsa非对称加密算法 不算简单的算法 既可以加密又可以解密
java rsa非对称加密算法3个实例源代码
* 字符串格式的密钥在未在特殊说明情况下都为BASE64编码格式 * 由于非对称加密速度极其缓慢,一般文件不使用它来... * 非对称加密算法可以用来对对称加密的密钥加密,这样保证密钥的安全也就保证了数据的安全
java关于加密的算法demo代码(Base64加密\数字签名\对称加密算法\非对称加密算法\消息摘要算法) JAVA安全实现三种方式: 1.JDK 2.Commons Codec 3.Bouncy Castle 一。非对称加密算法: 1.DH 2.RSA 3.ELGamal 二。...
C#和Java关于RSA非对称加密互通类,包括提供RSA加密、解密方法,Java到c#的RSA私钥格式转换,Java到c#的RSA公钥格式转换等,使用PKCS1填充算法
RSA加密算法-Java实现,随机生产秘钥对,由公钥对内容进行加密,私钥解密。
登录时,用户的密码用明文传输...非对称加密算法常用RSA算法,秘钥使用base64编码成字符串,后端使用jdk8的标准API,前端使用jsencrypt.js进行RSA的对应操作。经过测试,本例中的前后端代码的加密解密计算结果是一致的。
RSA算法是一种非对称密码算法,所谓非对称,就是指该算法需要一对密钥,使用其中一个加密,则需要用另一个才能解密。 这时一个简单的Java实现,在RSA网络安全工具类是比较完整的实现。
* Android版本的RSA加密算法实现 Java也适用 * 1. 可生成密钥对 * 2. 解析X509格式的公钥私钥
包括RSA算法的两种语言实现,原理正确,可以正常运行,对应博客为:https://blog.csdn.net/qq_41112170/article/details/104904340
AsymmetryEncrypt.java:非对称加密算法的上层接口,提供加/解密、数字签名/验证操作所需的方法 * 加密/解密操作(公钥加密,私钥解密) * 数字签名/验证(私钥加密,公钥验证) MessageDigester.java为...
文中基于研究设计适合于实际工程中使用的加密算法的目的,采用了分析与比较的方法,首先研究了目前常用的对称加密算法DES以及非对称加密算法RSA,然后分析比较了它们的优缺点,进而综合安全性和效率设计出适合于在...
Java加密技术非对称加密算法RSA.doc
RSA非对称加密和DES对称加密代码示例包含(commons-lang3-3.1 commons-codec-1.4.jar)
主要介绍了java 非对称加密算法RSA实现详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
运行环境:Windows + eclipse + JAVA RSA加密算法是一种非对称加密算法。在公开密钥加密和电子商业中RSA被广泛使用。
在这篇文档中,我们将深入探讨非对称加密RSA的实现及其源代码,以及如何使用JAVA代码来实现。此外,我们还将介绍RSA加密算法的历史和基本原理,以及RSA加密算法在现代网络安全领域中的应用。通过本文档的学习,您将...
代码描述了如何使用RSA算法进行非对称加密, 一对密钥由公钥和私钥组成(可以使用很多对密钥)。私钥可以解密公钥加密的数据,公钥可以解密私钥加密的数据(私钥公钥可以互相加密解密)
**基于Java的RSA文件加密软件的设计与实现** ...* Java学习者:通过学习和实践本项目,可以掌握RSA非对称加密算法的原理和应用。 * 软件开发者:可作为加密模块嵌入到其他Java应用中,提高数据安全性。
非对称加密算法RSA算法的JAVA实现,可视化界面,支持多种形式,供参考