[AES 암,복호화]

package sample;

import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;

public class AESEncryptionSample {
    
    public static void main(String[] args) throws NoSuchAlgorithmException
                , NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException
                , BadPaddingException, UnsupportedEncodingException {
        
        // 암호화에 사용할 키, 디폴트로 128bit(16Byte)
        String encryptionKey = "happyprogrammer!";
        
        // 암호화할 문자열
        String target = "Java 마스터!"; 
        
     
        // AES로 암호화 =================================================
        
         // AES Cipher 객체 생성
        Cipher cipher = Cipher.getInstance("AES");
        
        // 암호화 Chipher 초기화 
        SecretKeySpec secretKeySpec = new SecretKeySpec(encryptionKey.getBytes(),"AES");
        cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);
        
        // 암호화 완료 
        byte[] encryptBytes = cipher.doFinal(target.getBytes("UTF-8"));
        System.out.println(new String(encryptBytes)); // => 똑같은 암호화키로 복호화
        
        
        // AES로 복호화 =================================================
        
        // 복호화 Chipher 초기화, 똑같은 암호화키로 복호화
        cipher.init(cipher.DECRYPT_MODE, secretKeySpec);
        byte[] decryptBytes = cipher.doFinal(encryptBytes);
        System.out.println(new String(decryptBytes, "UTF-8"));
    }
    
}

 

[RSA 암,복호화]

package sample;

import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;



public class RSAEncryptionSample {

	public static void main(String[] args) throws NoSuchAlgorithmException
            , NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException
            , BadPaddingException, UnsupportedEncodingException {
        
        // 암호화할 문자열
        String target = "Java 마스터!"; 
        
     
        // RSA 로 암호화 =================================================
        
        // RSA 비밀키와 공개키를 생성
        KeyPairGenerator keypairgen = KeyPairGenerator.getInstance("RSA");
        KeyPair keyPair = keypairgen.generateKeyPair();
        RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();
        RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();
        
         // Cipher 객체 생성과 비밀키 초기화
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.ENCRYPT_MODE, privateKey);
        
        // 암호화 완료 
        byte[] encryptBytes = cipher.doFinal(target.getBytes());
        System.out.println(new String(encryptBytes)); // => 암호화되어 읽지못함
        
        // RSA로 복호화 =================================================
        
        // 복호화 Chipher 초기화, 비밀키와 쌍인 공개키로 복호화함.
        cipher.init(cipher.DECRYPT_MODE, publicKey);
        byte[] decryptBytes = cipher.doFinal(encryptBytes);
        System.out.println(new String(decryptBytes));
    }

}



 

[출처]

https://blog.naver.com/PostView.nhn?blogId=javaking75&logNo=220552245734&categoryNo=64&parentCategoryNo=0&viewDate=&currentPage=1&postListTopCurrentPage=1&from=search 

 

[Java] 암호화를 위한 Cipher 클래스 (Java로 AES, RSA 암호화, 복호화)

Java에서 암호화, 복호화를 하려면 javax.crypto.Cipher 클래스를 사용한다. AES와 DES, RSA 등...

blog.naver.com

 

+ Recent posts