Android

android)AES256 암호화 동적처리

Wootaeng 2022. 12. 21. 12:22
728x90

서버로 보내는 로그인 정보를 처리한 기존 방식

public class AES256Cipher {
    protected static final String aes_key = "1234567890abcdefghjklmnopqrstovwxyz";
    //protected static final byte[] ivBytes = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};



    protected static byte[] getIvBytes(){
        return new byte[]{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
    }
    
    public static String AES_Encode(String str) throws java.io.UnsupportedEncodingException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException {

        byte[] textBytes = str.getBytes("UTF-8");
        AlgorithmParameterSpec ivSpec = new IvParameterSpec(getIvBytes());
        SecretKeySpec newKey = new SecretKeySpec(aes_key.getBytes("UTF-8"), "AES");
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, newKey, ivSpec);
        return Base64.encodeToString(cipher.doFinal(textBytes), Base64.DEFAULT);

        

    }

}

 

구글에서 안전하지않은 암호화

 

안전하지 않은 암호화 문제 해결하기 - Google 고객센터

도움이 되었나요? 어떻게 하면 개선할 수 있을까요? 예아니요

support.google.com

 관련으로 경고 메세지를 받았다. 

대처 방안으로는  Jetpack Security 를 활용하거나 다른 방안이 있으나 

우선적으로 처리한 방법은 기존 코드는 정적으로 처리가 되어있어서

동적으로 처리를 진행했다.

 

//동적 처리 변경
String result = "";
try {

    byte[] textBytes = str.getBytes("UTF-8");
    AlgorithmParameterSpec ivSpec = new IvParameterSpec(getIvBytes());
    SecretKeySpec newKey = new SecretKeySpec(aes_key.getBytes("UTF-8"), "AES");
    Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
    cipher.init(Cipher.ENCRYPT_MODE, newKey, ivSpec);
    return Base64.encodeToString(cipher.doFinal(textBytes), Base64.DEFAULT);

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

return result;

 

result = "" 로 결과값을 초기화 하고 try catch 구문으로 감싸서 처리를 했다.

추가로 key 값을 위에 담아오면 안되는데 그 부분은 서버쪽과 연계라 좀 더 고민을 해봐야할듯도 싶다. 

외주앱이라 서버쪽은 어찌 해야할지..난감하구나

이게 정답인지는 모르겠으나 동적 처리에 대해 다시 한번 생각하게 된 일이다.

728x90
반응형