반응형
RSA 키생성 후 생성된 키로 암복호화 테스트 해 보았다.
package com.rsatest.rsaTest;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.file.Files;
import java.security.InvalidKeyException;
import java.security.KeyFactory;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.spec.DSAPrivateKeySpec;
import java.security.spec.DSAPublicKeySpec;
import java.security.spec.EncodedKeySpec;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import org.apache.commons.codec.binary.Base64;
//
// RSA 암복호화 및 테스트
//
// Created by netcanis on 2019/04/29.
// Copyright © 2019 netcanis. All rights reserved.
//
public class RSAEncryption
{
private Cipher cipher;
public RSAEncryption() throws NoSuchAlgorithmException, NoSuchPaddingException{
this.cipher = Cipher.getInstance("RSA");
}
// 주어진 파일을 byte array로 읽기
public byte[] getFileInBytes(File f) throws IOException{
FileInputStream fis = new FileInputStream(f);
byte[] fbytes = new byte[(int) f.length()];
fis.read(fbytes);
fis.close();
return fbytes;
}
// X509EncodedKey
public PublicKey getPublic(String filename) throws Exception {
byte[] keyBytes = Files.readAllBytes(new File(filename).toPath());
X509EncodedKeySpec spec = new X509EncodedKeySpec(keyBytes);
KeyFactory kf = KeyFactory.getInstance("RSA");
return kf.generatePublic(spec);
}
// PKCS8EncodedKeySpec
public PrivateKey getPrivate(String filename) throws Exception {
byte[] keyBytes = Files.readAllBytes(new File(filename).toPath());
//byte[] decoded = Base64.getDecoder().decode(keyBytes);
//println(new String(decoded)); // Outputs "Hello"
//Base64 codec = new Base64();
//byte[] decoded = codec.decode(keyBytes);
//System.out.println( new String(decoded) ); // Outputs "Hello"
PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(keyBytes);
KeyFactory kf = KeyFactory.getInstance("RSA");
return kf.generatePrivate(spec);
}
// PKCS1
// public PrivateKey getPrivate2(String filename) throws Exception {
// PEMParser pemParser = new PEMParser(new FileReader(privateKeyFile));
// JcaPEMKeyConverter converter = new JcaPEMKeyConverter().setProvider("BC");
// Object object = pemParser.readObject();
// KeyPair kp = converter.getKeyPair((PEMKeyPair) object);
// PrivateKey privateKey = kp.getPrivate();
// }
// public key로 암호화
public String encrypt(String str, PublicKey key) throws NoSuchAlgorithmException, NoSuchPaddingException, UnsupportedEncodingException, IllegalBlockSizeException, BadPaddingException, InvalidKeyException{
this.cipher.init(Cipher.ENCRYPT_MODE, key);
return Base64.encodeBase64String(cipher.doFinal(str.getBytes("UTF-8")));
}
// private key로 복호화
public String decrypt(String str, PrivateKey key) throws InvalidKeyException, UnsupportedEncodingException, IllegalBlockSizeException, BadPaddingException{
this.cipher.init(Cipher.DECRYPT_MODE, key);
return new String(cipher.doFinal(Base64.decodeBase64(str)), "UTF-8");
}
/*
String hexString = "01020304FF11";
byte[] ByteArray = hexStringToByteArray(hexString);
String str = byteArrayToHexString(ByteArray);
System.out.println(str);
*/
// hex to byte[]
public byte[] hexStringToByteArray(String s) {
int len = s.length();
byte[] data = new byte[len / 2];
for (int i = 0; i < len; i += 2) {
data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
+ Character.digit(s.charAt(i+1), 16));
}
return data;
}
// byte[] to hex
public String byteArrayToHexString(byte[] bytes){
StringBuilder sb = new StringBuilder();
for(byte b : bytes){
sb.append(String.format("%02X", b&0xff));
}
return sb.toString();
}
public static void main(String[] args) throws Exception {
RSAEncryption ac = new RSAEncryption();
// 공개키
PublicKey publicKey = ac.getPublic("KeyPair/public.key");
// 개인키
PrivateKey privateKey = ac.getPrivate("KeyPair/private.key");
// 원문
String msg = "test message";
System.out.println("----------------------------------------------------");
System.out.println(":::: TEST 암호화 및 복호화 ::::");
// 암호화 원문 BASE64
String encryptedString = ac.encrypt(msg, publicKey);
// 복호화
String decryptedString = ac.decrypt(encryptedString, privateKey);
System.out.println("----------------------------------------------------");
System.out.println(
"원문 = " + msg + "\n" +
"공개키 암호화 = " + encryptedString + "\n" +
"개인키 복호화 = " + decryptedString
);
System.out.println("----------------------------------------------------");
}
}
사용방법 :
Encode or decode byte arrays:
byte[] encoded = Base64.getEncoder().encode("Hello".getBytes());
println(new String(encoded)); // Outputs "SGVsbG8="
byte[] decoded = Base64.getDecoder().decode(encoded);
println(new String(decoded)) // Outputs "Hello"
Or if you just want the strings:
String encoded = Base64.getEncoder().encodeToString("Hello".getBytes());
println(encoded); // Outputs "SGVsbG8="
String decoded = new String(Base64.getDecoder().decode(encoded.getBytes()));
println(decoded) // Outputs "Hello"
Base64 codec = new Base64();
byte[] encoded = codec.encode("Hello".getBytes());
println(new String(encoded)); // Outputs "SGVsbG8="
byte[] decoded = codec.decode(encoded);
println(new String(decoded)) // Outputs "Hello"
openssl rsa -text -in public.key -inform DER -pubin
-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCCH+bC/6rGV8I9NgqCUTpcUcUX
q15NC8N86kHtSPFl03dwwGIO3hPQVZyMiVwJYq8gz0ZFacf27tkvgphqJOAf/ckY
PKb52+8bEXNowbPhKqiwPaclEmtvm8MJplxZKa/Y+IhYitRRI7aVow841PRxC/nA
Z8AEiw5xkfBMsy9jdwIDAQAB
-----END PUBLIC KEY-----
openssl rsa -text -in private.key -inform DER
-----BEGIN RSA PRIVATE KEY-----
MIICXQIBAAKBgQCCH+bC/6rGV8I9NgqCUTpcUcUXq15NC8N86kHtSPFl03dwwGIO
3hPQVZyMiVwJYq8gz0ZFacf27tkvgphqJOAf/ckYPKb52+8bEXNowbPhKqiwPacl
Emtvm8MJplxZKa/Y+IhYitRRI7aVow841PRxC/nAZ8AEiw5xkfBMsy9jdwIDAQAB
AoGAGfdvvz3xwD7G/hwIoNTAxqdyozSJTuqoCgddPJKvyVgXn6Jkbv8WCR7sbIcE
nMLam7uBFFz1kGs9X+O5soTFTJHvyN1QyhvWODd2Gqqheh2fHqNunjegBJKnMVDS
ZCF+0HQe1SBub2D/WkPRMNb1O24v0FaR9LalaZAPqgM9eYECQQDpl+lSEddOsB7f
jX6QS9Z56s8St2Lql/HVlEsyZ3RZIHA+oGMuHaK124fo/CEQxH/SHouVCrjdgiR1
2rLbZY1hAkEAjps5Yw2SHb3AOUqG7x+FmVhb+DaMR+KZZpVlJvRxy4fQpoB2GdC4
Okf7idpQGZJHXeOI8sQbtS4GTFT7nwgH1wJBAKS2natlMTEnN13jZA9TqpSlYgaM
kY9iPQChZLTSBlSibwN6DPWDqCwb9KXww1ATfx4ms74QpXJsRlFkOSJDZYECQD4I
T6JC2PiO/frt7FxAPgFCal9CeysZ6tiwy9tyIb4DSkPjRXxdSoYvob+Jc6Zr9xdk
TsIrhFH/v0WdAgdfmukCQQCRnRh0nMw5GhjkP9pJTLZo4j8H/kp7lY0LDBLzo1cW
+nvyJ4Xg+Jgig5/lsn3QaIckocfnxcq10fGk+bFr67gM
-----END RSA PRIVATE KEY-----
UBpay RSA 암호화 결과 (위의 public key로 암호화)
원문 = test message
원문 HEXA = 74657374206D657373616765
암호화 원문 BASE64 = JLvhRkfskdQNcJ5mjMIy9hcCrfM1QDL5Qy/4tsnW8pG7QXvPrwmo7OxiTiC4FVBMw/Ae/NeLJCxY2jr9uRvKXFxqSrdrjBoO3QL+oFpKbldcvTRUzXCRsWzKzeM8v3FOclWgkZYZMzGq8PQQu3PgFKHCVMnX7//UE7aguHvLiJo=
반응형
'개발 > Note' 카테고리의 다른 글
20진수 변환 (0) | 2021.09.16 |
---|---|
root-level 디렉토리에 폴더, symbolic link 생성 방법 (0) | 2021.03.15 |
RSA key 파일 생성 (0) | 2021.02.05 |
용어 정리 (0) | 2021.02.05 |
Korea Bank Codes (0) | 2021.02.05 |