반응형
Java Code
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.RSAPublicKeySpec;
import javax.crypto.Cipher;
import xxx.MsgException;
public class Rsa {
public static String decryptRsa(PrivateKey privateKey, String securedValue) {
try {
Cipher cipher = Cipher.getInstance("RSA");
byte[] encryptedBytes = ByteUtil.hexToByteArray(securedValue);
cipher.init(Cipher.DECRYPT_MODE, privateKey);
byte[] decryptedBytes = cipher.doFinal(encryptedBytes);
String decryptedValue = new String(decryptedBytes, "UTF-8");
return decryptedValue;
} catch (Exception e) {
throw new MsgException("복호화 실패", e);
}
}
public static String encryptRsa(PublicKey publicKey, String plainText) {
try {
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
byte[] encryptedBytes = cipher.doFinal(plainText.getBytes("UTF-8"));
return ByteUtil.byteArrayToHex(encryptedBytes);
} catch (Exception e) {
throw new MsgException("암호화 실패", e);
}
}
public static RsaKey generateKeyPair() {
try {
KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA");
generator.initialize(1024);
KeyPair keyPair = generator.genKeyPair();
PublicKey publicKey = keyPair.getPublic();
PrivateKey privateKey = keyPair.getPrivate();
return new RsaKey(publicKey, privateKey);
} catch (Exception e) {
throw new MsgException("암호화 키 생성 실패", e);
}
}
public static void main(String[] args) {
RsaKey rsaKey = Rsa.generateKeyPair();
String plainText = "머0ㄴㅇㄹ92ㅡㅔㅇㄹ-ㄴ90ㅎ-ㅕ3ㄷ2sd22!";
String encryptedText = Rsa.encryptRsa(rsaKey.getPublicKey(), plainText);
System.out.println(rsaKey.getPublicKeyModulus());
System.out.println(rsaKey.getPublicKeyExponent());
System.out.println(encryptedText);
System.out.println(Rsa.decryptRsa(rsaKey.getPrivateKey(), encryptedText));
}
public static class RsaKey {
private PublicKey publicKey;
private PrivateKey privateKey;
private String publicKeyModulus;
private String publicKeyExponent;
public RsaKey(PublicKey publicKey, PrivateKey privateKey) throws NoSuchAlgorithmException, InvalidKeySpecException {
this.publicKey = publicKey;
this.privateKey = privateKey;
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
RSAPublicKeySpec publicSpec = (RSAPublicKeySpec) keyFactory.getKeySpec(publicKey, RSAPublicKeySpec.class);
String publicKeyModulus = publicSpec.getModulus().toString(16);
String publicKeyExponent = publicSpec.getPublicExponent().toString(16);
this.publicKeyModulus = publicKeyModulus;
this.publicKeyExponent = publicKeyExponent;
}
public PrivateKey getPrivateKey() {
return privateKey;
}
public String getPublicKeyModulus() {
return publicKeyModulus;
}
public String getPublicKeyExponent() {
return publicKeyExponent;
}
public PublicKey getPublicKey() {
return publicKey;
}
}
}
Result
8d7d86cb63894b38c71727053b29adaca78ae9b5eb847bae84e9f0c5fa6c678e3b083cf0ea0a3f07365809f93ed9329291e56137fdb8cc9f89019d5f3532c8a49ae518256d1f7dc770256bad809453c186e0011421c512efadc18e4927eaaeeb355fedc73df84a117ee0f98f162de216651f1453b5bec0ff3202596ca637b055
10001
09632b3b97cc17c724d030c4bd1bd17ef5176aa80feb99cc01e57d325b1af0d5eb8528375c495d4cabf6d209ea93fff41f5843a2505c6cd31f71d4a0f23ca1d37af4272dbe66a840881d93970ce989fc0ca612eb5eaa469740a00a09138038f7198f29df78d751d8e3ea466130cf12d8675cb800e6681dcfcbbb4cbd3676559f
머0ㄴㅇㄹ92ㅡㅔㅇㄹ-ㄴ90ㅎ-ㅕ3ㄷ2sd22!
Html and Javascript code
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script type="text/javascript" src="http://www-cs-students.stanford.edu/~tjw/jsbn/rsa.js"></script>
<script type="text/javascript" src="http://www-cs-students.stanford.edu/~tjw/jsbn/jsbn.js"></script>
<script type="text/javascript" src="http://www-cs-students.stanford.edu/~tjw/jsbn/prng4.js"></script>
<script type="text/javascript" src="http://www-cs-students.stanford.edu/~tjw/jsbn/rng.js"></script>
<script>
function encrypt() {
var rsa = new RSAKey();
var RSAModulus = document.getElementById("RSAModulus").value;
var RSAExponent = document.getElementById("RSAExponent").value;
var plainText = document.getElementById("plainText").value;
rsa.setPublic(RSAModulus, RSAExponent);
document.getElementById("encryptedText").value = rsa.encrypt(plainText);
}
function decrypt() {
var rsa = new RSAKey();
var RSAModulus = document.getElementById("RSAModulus").value;
var RSAExponent = document.getElementById("RSAExponent").value;
rsa.setPublic(RSAModulus, RSAExponent);
}
</script>
</head>
<body>
<dl>
<dt>RSAModulus</dt>
<dd>
<textarea id="RSAModulus">8d7d86cb63894b38c71727053b29adaca78ae9b5eb847bae84e9f0c5fa6c678e3b083cf0ea0a3f07365809f93ed9329291e56137fdb8cc9f89019d5f3532c8a49ae518256d1f7dc770256bad809453c186e0011421c512efadc18e4927eaaeeb355fedc73df84a117ee0f98f162de216651f1453b5bec0ff3202596ca637b055</textarea>
</dd>
<dt>RSAExponent</dt>
<dd>
<textarea id="RSAExponent">10001</textarea>
</dd>
<dt>암호화</dt>
<dd>
<textarea id="plainText">머0ㄴㅇㄹ92ㅡㅔㅇㄹ-ㄴ90ㅎ-ㅕ3ㄷ2sd22!</textarea> -> <textarea id="encryptedText"></textarea>
</dd>
<dd>
<button onclick="encrypt();">암호화</button>
</dd>
</dl>
</body>
</html>
Result
---------------------------------
공개키 / 개인키 문자열로 처리.
반응형
'스크립트' 카테고리의 다른 글
jquery self unwrap (0) | 2018.11.06 |
---|---|
jquery 확장 enter key function (0) | 2018.11.05 |
javascript cookie getCookie setCookie (0) | 2018.11.05 |
netstat unique ip (0) | 2018.09.10 |
aes256 cbc javascript and java (0) | 2018.08.09 |
ajax multiple parameter request list (0) | 2018.07.24 |
javascript object size (0) | 2018.07.23 |
jquery ajax ie no transport error (0) | 2018.07.17 |