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.SecureRandom;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;

import javax.crypto.Cipher;

public class MoonRsaFullSample {

    private static String publicKey = "";
    private static String privateKey = "";

    public static void main(String[] args) throws Exception {

        createRsaGenKey(); // key 생성

        String encStr = "가나다라마ABCDefghi되는거냐";

        System.out.println(encStr);

        String decStr = rsaEnc(encStr);
        System.out.println(decStr);

        String finalEncStr = rsaDec(decStr);
        System.out.println(finalEncStr);

    }

    /**
     * "smart" 를 이용해 random 값을 추출하여 2048bit에 해당하는 키 생성
     * @throws Exception
     */
    private static void createRsaGenKey() throws Exception{

        String pubkey = "smart";

        SecureRandom random = new SecureRandom(pubkey.getBytes());
        KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA","SunJSSE"); // OK
        generator.initialize(2048, random); // 여기에서는 2048 bit 키를 생성하였음

        KeyPair pair = generator.generateKeyPair();
        Key pubKey = pair.getPublic(); // Kb(pub) 공개키
        Key privKey = pair.getPrivate();// Kb(pri) 개인키

        System.out.println("pubKeyHex:\n" + byteArrayToHex(pubKey.getEncoded())+"\n");
        System.out.println("privKeyHex:\n" + byteArrayToHex(privKey.getEncoded())+"\n");

        publicKey = byteArrayToHex(pubKey.getEncoded());
        privateKey = byteArrayToHex(privKey.getEncoded());

    }

    /**
     * 암호화
     * @param encStr
     * @return byteArrayToHex(cipherText)
     * @throws Exception
     */
    private static String rsaEnc(String encStr) throws Exception{

        Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1PADDING", "SunJCE"); // 알고리즘 명 / Cipher 알고리즘 mode / padding

        X509EncodedKeySpec ukeySpec = new X509EncodedKeySpec(hexToByteArray(publicKey));

        KeyFactory ukeyFactory = KeyFactory.getInstance("RSA");

        PublicKey publickey = null;

        try {
            // PublicKey에 공용키 값 설정
            publickey = ukeyFactory.generatePublic(ukeySpec);

        } catch (Exception e) {
            e.printStackTrace();
        }

        byte[] input = encStr.getBytes();
        cipher.init(Cipher.ENCRYPT_MODE, publickey);

        byte[] cipherText = cipher.doFinal(input);

        return byteArrayToHex(cipherText);

    }

    /**
     * 복호화
     * @param byteArrayToHex(cipherText) ==> decStr
     * @return
     * @throws Exception
     */
    private static String rsaDec(String decStr) throws Exception{

        Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1PADDING", "SunJCE");

        PKCS8EncodedKeySpec rkeySpec = new PKCS8EncodedKeySpec(hexToByteArray(privateKey));
        KeyFactory rkeyFactory = KeyFactory.getInstance("RSA");

        PrivateKey privateKey = null;

        try {
            privateKey = rkeyFactory.generatePrivate(rkeySpec);
        } catch (Exception e) {
            e.printStackTrace();
        }

        // 복호
        cipher.init(Cipher.DECRYPT_MODE, privateKey);
        byte[] plainText = cipher.doFinal(hexToByteArray(decStr));

        String returnStr = new String(plainText);

        return returnStr;
    }



    // hex string to byte[]
    public static byte[] hexToByteArray(String hex) {
        if (hex == null || hex.length() == 0) {
            return null;
        }
        byte[] ba = new byte[hex.length() / 2];
        for (int i = 0; i < ba.length; i++) {
            ba[i] = (byte) Integer.parseInt(hex.substring(2 * i, 2 * i + 2), 16);
        }
        return ba;
    }

    // byte[] to hex sting
    public static String byteArrayToHex(byte[] ba) {
        if (ba == null || ba.length == 0) {
            return null;
        }
        StringBuffer sb = new StringBuffer(ba.length * 2);
        String hexNumber;
        for (int x = 0; x < ba.length; x++) {
            hexNumber = "0" + Integer.toHexString(0xff & ba[x]);

            sb.append(hexNumber.substring(hexNumber.length() - 2));
        }
        return sb.toString();
    }


'프로그래밍 > JAVA' 카테고리의 다른 글

json을 map으로, map을 json으로 변환하는 예제들  (0) 2019.03.07
java 컬렉션 종류  (0) 2019.03.07
RSA암호화 sample  (0) 2019.03.07
날짜관련 util  (0) 2019.03.07
FCM push 보내기  (0) 2019.03.07
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.security.InvalidKeyException;
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.SecureRandom;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.RSAPrivateKeySpec;
import java.security.spec.RSAPublicKeySpec;
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;

public class MoonRSAManagerExample {

    public static void main(String[] args) {

        System.out.println("Server Start-----------------------------------");
        // 서버측 키 파일 생성 하기
        PublicKey publicKey1 = null;
        PrivateKey privateKey1 = null;

        SecureRandom secureRandom = new SecureRandom();

        KeyPairGenerator keyPairGenerator;

        try {

            keyPairGenerator = KeyPairGenerator.getInstance("RSA");
            keyPairGenerator.initialize(512, secureRandom);

            KeyPair keyPair = keyPairGenerator.genKeyPair();
            publicKey1 = keyPair.getPublic();
            privateKey1 = keyPair.getPrivate();

            KeyFactory keyFactory1 = KeyFactory.getInstance("RSA");

            RSAPublicKeySpec rsaPublicKeySpec = keyFactory1.getKeySpec(publicKey1, RSAPublicKeySpec.class);
            RSAPrivateKeySpec rsaPrivateKeySpec = keyFactory1.getKeySpec(privateKey1, RSAPrivateKeySpec.class);

            System.out.println("Public  key modulus : "+ rsaPublicKeySpec.getModulus());
            System.out.println("Public  key exponent: "+ rsaPublicKeySpec.getPublicExponent());
            System.out.println("Private key modulus : "+ rsaPrivateKeySpec.getModulus());
            System.out.println("Private key exponent: "+ rsaPrivateKeySpec.getPrivateExponent());

        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (InvalidKeySpecException e) {
            e.printStackTrace();
        }

        byte[] bPublicKey1 = publicKey1.getEncoded();
        String sPublicKey1 = Base64.encodeBase64String(bPublicKey1);

        byte[] bPrivateKey1 = privateKey1.getEncoded();
        String sPrivateKey1 = Base64.encodeBase64String(bPrivateKey1);

        try {

            BufferedWriter bw1 = new BufferedWriter(new FileWriter("PublicKey.txt"));
            bw1.write(sPublicKey1);
            bw1.newLine();
            bw1.close();

            BufferedWriter bw2 = new BufferedWriter(new FileWriter("PrivateKey.txt"));
            bw2.write(sPrivateKey1);
            bw2.newLine();
            bw2.close();

        } catch (IOException e) {
            e.printStackTrace();
        }

        // 클라이언트측 키 파일 로딩
        System.out.println("Client Start-----------------------------------");
        String sPublicKey2 = null;
        String sPrivateKey2 = null;

        BufferedReader brPublicKey = null;
        BufferedReader brPrivateKey = null;
        try {
            brPublicKey = new BufferedReader(new FileReader("PublicKey.txt"));
            sPublicKey2 = brPublicKey.readLine(); // First Line Read
            brPrivateKey = new BufferedReader(new FileReader("PrivateKey.txt"));
            sPrivateKey2 = brPrivateKey.readLine(); // First Line Read
            System.out.println("Pubilc Key & Private Key Read");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (brPublicKey != null)
                    brPublicKey.close();
                if (brPrivateKey != null)
                    brPrivateKey.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        byte[] bPublicKey2 = Base64.decodeBase64(sPublicKey2.getBytes());
        PublicKey publicKey2 = null;

        byte[] bPrivateKey2 = Base64.decodeBase64(sPrivateKey2.getBytes());
        PrivateKey privateKey2 = null;

        try {
            KeyFactory keyFactory2 = KeyFactory.getInstance("RSA");

            X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(bPublicKey2);
            publicKey2 = keyFactory2.generatePublic(publicKeySpec);

            PKCS8EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(bPrivateKey2);
            privateKey2 = keyFactory2.generatePrivate(privateKeySpec);

        } catch (Exception e) {
            e.printStackTrace();
        }

        String sPlain1 = "Welcome to RSA";
        String sPlain2 = null;

        try {
            Cipher cipher = Cipher.getInstance("RSA");

            // 공개키 이용 암호화
            cipher.init(Cipher.ENCRYPT_MODE, publicKey2);
            byte[] bCipher1 = cipher.doFinal(sPlain1.getBytes());
            String sCipherBase64 = Base64.encodeBase64String(bCipher1);

            // 개인키 이용 복호화
            byte[] bCipher2 = Base64.decodeBase64(sCipherBase64.getBytes());
            cipher.init(Cipher.DECRYPT_MODE, privateKey2);
            byte[] bPlain2 = cipher.doFinal(bCipher2);
            sPlain2 = new String(bPlain2);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (NoSuchPaddingException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (IllegalBlockSizeException e) {
            e.printStackTrace();
        } catch (BadPaddingException e) {
            e.printStackTrace();
        }

        System.out.println("sPlain1 : " + sPlain1); // 평문(원본)
        System.out.println("sPlain2 : " + sPlain2); // 평문(암호화후 복호화된 평문)
    }


'프로그래밍 > JAVA' 카테고리의 다른 글

java 컬렉션 종류  (0) 2019.03.07
RSA암호화 key생성 암호화 복호화  (0) 2019.03.07
날짜관련 util  (0) 2019.03.07
FCM push 보내기  (0) 2019.03.07
Map을 VO로 변환  (0) 2019.03.07

+ Recent posts