算法基础(非对称加密算法)
算法基础(消息摘要算法)https://www.52pojie.cn/thread-1613919-1-1.html(出处: 吾爱破解论坛)算法基础(对称加密算法)https://www.52pojie.cn/thread-1615926-1-1.html(出处: 吾爱破解论坛)
RSA算法(JAVA)
使用公钥加密、私钥解密
公钥公开、私钥保密
安全性高、一般明文字节数<密钥字节数-11
RSA可用于数据交换、数据校验(MDwithRSA)
两种加密算法结合套路
随机生成密钥
密钥用于AES、DES、3DES加密数据
RSA对密钥加密
提交加密后的密钥和加密后的数据给服务器
算法 密钥长度工作模式 填充方式 备注
RSA512~65536位(密钥长度必须是64的倍数) ECB NoPadding、
PKCS1Padding、
OAEPWTTHMD5AndMGFPadding、
OAEPWITHSHA1AndMGFIPadding、
OAEPWITHSHA256AndMGF1Padding、
OAEPWITHSHA384AndMGF1Padding、OAEPWITHSHA512AndMGF1Padding
Java 6实现
NONENoPadding、
PKCS1Padding、
OAEPWithMD5AndMGF1Padding、OAEPWithSHA1AndMGF1Padding、
OAEPWithSHA224AndMGFIPadding、
OAEPWithSHA256AndMGFIPadding、
OAEPWithSHA384AndMGFIPadding、
OAEPWithSHA512AndMGF1Padding、
ISO9796-1Padding
BouncyCastle
实现
import java.security.KeyFactory;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.Base64;
import java.util.Base64.Encoder;
import javax.crypto.Cipher;
public class RSA {
//公钥
public static String str_pubK="MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDxNrfFmfAAmP0kWOc09mHvJpUz"
+ "h66gfLyWRs/3eJasiwaBVwoLpA2bFwO6oABMsBV+jcJxlgoKhmv9dAd1KoNtUuTg"
+ "ktl4BwBVkhqGkdNLXqJlpJ4ckgZLHoGjIFGehVwvKQ6Nilmimo66Jkl3OvTubaq1"
+ "hHhpTpFV15xN6wDUXQIDAQAB";
//私钥
public static String str_priK="MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAPE2t8WZ8ACY/SRY"
+ "5zT2Ye8mlTOHrqB8vJZGz/d4lqyLBoFXCgukDZsXA7qgAEywFX6NwnGWCgqGa/10"
+ "B3Uqg21S5OCS2XgHAFWSGoaR00teomWknhySBksegaMgUZ6FXC8pDo2KWaKajrom"
+ "SXc69O5tqrWEeGlOkVXXnE3rANRdAgMBAAECgYEAxyUQxiBYaHXmkQqhIDXGFya4"
+ "icmcX718CWm/KyYw55UEMuaEUaYI4aE3n+2XMbkylcA9538jHRtrrt4Lhi0qQQ59"
+ "1baXhTcgVIzOYmLN9F2dQPuf7UVP25FJ6RmakteZAgkr9/VPqx2frlli/SjtdwOs"
+ "ZoyxYy/TzplL7rjzvLUCQQD+fhsCj2OCR5PL1VOm72qDiyqd74kf9HyBb2G+0tHy"
+ "N60SnN6vfQq+0y8xDTUYUv1y34RxSuNuh+JOP1dptAhbAkEA8qR6LApDr+XHBCW9"
+ "OB0ny5QRzpjFA9U2isNt0ynGPRlEpmj40uLpi0H8FHEeiqdHUSTXLunTv2ebs12d"
+ "obPzpwJAVZ7VtYHKZ81vExfdgf9wpXsPxn0NdFvL+7wedZX2HnjO2MFARmhaUiM4"
+ "8FbSMzHQpXI/2+HnLIu4/rxey2hPmwJAR867RTtIas7iheyxQeEG/O12E6EMdSTt"
+ "PkW1w89SFpTaxzC7qpVyrj653kbX1grJRU9Uy1Jx0uxTnNJwqwE5cwJAXulSSoAa"
+ "EhCrN2MPEmzHz21GH5ImFa96s3PZLakO5XGna86Dx3/xWuyYVmlv+yY3uf/Dk8Ib"
+ "gx6n6lvVot29tg==";
public static PublicKey getpublicKey(String key) throws Exception {
//公钥解码
byte[] keyBytes= Base64.getDecoder().decode(key);
//公钥实例化函数
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes);
KeyFactory keyFactory= KeyFactory.getInstance("RSA");
//最终加密公钥
PublicKey publicKey = keyFactory.generatePublic(keySpec);
return publicKey;
}
public static PrivateKey getprivateKey(String key) throws Exception {
//私钥解码
byte[] keyBytes= Base64.getDecoder().decode(key);
//私钥实例化函数
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyBytes);
KeyFactory keyFactory= KeyFactory.getInstance("RSA");
//最终加密私钥(私有)
PrivateKey privateKey = keyFactory.generatePrivate(keySpec);
return privateKey;
}
//(公钥+明文)加密
public static byte[] encrypt(byte[] bt_plaintext) throws Exception {
PublicKey publicKey= getpublicKey(str_pubK);
Cipher cipher= Cipher.getInstance("RSA/ECB/PKCS1Padding");
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
byte[] bt_encrypted= cipher.doFinal(bt_plaintext);
return bt_encrypted;
}
//私钥解密
public static byte[] decrypt(byte[] bt_encrypted) throws Exception {
PrivateKey privateKey= getprivateKey(str_priK);
Cipher cipher= Cipher.getInstance("RSA/ECB/PKCS1Padding");
cipher.init(Cipher.DECRYPT_MODE, privateKey);
byte[] bt_original= cipher.doFinal(bt_encrypted);
return bt_original;
}
//签名
public static byte[] sign(byte[] data) throws Exception {
PrivateKey prik = getprivateKey(str_priK);
Signature sig = Signature.getInstance("SHA256withRSA");
sig.initSign(prik);
sig.update(data);
return sig.sign();
}
//验证签名
public static boolean verify(byte[] data,byte[] sign) throws Exception {
PublicKey pubk = getpublicKey(str_pubK);
Signature sig = Signature.getInstance("SHA256withRSA");
sig.initVerify(pubk);
sig.update(data);
return sig.verify(sign);
}
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
String str_plaintext="吾爱破解";
byte[] bt_ciphre= encrypt(str_plaintext.getBytes());
System.out.println("明文:"+str_plaintext);
System.out.println("加密结果显示为十六进制:"+byteToHexString(bt_ciphre));
byte[] bt_original=decrypt(bt_ciphre);
String str_original= new String(bt_original);
System.out.println("解密:"+str_original);
System.out.println("==============================");
String str="52pojie";
byte[] signature= sign(str.getBytes());
System.out.println("原文:"+str);
System.out.println("产生签名:"+Base64.getEncoder().encode(signature));
boolean status= verify(str.getBytes(), signature);
System.out.println("验证情况:"+status);
}
//字节集转十六进制
public static String byteToHexString(byte[ ] by) {
StringBuffer SB = new StringBuffer();
int i = 0;
while (i < by. length) {
int k = by;
int j = k;
if (k < 0) {
j = k + 256;}
if (j < 16) {
SB.append("0");}
SB.append( Integer.toHexString(j));i +=1;
}
return SB.toString();
}
}
RSA算法(JS)
function getRSA(password,pubkey){
setMaxDigits(131);//最低是密钥字节数+1
var key = new RSAKeyPair("10001",pubkey);
return encryptedString(key,password);
} 应该发给同行让他们看看 我来看下 这个帖子很好! 楼主的标题为什么不写“RSA 算法java和Android如何一致” ?
Java中
public static final String KEY_ALGORITHM = "RSA";
Android中
public static final String KEY_ALGORITHM = "RSA/ECB/PKCS1Padding";
获取Cipher
Cipher cipher = Cipher.getInstance(KEY_ALGORITHM);
复制粘贴 好简单。 学习到了
页:
[1]