[AES 암,복호화]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | 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 암,복호화]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | 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)); } } |
[출처]
[Java] 암호화를 위한 Cipher 클래스 (Java로 AES, RSA 암호화, 복호화)
Java에서 암호화, 복호화를 하려면 javax.crypto.Cipher 클래스를 사용한다. AES와 DES, RSA 등...
blog.naver.com
'Development > JAVA' 카테고리의 다른 글
Spring Boot JPA - Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set 오류발생시 (0) | 2022.10.28 |
---|---|
jar 실행 (0) | 2022.05.25 |
[컴파일] 자바 classpath 컴파일 및 실행 (0) | 2021.05.19 |
Tomcat hotswap [Spring-Loaded] (0) | 2021.05.03 |
Tomcat hotswap [JRebel] (0) | 2021.05.03 |