본문 바로가기
Android

AES128 암호화

by Birthmark 2020. 12. 18.

wiki.toridge.com/index.php?android-AES128%E6%9A%97%E5%8F%B7%E5%8C%96

고객정보가 SQL <-> 외부메모리간 read/write 될때 암호화가 필요하다.

위 링크를 바탕으로 초안을 작성했다.

고객 정보가 임의의 스트링으로 암호화는 되지만 파일 암호화로 계획이 수정됐다.

아래의 샘플 코드를 바탕으로 수정했다.

 

 

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
53
54
55
56
57
58
 
 
public class EncryptionUtils {
    public static String getKey(Context context){
        SharedPreferences sp = context.getSharedPreferences("PASSWORD", MODE_PRIVATE);
        String key =sp.getString("key""1234567890123456");
        return key;
    }
    public static String getParam(Context context){
        SharedPreferences sp = context.getSharedPreferences("PASSWORD", MODE_PRIVATE);
        String param =sp.getString("param""1234567890123456");
        return param;
    }
    public static void encryption(File file, String value, Context context) throws IOException, InvalidKeyException
            , NoSuchAlgorithmException, NoSuchPaddingException
            , InvalidAlgorithmParameterException {
 
        String key = getKey(context);
        String param = getParam(context);
        SecretKey secretKey = new SecretKeySpec(key.getBytes("UTF-8"), "AES");
        IvParameterSpec ivParameterSpec = new IvParameterSpec(param.getBytes("UTF-8"));
        Cipher c = Cipher.getInstance("AES/CBC/PKCS7Padding");
        c.init(Cipher.ENCRYPT_MODE, secretKey,ivParameterSpec);
        try(FileOutputStream f = new FileOutputStream(file, true);
            BufferedOutputStream b = new BufferedOutputStream(f);
            CipherOutputStream out= new CipherOutputStream(b, c)){
            out.write(value.getBytes());
        }
    }
 
    public static String descryption(File file, Context context) throws IOException, InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException, InvalidAlgorithmParameterException {
        String key = getKey(context);
        String param = getParam(context);
        SecretKey secretKey = new SecretKeySpec(key.getBytes("UTF-8"), "AES");
        IvParameterSpec ivParameterSpec = new IvParameterSpec(param.getBytes("UTF-8"));
        Cipher c = Cipher.getInstance("AES/CBC/PKCS7Padding");
        c.init(Cipher.DECRYPT_MODE,secretKey,ivParameterSpec);
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        byte[] data = new byte[1024];
        try(FileInputStream f = new FileInputStream(file);
            BufferedInputStream b = new BufferedInputStream(f);
            CipherInputStream in= new CipherInputStream(b, c)){
            while(true) {
                int num = in.read(data);
                if ( num == -1 ) break;
                out.write(data, 0, num);
            }
        }
 
        catch(Exception e){
            e.printStackTrace();
            return "keyNG";
        }
        String str = new String(out.toByteArray());
        System.out.println(str);
        return str;
    }
}
cs

댓글