You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
188 lines
8.9 KiB
Java
188 lines
8.9 KiB
Java
|
1 year ago
|
package com.sztzjy.linkCommerce.util;
|
||
|
|
|
||
|
|
import org.apache.commons.codec.binary.Base64;
|
||
|
|
|
||
|
|
import javax.crypto.Cipher;
|
||
|
|
import java.nio.charset.StandardCharsets;
|
||
|
|
import java.security.*;
|
||
|
|
import java.security.interfaces.RSAPrivateKey;
|
||
|
|
import java.security.interfaces.RSAPublicKey;
|
||
|
|
import java.security.spec.PKCS8EncodedKeySpec;
|
||
|
|
import java.security.spec.X509EncodedKeySpec;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* RSA密码工具
|
||
|
|
*/
|
||
|
|
public class RsaUtil {
|
||
|
|
private static final String publicKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCRehqqa1eLnTL3BSRq3zmB+Yw7nLAMAZ0G+FpoGP0eFLc0JVC2P2sfkCJJjH2cOmoLcUFjHfDcHzMNyl4wmNTMeXhpvK3v2ha1ufZnGmoMd9d4+1R/t/pZdxXXzkQMN2012X/KIojluEJmrIXLUM0zjOTABSMlTY6TPfSrmuXyGwIDAQAB";
|
||
|
|
private static final String privateKey = "MIICdQIBADANBgkqhkiG9w0BAQEFAASCAl8wggJbAgEAAoGBAJF6GqprV4udMvcFJGrfOYH5jDucsAwBnQb4WmgY/R4UtzQlULY/ax+QIkmMfZw6agtxQWMd8NwfMw3KXjCY1Mx5eGm8re/aFrW59mcaagx313j7VH+3+ll3FdfORAw3bTXZf8oiiOW4QmashctQzTOM5MAFIyVNjpM99Kua5fIbAgMBAAECgYBtr3fNiHOQg6z6jLkBj18nvYTbKW+fDdRcfgKEPFsURCBBw+TQFI7rVFxVwgSvN2ffSrw3RErnXOq8ehl8YK8IfPWleMTXkuGB4QDptou3S7aJ4OlK+p5YOsjVEgnzJ3J7gPUYJRGAX59G+EbEnHKvoTQQjlQ4EaFqp77ba+SN+QJBAM1rre1TSSIuZd7biS4d0znVDRA3Zv/AnpmSaLZODU6p70DnPn3WCf5LNJGsS+Bt1xkq/EHzhpspjy/IfJrZOO8CQQC1S/vrpm8+WsXp8FAlvuQ+UOR1UlMs4WMokVMSOcPcgG2Iab8f1UcD9GptjBjORKiloQBpjG/+FXp2VJPd7yGVAkBvmRRAXoLYwwQs8m+wUhuyy3/xU2ftgaOoItYoVHb+SWvlgrt8eY+sSwcgLM57+rBkx+mLmtWB7i4P84deSKyZAkBYJ5FgnXY8MLFJtoOSRwb+0iC0d4pgKVwo7rkhBJubTYt1KE458V/tqVxS1it9qN8EYowrxpDyUIlSnn+kC9IdAkAH6TkJw2tb4N7rIvcZWjd74EBNQdP0FzvZWU2rE6SGqj87yWDKOKGKWu/hz84DCSkyltII4uB+YasJhsgsJnjb";
|
||
|
|
|
||
|
|
public static void main(String[] args) throws Exception {
|
||
|
|
// System.out.println("\n");
|
||
|
|
// RsaKeyPair keyPair = generateKeyPair();
|
||
|
|
// System.out.println("公钥:" + keyPair.getPublicKey());
|
||
|
|
// System.out.println("私钥:" + keyPair.getPrivateKey());
|
||
|
|
//System.out.println("\n");
|
||
|
|
String text1 = encryptByPublicKey("123qwe");
|
||
|
|
// String text1 = encryptByPrivateKey("123");
|
||
|
|
System.out.println(text1);
|
||
|
|
//String text1 = "Bs+MJo8yrGxvwD/G5QGTihU6lh/PjaDKQNxBDs66GQrNXoX0ttzX0grYFxWh8MibHkHikkw8rLsoxXk6bQOzs0eHst/yzj823sTZRDBYdx0oYoEKeda7s7xjYheq4nBnHJn8HVkQbYtchddRRfKVcwyBrUQquhlnzmnfZQ98QiU=";
|
||
|
|
// String text1 = "CP7GpqY3Tw3DSazqwMHvlRys0/RARfjNL0fgw5IEXEHdZB2K+7G8Ro+wVAm9fiViMba3KcQidG5g244RrpD0HJc/g6PVW4kn81xx1WHjW0SgVUUrsjj4RgHUnUyY/lN0506Ng945svGrGd6pXb+XdtETv4ZKau5EHdR6L/TOR8I=";
|
||
|
|
// String text1 = "dWRzqk2DC/rm55B+m1Is4mwTnPoc3qoDIZ41Plie6s4vsJwDdHzoWhnIx1pfdFAwEZ3eQ6FV7tcfHJwHDFzNal15e1c2+EFThmWdqdgGNCXttH/c/fsmzaXda5kQRzQxqbhvF96KmJKtshzq23766iLV1Vm3KUsvB6c7bP5yKYc=";
|
||
|
|
//String text2 = decryptByPrivateKey(text1);
|
||
|
|
// String text2 = decryptByPrivateKey("R/t6KFVPbCgYSVlYJ4ULuFFwn29/RlLo/IBeXqdtwm0ulIcG0iTDqbCNcQZChE8iN/AL7S4MoGL/HWZArEqAz4BdDmYgLggykX/hhu1fue7tpefyVEnW5TF27rFBCxo1cH0kB1VP0FbwlWHDqG1GCnHOBZbS6j4yUTqu41WBmq4=");
|
||
|
|
// System.out.println(text2);
|
||
|
|
}
|
||
|
|
|
||
|
|
public static String encryptByPublicKey(String text) throws Exception {
|
||
|
|
X509EncodedKeySpec x509EncodedKeySpec2 = new X509EncodedKeySpec(Base64.decodeBase64(publicKey));
|
||
|
|
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
|
||
|
|
PublicKey publicKey = keyFactory.generatePublic(x509EncodedKeySpec2);
|
||
|
|
Cipher cipher = Cipher.getInstance("RSA");
|
||
|
|
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
|
||
|
|
byte[] result = cipher.doFinal(text.getBytes("UTF-8"));
|
||
|
|
return Base64.encodeBase64String(result);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 私钥加密
|
||
|
|
*
|
||
|
|
* @param text 待加密的信息
|
||
|
|
* @return /
|
||
|
|
* @throws Exception /
|
||
|
|
*/
|
||
|
|
public static String encryptByPrivateKey(String text) throws Exception {
|
||
|
|
PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(Base64.decodeBase64(privateKey));
|
||
|
|
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
|
||
|
|
PrivateKey privateKey = keyFactory.generatePrivate(pkcs8EncodedKeySpec);
|
||
|
|
Cipher cipher = Cipher.getInstance("RSA");
|
||
|
|
cipher.init(Cipher.ENCRYPT_MODE, privateKey);
|
||
|
|
byte[] result = cipher.doFinal(text.getBytes(StandardCharsets.UTF_8));
|
||
|
|
return Base64.encodeBase64String(result);
|
||
|
|
}
|
||
|
|
|
||
|
|
public static String encryptByPrivateKey(String text,String privateKey) throws Exception {
|
||
|
|
PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(Base64.decodeBase64(privateKey));
|
||
|
|
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
|
||
|
|
PrivateKey privateKey1 = keyFactory.generatePrivate(pkcs8EncodedKeySpec);
|
||
|
|
Cipher cipher = Cipher.getInstance("RSA");
|
||
|
|
cipher.init(Cipher.ENCRYPT_MODE, privateKey1);
|
||
|
|
byte[] result = cipher.doFinal(text.getBytes(StandardCharsets.UTF_8));
|
||
|
|
|
||
|
|
return Base64.encodeBase64String(result);
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
public static String decryptByPrivateKey(String text) throws Exception {
|
||
|
|
PKCS8EncodedKeySpec pkcs8EncodedKeySpec5 = new PKCS8EncodedKeySpec(Base64.decodeBase64(privateKey));
|
||
|
|
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
|
||
|
|
PrivateKey privateKey = keyFactory.generatePrivate(pkcs8EncodedKeySpec5);
|
||
|
|
Cipher cipher = Cipher.getInstance("RSA");
|
||
|
|
cipher.init(Cipher.DECRYPT_MODE, privateKey);
|
||
|
|
byte[] result = cipher.doFinal(Base64.decodeBase64(text));
|
||
|
|
return new String(result);
|
||
|
|
}
|
||
|
|
|
||
|
|
public static String decryptByPublicKey(String text) throws Exception {
|
||
|
|
X509EncodedKeySpec x509EncodedKeySpec2 = new X509EncodedKeySpec(Base64.decodeBase64(publicKey));
|
||
|
|
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
|
||
|
|
PublicKey publicKey = keyFactory.generatePublic(x509EncodedKeySpec2);
|
||
|
|
Cipher cipher = Cipher.getInstance("RSA");
|
||
|
|
cipher.init(Cipher.DECRYPT_MODE, publicKey);
|
||
|
|
byte[] result = cipher.doFinal(Base64.decodeBase64(text));
|
||
|
|
return new String(result);
|
||
|
|
}
|
||
|
|
|
||
|
|
public static String decryptByPublicKey(String text,String publicKey) throws Exception {
|
||
|
|
X509EncodedKeySpec x509EncodedKeySpec2 = new X509EncodedKeySpec(Base64.decodeBase64(publicKey));
|
||
|
|
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
|
||
|
|
PublicKey publicKey1 = keyFactory.generatePublic(x509EncodedKeySpec2);
|
||
|
|
Cipher cipher = Cipher.getInstance("RSA");
|
||
|
|
cipher.init(Cipher.DECRYPT_MODE, publicKey1);
|
||
|
|
byte[] result = cipher.doFinal(Base64.decodeBase64(text));
|
||
|
|
return new String(result);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 构建RSA密钥对512
|
||
|
|
*
|
||
|
|
* @return /
|
||
|
|
* @throws NoSuchAlgorithmException /
|
||
|
|
*/
|
||
|
|
public static RsaKeyPair generateKeyPair() throws NoSuchAlgorithmException {
|
||
|
|
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
|
||
|
|
keyPairGenerator.initialize(512);
|
||
|
|
KeyPair keyPair = keyPairGenerator.generateKeyPair();
|
||
|
|
RSAPublicKey rsaPublicKey = (RSAPublicKey) keyPair.getPublic();
|
||
|
|
RSAPrivateKey rsaPrivateKey = (RSAPrivateKey) keyPair.getPrivate();
|
||
|
|
String publicKeyString = Base64.encodeBase64String(rsaPublicKey.getEncoded());
|
||
|
|
String privateKeyString = Base64.encodeBase64String(rsaPrivateKey.getEncoded());
|
||
|
|
return new RsaKeyPair(publicKeyString, privateKeyString);
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
// 从公钥生成钱包地址
|
||
|
|
public static String generateBlockchainAddress(String publicKey) throws NoSuchAlgorithmException {
|
||
|
|
MessageDigest md = MessageDigest.getInstance("SHA-256");
|
||
|
|
|
||
|
|
byte[] hash = md.digest(publicKey.getBytes(StandardCharsets.UTF_8));
|
||
|
|
|
||
|
|
// Base58编码是一种常用于区块链地址的编码方式
|
||
|
|
String blockchainAddress = java.util.Base64.getEncoder().encodeToString(hash);
|
||
|
|
|
||
|
|
return blockchainAddress;
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
public static String calculateMD5(String input) {
|
||
|
|
try {
|
||
|
|
MessageDigest md = MessageDigest.getInstance("MD5");
|
||
|
|
byte[] hashBytes = md.digest(input.getBytes(StandardCharsets.UTF_8));
|
||
|
|
StringBuilder sb = new StringBuilder();
|
||
|
|
for (byte b : hashBytes) {
|
||
|
|
sb.append(String.format("%02X", b));
|
||
|
|
}
|
||
|
|
return sb.toString();
|
||
|
|
} catch (NoSuchAlgorithmException e) {
|
||
|
|
e.printStackTrace();
|
||
|
|
return null;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
public static String formatHash(String hash) {
|
||
|
|
StringBuilder formattedHash = new StringBuilder();
|
||
|
|
for (int i = 0; i < hash.length(); i += 2) {
|
||
|
|
formattedHash.append(hash.substring(i, i + 2));
|
||
|
|
if (i < hash.length() - 2) {
|
||
|
|
formattedHash.append("-");
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return formattedHash.toString();
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
/**
|
||
|
|
* RSA密钥对对象
|
||
|
|
*/
|
||
|
|
public static class RsaKeyPair {
|
||
|
|
|
||
|
|
private final String publicKey;
|
||
|
|
private final String privateKey;
|
||
|
|
|
||
|
|
public RsaKeyPair(String publicKey, String privateKey) {
|
||
|
|
this.publicKey = publicKey;
|
||
|
|
this.privateKey = privateKey;
|
||
|
|
}
|
||
|
|
|
||
|
|
public String getPublicKey() {
|
||
|
|
return publicKey;
|
||
|
|
}
|
||
|
|
|
||
|
|
public String getPrivateKey() {
|
||
|
|
return privateKey;
|
||
|
|
}
|
||
|
|
|
||
|
|
}
|
||
|
|
}
|