AES256 암호화 / 복호화 > 기술자료 | 해피정닷컴

AES256 암호화 / 복호화 > 기술자료

본문 바로가기

사이트 내 전체검색

AES256 암호화 / 복호화 > 기술자료

Java AES256 암호화 / 복호화

페이지 정보


본문

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.spec.AlgorithmParameterSpec;
import java.util.Base64;
 
public class AES256Cipher {
    private static String key = "testggisuitken1959imy91jb7076jas"; // AES 암호화  키
    //private static String key = "cde0123456789abf"; // 16 바이트키
    public static byte[] ivBytes = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
 
    public static String AES_Encode(String str, String key)    throws UnsupportedEncodingException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException {
 
        byte[] textBytes = str.getBytes("UTF-8");
        AlgorithmParameterSpec ivSpec = new IvParameterSpec(ivBytes);
        SecretKeySpec newKey = new SecretKeySpec(key.getBytes("UTF-8"), "AES");
        Cipher cipher = null;
        cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, newKey, ivSpec);
 
        Base64.Encoder encoder = Base64.getEncoder();
        return encoder.encodeToString(cipher.doFinal(textBytes));
    }
 
    public static String AES_Decode(String str, String key)    throws UnsupportedEncodingException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException {
 
        Base64.Decoder decoder = Base64.getDecoder();
        byte[] textBytes =decoder.decode(str);
        AlgorithmParameterSpec ivSpec = new IvParameterSpec(ivBytes);
        SecretKeySpec newKey = new SecretKeySpec(key.getBytes("UTF-8"), "AES");
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        cipher.init(Cipher.DECRYPT_MODE, newKey, ivSpec);
        return new String(cipher.doFinal(textBytes), "UTF-8");
    }
 
    // 암호화
    public static String encrypt(String data) {
        try {
            data = AES_Encode(data, AES256Cipher.key);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (NoSuchPaddingException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (InvalidAlgorithmParameterException e) {
            e.printStackTrace();
        } catch (IllegalBlockSizeException e) {
            e.printStackTrace();
        } catch (BadPaddingException e) {
            e.printStackTrace();
        }
        return data;
    }
 
    // 복호화
    public static String decrypt(String data) {
        try {
            data = AES_Decode(data, AES256Cipher.key);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (NoSuchPaddingException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (InvalidAlgorithmParameterException e) {
            e.printStackTrace();
        } catch (IllegalBlockSizeException e) {
            e.printStackTrace();
        } catch (BadPaddingException e) {
            e.printStackTrace();
        }
        return data;
    }
}


암호화 : t7VJP1+3mcZlcJsh9nChPSUYOZSw+QIl2UFJlvkis8Y=
복호화 : 암호화 예제


참고자료
https://link2me.tistory.com/2013

댓글목록

등록된 댓글이 없습니다.


Total 195건 1 페이지
  • RSS
기술자료 목록
195
그누보드   886  2024-11-22 10:52 ~ 2024-11-22 11:03  
194
호스팅   865  2024-11-19 14:41 ~ 2024-11-19 21:17  
193
JavaScript   1854  2024-02-16 18:50 ~ 2024-02-16 20:37  
열람
Java   1850  2024-02-06 16:49  
191
PHP   2076  2024-02-06 16:42  
190
그누보드   2768  2023-11-06 20:38  
189
SNS   3025  2023-08-30 17:10  
188
Editor   4511  2023-03-31 09:54 ~ 2023-03-31 12:51  
187
호스팅   3738  2023-03-06 15:52 ~ 2023-12-18 16:54  
186
호스팅   4684  2023-02-17 17:17  
185
호스팅   4611  2022-12-16 12:22 ~ 2022-12-26 19:57  
184
PHP   4132  2022-11-16 18:17  
183
그누보드   4092  2022-11-10 20:41 ~ 2022-11-14 01:03  
182
Adobe   7254  2022-03-21 15:11 ~ 2022-11-11 16:08  
181
일반   5722  2021-12-15 11:20  
180
MySQL   7701  2021-11-24 00:17 ~ 2021-11-24 00:22  
179
일반   5270  2021-11-06 01:29 ~ 2021-11-06 09:23  
178
Editor   6276  2021-10-26 18:16 ~ 2021-11-09 14:15  
177
그누보드   5369  2021-10-08 17:20 ~ 2021-10-21 15:24  
176
그누보드   6743  2021-10-08 07:41 ~ 2021-10-14 10:40  

검색

해피정닷컴 정보

회사소개 회사연혁 협력사 오시는길 서비스 이용약관 개인정보 처리방침

회사명: 해피정닷컴   대표: 정창용   전화: 070-7600-3500   팩스: 042-670-8272
주소: (34368) 대전시 대덕구 대화로 160 대전산업용재유통단지 1동 222호
개인정보보호책임자: 정창용   사업자번호: 119-05-36414
통신판매업신고: 제2024-대전대덕-0405호 [사업자등록확인]  
Copyright 2001~2025 해피정닷컴. All Rights Reserved.