import java.security.*;
import java.security.spec.*;
import javax.crypto.*;
public static HashMap genarateRSAKeyPair() throws NoSuchAlgorithmException {
PublicKey publicKey = null;
PrivateKey privateKey = null;
SecureRandom secureRandom = new SecureRandom();
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
keyPairGenerator.initialize(512, secureRandom);
KeyPair keyPair = keyPairGenerator.genKeyPair();
publicKey = keyPair.getPublic();
privateKey = keyPair.getPrivate();
HashMap map = new HashMap();
map.put("publickey", publicKey);
map.put("privatekey", privateKey);
return map;
}
public static byte[] rsaEncrypt(PublicKey publicKey, byte[] bPlain) throws NoSuchPaddingException, NoSuchAlgorithmException, IllegalBlockSizeException, BadPaddingException, InvalidKeyException {
Cipher cipher = Cipher.getInstance("RSA");
// 공개키 이용 암호화
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
byte[] bCipher = cipher.doFinal(bPlain);
return bCipher;
}
public static byte[] rsaDecrypt(PrivateKey privateKey, byte[] bEncyptedData) throws NoSuchPaddingException, NoSuchAlgorithmException, IllegalBlockSizeException, BadPaddingException, InvalidKeyException {
Cipher cipher = Cipher.getInstance("RSA");
// 개인키 이용 복호화
cipher.init(Cipher.DECRYPT_MODE, privateKey);
byte[] bPlain = cipher.doFinal(bEncyptedData);
return bPlain;
}
public static byte[] rsaSign(PrivateKey privateKey, byte[] bPlain) throws NoSuchPaddingException, NoSuchAlgorithmException, IllegalBlockSizeException, BadPaddingException, InvalidKeyException, SignatureException {
Cipher cipher = Cipher.getInstance("RSA");
// sign
Signature rsa = Signature.getInstance("SHA256withRSA");
rsa.initSign(privateKey);
rsa.update(SHA256(bPlain));
byte[] ds = rsa.sign();
return ds;
}
public static boolean rsaSignVerify(PublicKey publicKey, byte[] bPlain, byte[] bSign) throws NoSuchPaddingException, NoSuchAlgorithmException, IllegalBlockSizeException, BadPaddingException, InvalidKeyException, SignatureException {
Cipher cipher = Cipher.getInstance("RSA");
Signature rsa = Signature.getInstance("SHA256withRSA");
rsa.initVerify(publicKey);
rsa.update(SHA256(bPlain));
boolean rtnVal = rsa.verify(bSign);
return rtnVal;
}
HashMap key = UtilClass.genarateRSAKeyPair();
String plain = "000001" + System.lineSeparator() +
"192.168.0.8" + System.lineSeparator() +
"90-78-41-57-56-2F" + System.lineSeparator() +
"5" + System.lineSeparator() +
"20231231" + System.lineSeparator();
byte[] baSign = UtilClass.rsaSign((PrivateKey) key.get("privatekey"), plain.getBytes());
String license = plain + UtilClass.Base64Encode(baSign);
String ip =UtilClass.getAddress1();
String mac = UtilClass.getMacAddress1();
String[] data = license.split(System.lineSeparator());
boolean bResult = UtilClass.rsaSignVerify((PublicKey) key.get("publickey"), plain.getBytes(), UtilClass.Base64Decode(data[5]));
https://link.coupang.com/a/NeJdE
이 포스팅은 쿠팡 파트너스 활동의 일환으로, 이에 따른 일정액의 수수료를 제공받습니다.