javaRSA2加密、解密、签名和验证签名
突然发现接收百度资源太多了,我也回馈⼀下吧。
RSA2 标准算法名称 SHA256WithRSA
1、RSA⾮对称加密技术
2、SHA256 是SHA-2下细分出的⼀种算法
SHA-2,名称来⾃于安全散列算法2(英语:Secure Hash Algorithm 2)的缩写,⼀种密码散列函数算法标准,由美国国家安全局研发,属于SHA算法之⼀,是SHA-1的后继者。
2、签名和验证签名
签名是由发送数据的⼀⽅发起的,防⽌传输过程中被篡改数据内容。因此签名使⽤的是私钥。⽽验证签名使⽤的是公钥。
t.Context;
import android.util.Log;
import dec.binary.Base64;
import java.security.Key;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.SecureRandom;
import java.security.Signature;
import java.security.spec.KeySpec;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.ArrayList;
pto.Cipher;
public final class RSAUtils {
private static final String KEY_ALGORITHM = "RSA";
private static final int KEY_SIZE = 2048;//设置长度
public static final String SIGNATURE_ALGORITHM = "SHA256withRSA";
public static final String RSA_TYPE = "RSA/ECB/PKCS1Padding";
private final static String PUBLIC_KEY_NAME = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAvJWHTWgcfPjzWTt1OAUAvOnd8N/ITzP+Xe 1PY2fMhFNzmRV9a78fAZ5C6ynFzsQGJNffvlw4HoB6HsUWmGymkR9950hqXXiMpbNoQ4MqEtxPKx1Blusp3E8tJ9ABzrSat4/PfkQ+Iw9t5sHQhSFt2kJCD7 KUw9gW8hVMnQTngMlgvt1U9ozj7tBdQMRduk/KkA1G/ZxvYWb5ZcFb0PeHoUDuEr6HXOycxm6cRZBjly97IrfUCEemKKKPmjacntaP+pgS0cc9jWIYAeMhhj mRa7hY2pn6Q3mSiq+V9ruUnKQOlyLcQ1Y1wrfOydo0e/3jAzoLeJR8uWUnZQcxmnuzUwIDAQAB";
private final static String PRI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JS7BPm6bb7iuY3U2XPHfEbrGICjQ4m0xLxywKBgQDA4+XGO4V8bC1JpFGRY+6NVRkl/qJ9cqC9eZqMgT9kZif6R1rP4 tfQUTPAy1S2KruKhR6TUjLGLCZOGBg0GJ+2JfM3VpUlPb4m/gzwe5vsyLppfXGGIOGW+gTGao74+TbVkXyeVswkB7yl5wNP0ATRFwNr2CK2BN9c8rz3o+U WcQKBgQCgEQjVuKMJCRrG5boC/7XktsDpclAs+La3+1AT6AutsDSS0mEmjvs2EuJ96rqaVAiaTzsgQSg7fh7sLcSw6FX5gKH/k00ORWsYiR0Ga5JiKV/FWtz miN116Kotr4xDZYMDxDd7tdB7mqSJbz1/CjgPckx3XCdXP8vcgHUp7BmNMQKBgFRZbnts4P+z+hgA8sjxYB7F2TXS6mcJJrBvXE0fi2XWn61M7NR3KBBH7r J1tZG5AyIDuZf0/3boAtilrc+2dkpivqmuhHupjwxCZRIT0AqhndoAFuUTaLxkRHRrrzufvf04MjMKljQxbCnStCKf686G5Sii1DZ9dOuoc95vcjtN";
private final static String SERVICE_PUBLIC_KEY_NAME = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEApXrOQ8d9wUvSDLqeS+QbrL6FM
private final static String SERVICE_PUBLIC_KEY_NAME = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEApXrOQ8d9wUvSDLqeS+QbrL6FM g5qKeB9PDnUkvXG71xUUSxTW4uIDggzm3v9OXTYRt3yZEvr2WNJuvLy6xtBn/sMbP9vVnp1Q6KcXCH8pWnHjziS/Wv2cwjYWMVEOj0vICtiwei7G9oulgzBR nO8k8kIeqxrDLwHC9w2/Zh6R39f49xqIo2iHIiVSGObNEqS9NkGbKVPR1WlVzB2VKGc4tsciWYagi98OZwMuvz29Ums0yiG06HlUSAhAWEb+qKeoT4e3/DtX Rbu8fXEF4EzjR0X4O6sS3PdwKpKkIlih99IdbnJ0jL8ALqhgV6NXEe/az4+I5WHM7lzviLVWfxIIQIDAQAB";
/**
* ⽣成公、私钥
* 根据需要返回String或byte[]类型
* @return
*/
public static ArrayList<String> createRSAKeys(){
ArrayList<String> array = new ArrayList<>();
try {
KeyPairGenerator keyPairGenerator = Instance(KEY_ALGORITHM);
keyPairGenerator.initialize(KEY_SIZE, new SecureRandom());
KeyPair keyPair = ateKeyPair();
PublicKey publicKey = Public();
PrivateKey privateKey = Private();
//获取公、私钥值
String publicKeyValue = Encoded());
String privateKeyValue = Encoded());
//存⼊
array.add(publicKeyValue);
array.add(privateKeyValue);
Log.e(" >>> ",publicKeyValue);
Log.e(" >>> ",privateKeyValue);
} catch (Exception e) {
e.printStackTrace();
}
return array;
}
//获取本地RSA公钥
public static PublicKey getPublicKey() {
try {
return  getPublicKey(PUBLIC_KEY_NAME);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
//获取本地RSA公钥
public static String getPublicKeyString() {
try {
return  PUBLIC_KEY_NAME;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
//获取服务器RSA公钥
public static PublicKey getServicePublicKey() {
try {
return  getPublicKey(SERVICE_PUBLIC_KEY_NAME);
} catch (Exception e) {
e.printStackTrace();
}
}
return null;
}
//获取RSA公钥根据钥匙字段
public static PublicKey getPublicKey(String key) {
try {
byte[] byteKey = Base64.decodeBase64(key);
X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(byteKey);
KeyFactory keyFactory = Instance(KEY_ALGORITHM);
atePublic(x509EncodedKeySpec);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
//获取RSA私钥根据钥匙字段
private static PrivateKey getPrivateKey(String key) {
try {
byte[] byteKey = Base64.decodeBase64(key);
PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(byteKey);            KeyFactory keyFactory = Instance(KEY_ALGORITHM);
atePrivate(pkcs8EncodedKeySpec);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
//本地RSA私钥签名
public static String sign(String requestData){
String signature = null;
byte[] signed = null;
try {
//            Log.e("=0== 签名前 >>>",requestData);
PrivateKey privateKey = getPrivateKey(PRIVATE_KEY_NAME);
Signature Sign = Instance(SIGNATURE_ALGORITHM);
Sign.initSign(privateKey);
Sign.Bytes());
signed = Sign.sign();
android获取真正的签名signature = deBase64String(signed);
} catch (Exception e) {
e.printStackTrace();
}
return signature;
}
//公钥验证签名  base64签名 signature  签名内容requestData
public static boolean verifySign(String requestData, String signature){
boolean verifySignSuccess = false;
try {
PublicKey publicKey = getServicePublicKey();
Signature verifySign = Instance(SIGNATURE_ALGORITHM);
verifySign.initVerify(publicKey);
verifySign.Bytes());
verifySignSuccess = verifySign.verify(Base64.decodeBase64(signature));            System.out.println(" >>> "+verifySignSuccess);
} catch (Exception e) {
e.printStackTrace();
}
return verifySignSuccess;
}
public static String encrypt(String clearText) {
String encryptedBase64 = "";
try {
Key key = getServicePublicKey();
final Cipher cipher = Instance(RSA_TYPE);
cipher.init(Cipher.ENCRYPT_MODE, key);
//
byte[] encryptedBytes = cipher.Bytes("UTF-8"));
encryptedBase64 = deBase64String(encryptedBytes);
} catch (Exception e) {
e.printStackTrace();
}
return encryptedBase64;
}
public static String decrypt(String encryptedBase64) {
String decryptedString = "";
try {
Key key =  getPrivateKey(PRIVATE_KEY_NAME);
final Cipher cipher = Instance(RSA_TYPE);
cipher.init(Cipher.DECRYPT_MODE, key);
byte[] encryptedBytes = Base64.decodeBase64(encryptedBase64);
byte[] decryptedBytes = cipher.doFinal(encryptedBytes);
decryptedString = new String(decryptedBytes);
} catch (Exception e) {
e.printStackTrace();
}
return decryptedString;
}
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。