RSA概要介绍
1976年以前,所有的加密方法都是同一种模式:
- 甲方选择某一种加密规则,对信息进行加密;
- 乙方使用同一种规则,对信息进行解密
由于加密和解密使用同样的规则(简称“秘钥”),这种被称为“对称加密算法”。这种加密模式有个最大的弱点:甲方必须把加密规则告诉乙方,否则无法解密。保存和传递秘钥成了最头疼的问题。
1976年,两位美国计算机科学家Whitfield Diffie和Martin Hellman,提出了一种崭新的构思,可以在不直接传递秘钥完成加密,这个被称为”Diffie-Hellman密钥交换算法”。这个算法启发了其他科学家。人们认识到,加密和解密可以使用不同的规则,这要这种规则直接存在某种对应的关系即可,这样就避免了直接传递秘钥。
这种新的加密模式被称为“非对称加密算法”:
- 乙方生成两把密钥(公钥和私钥)。公钥是公开的,任何人都可以获得,私钥则是保密的。
- 甲方获取乙方的公钥,然后用它对信息加密。
- 乙方得到加密后的信息,用私钥解密。
如果公钥加密的信息只有私钥解得开,那么只要私钥不泄漏,通信就是安全的。
1977年,三位数学家Rivest、Shamir 和 Adleman 设计了一种算法,可以实现非对称加密。这种算法用他们三个人的名字命名,叫做RSA算法。从那时直到现在,RSA算法一直是最广为使用的”非对称加密算法”。毫不夸张地说,只要有计算机网络的地方,就有RSA算法。
这种算法非常可靠,密钥越长,它就越难破解。根据已经披露的文献,目前被破解的最长RSA密钥是768个二进制位。也就是说,长度超过768位的密钥,还无法破解(至少没人公开宣布)。因此可以认为,1024位的RSA密钥基本安全,2048位的密钥极其安全。
RSA是目前最有影响力的公钥加密算法,它能够抵抗到目前为止已知的绝大多数密码攻击,已被ISO推荐为公钥数据加密标准。
RSA加解密Java实现方式
RSA基础类:
public class RSABase {
private static final String ALGORITHM = "RSA";
private static final String SIGN_ALGORITHMS = "SHA1WithRSA";
private static final String DEFAULT_CHARSET = "UTF-8";
/** RSA最大加密明文大小 */
private static final int MAX_ENCRYPT_BLOCK = 117;
/** RSA最大解密密文大小 */
private static final int MAX_DECRYPT_BLOCK = 128;
private static final String TRANSFORMATION = "RSA";
public static String sign(String content, String privateKey) {
try {
PrivateKey priKey = getPrivateKey(privateKey);
java.security.Signature signature = java.security.Signature.getInstance(SIGN_ALGORITHMS);
signature.initSign(priKey);
signature.update(content.getBytes(DEFAULT_CHARSET));
byte[] signed = signature.sign();
return Base64.encode(signed);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 解密
*
* @param content
* 密文
* @param private_key
* 私钥
* @return 解密后的字符串
*/
public static String decryptByPrivateKey(String content, String private_key) throws Exception {
return decrypByKey(content, getPrivateKey(private_key));
}
/**
* 解密
*
* @param content
* 密文
* @param private_key
* 私钥
* @return 解密后的字符串
*/
public static String decryptByPublicKey(String content, String public_key) throws Exception {
return decrypByKey(content, getPublickKey(public_key));
}
/**
* 解密
*
* @param content
* @param key
* @return
* @throws Exception
*/
private static String decrypByKey(String content, Key key) throws Exception {
Cipher cipher = Cipher.getInstance(TRANSFORMATION);
cipher.init(Cipher.DECRYPT_MODE, key);
InputStream ins = new ByteArrayInputStream(Base64.decode(content));
ByteArrayOutputStream writer = new ByteArrayOutputStream();
// rsa解密的字节大小最多是128,将需要解密的内容,按128位拆开解密
byte[] buf = new byte[MAX_DECRYPT_BLOCK];
int bufl;
while ((bufl = ins.read(buf)) != -1) {
byte[] block = null;
if (buf.length == bufl) {
block = buf;
} else {
block = new byte[bufl];
for (int i = 0; i < bufl; i++) {
block[i] = buf[i];
}
}
writer.write(cipher.doFinal(block));
}
return new String(writer.toByteArray(), DEFAULT_CHARSET);
}
/**
* 得到私钥
*
* @param key
* 密钥字符串(经过base64编码)
* @throws Exception
*/
protected static PrivateKey getPrivateKey(String key) throws Exception {
byte[] keyBytes = Base64.decode(key);
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyBytes);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PrivateKey privateKey = keyFactory.generatePrivate(keySpec);
return privateKey;
}
/**
* 从文件中加载私钥
*
* @throws Exception
*/
protected static PrivateKey getPrivateKey(InputStream in) throws Exception {
return getPrivateKey(readFileKey(in));
}
/**
* 从文件中加载私钥
*
* @throws Exception
*/
protected static PublicKey getPublickKey(InputStream in) throws Exception {
return getPublickKey(readFileKey(in));
}
/**
* 读取文件中的秘钥信息
* @param in
* @return
* @throws Exception
*/