package com.mollen.utils;
import javax.crypto.Cipher;
import java.io.ByteArrayOutputStream;
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.Signature;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;
public class RsaUtils {
public static final String KEY_ALGORITHM = "RSA";
public static final String SIGNATURE_ALGORITHM = "SHA256withRSA";
private static final int MAX_ENCRYPT_BLOCK = 117;
private static final int MAX_DECRYPT_BLOCK = 128;
public static byte[] sign(byte[] data, String privateKey) {
try {
byte[] keyBytes = Base64.getDecoder().decode(privateKey);
PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(keyBytes);
KeyFactory keyFactory;
keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
PrivateKey privateK = keyFactory.generatePrivate(pkcs8KeySpec);
Signature signature = Signature.getInstance(SIGNATURE_ALGORITHM);
signature.initSign(privateK);
signature.update(data);
return signature.sign();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static boolean verify(byte[] data, String publicKey, byte[] sign) {
try {
byte[] keyBytes = Base64.getDecoder().decode(publicKey);
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes);
KeyFactory keyFactory;
keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
PublicKey publicK = keyFactory.generatePublic(keySpec);
Signature signature = Signature.getInstance(SIGNATURE_ALGORITHM);
signature.initVerify(publicK);
signature.update(data);
return signature.verify(sign);
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
public static byte[] encryptByPublicKey(byte[] data, String publicKey) {
try {
byte[] keyBytes = Base64.getDecoder().decode(publicKey);
X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(keyBytes);
KeyFactory keyFactory;
keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
Key publicK = keyFactory.generatePublic(x509KeySpec);
Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
cipher.init(Cipher.ENCRYPT_MODE, publicK);
return getBytes(data, cipher, MAX_ENCRYPT_BLOCK);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static byte[] decryptByPrivateKey(byte[] encryptedData, String privateKey) {
try {
byte[] keyBytes = Base64.getDecoder().decode(privateKey);
PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(keyBytes);
KeyFactory keyFactory;
keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
Key privateK = keyFactory.generatePrivate(pkcs8KeySpec);
Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
cipher.init(Cipher.DECRYPT_MODE, privateK);
return getBytes(encryptedData, cipher, MAX_DECRYPT_BLOCK);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
private static byte[] getBytes(byte[] contentData, Cipher cipher, int maxBlockSize) throws Exception {
ByteArrayOutputStream out = new ByteArrayOutputStream();
int inputLen = contentData.length;
int offSet = 0;
byte[] cache;
int i = 0;
while (inputLen - offSet > 0) {
if (inputLen - offSet > maxBlockSize) {
cache = cipher.doFinal(contentData, offSet, maxBlockSize);
}
else {
cache = cipher.doFinal(contentData, offSet, inputLen - offSet);
}
out.write(cache, 0, cache.length);
i++;
offSet = i * maxBlockSize;
}
byte[] decryptedData = out.toByteArray();
out.close();
return decryptedData;
}
public static Map<String,String> getGeneraKey(){
try{
KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance(KEY_ALGORITHM);
keyPairGen.initialize(1024);
KeyPair keyPair = keyPairGen.generateKeyPair();
byte[] publicKey = keyPair.getPublic().getEncoded();
byte[] privateKey = keyPair.getPrivate().getEncoded();
String publicKe = Base64.getEncoder().encodeToString(publicKey);
String privateKe = Base64.getEncoder().encodeToString(privateKey);
Map keyMap = new HashMap(2);
System.out.println("--------Generate secret key------");
System.out.println("|-- publicKey: " + publicKe);
System.out.println("|-- privateKey: " + privateKe);
System.out.println("---------------------------------");
keyMap.put("rsa_pub", publicKe);
keyMap.put("rsa", privateKe);
return keyMap;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
package com.mollen.utils;
import java.util.Base64;
import java.util.Map;
public class RsaTest {
public static void main(String[] args) {
Map<String, String> generaKey = RsaUtils.getGeneraKey();
String privateKey = generaKey.get("rsa");
String publicKey = generaKey.get("rsa_pub");
String message = "My public key:" + publicKey ;
byte[] deMessage = Base64.getEncoder().encode(message.getBytes());
byte[] sign = RsaUtils.sign(deMessage, privateKey);
boolean verify = RsaUtils.verify(deMessage, publicKey, sign);
System.out.println("|-- Response message:" + verify);
String password = "Test_123";
System.out.println("|-- Resource password:" + password);
byte[] encode = Base64.getEncoder().encode(password.getBytes());
byte[] encryptData = RsaUtils.encryptByPublicKey(encode, publicKey);
byte[] decryptData = RsaUtils.decryptByPrivateKey(encryptData, privateKey);
byte[] decode = Base64.getDecoder().decode(decryptData);
System.out.println("|-- PassWord: " + new String(decode));
}
}
|