투케이2K

503. (kotlin/코틀린) [유틸 파일] getAES192DecodeString : Aes192 디코딩 수행 : key , iv 사용 본문

Kotlin

503. (kotlin/코틀린) [유틸 파일] getAES192DecodeString : Aes192 디코딩 수행 : key , iv 사용

투케이2K 2024. 5. 5. 23:39
반응형

[개발 환경 설정]

개발 툴 : AndroidStudio

개발 언어 : Kotlin

 

[소스 코드]

        // ----------------------------------------------------------------------------------
        // TODO [SEARCH FAST] : [getAES192DecodeString] : Aes192 디코딩 : key + iv 사용
        // ----------------------------------------------------------------------------------
        fun getAES192DecodeString(aes128SecretKey: String, aes128Iv: String, data: String) : String {

            /**
             * // -----------------------------------------
             * [getAES192DecodeString 메소드 설명]
             * // -----------------------------------------
             * 1. Aes192 암호화 방식을 사용해 데이터 디코딩 수행 실시
             * // -----------------------------------------
             * 2. 호출 방법 : [aes 비밀 키 / aes iv 바이트 [16 바이트 고정] / 디코딩할 데이터]
             *
             * C_Encryption.getAES192DecodeString("0123456789abcdef", "", "96Mzp/yW0lz/FjdWJkjljA==") // hello
             *
             * C_Encryption.getAES192DecodeString("0123456789abcdef", "0123456789abcdef", "IYW6eSwJzjmDBeZHnWyjgg==") // hello
             *
             * // -----------------------------------------
             * 3. 리턴 데이터 : aes 암호화 디코딩 된 데이터 반환
             * // -----------------------------------------
             * 4. AES Secret Key 참고 : [aes128 = 16 byte / aes192 = 24 byte / aes256 = 32 byte]
             * // -----------------------------------------
             * */


            // [리턴 변수 선언]
            var returnData = ""


            // [로직 처리 실시]
            try {

                // [필수 데이터 널 여부 체크]
                if (C_Util.stringNotNull(aes128SecretKey) == true
                    && aes128SecretKey.length == 24
                    && C_Util.stringNotNull(data) == true) {

                    // ----------------------------
                    // [캐릭터 셋 선언]
                    val charSet = Charsets.UTF_8
                    // ----------------------------
                    // [key >> byte 변환]
                    val keyByte = aes128SecretKey.toByteArray(charSet)
                    // ----------------------------
                    // [iv 값 널 체크 실시]
                    var ivByte : ByteArray? = null
                    if (C_Util.stringNotNull(aes128Iv) == true && aes128Iv.length == 16){
                        ivByte = aes128Iv.toByteArray(charSet)
                    }
                    else { // [널인 경우]
                        ivByte = ByteArray(16)
                    }
                    // ----------------------------
                    // [data >> byte 변환 (base64 디코딩)]
                    val dataByte = Base64.decode(data, Base64.DEFAULT)
                    // ----------------------------
                    // [AES 128 디코딩 수행]
                    val ivSpec: AlgorithmParameterSpec = IvParameterSpec(ivByte) // [알고리즘 스펙]

                    val newKey = SecretKeySpec(keyByte, "AES") // [암호화 알고리즘]

                    var cipher: Cipher? = null
                    cipher = Cipher.getInstance("AES/CBC/PKCS5Padding") // [패딩]

                    cipher.init(Cipher.DECRYPT_MODE, newKey, ivSpec) // [key 지정해 암호화 지정]
                    // ----------------------------
                    // [리턴 데이터 반환 실시]
                    returnData = cipher.doFinal(dataByte).toString(charSet)
                    // ----------------------------

                }
                else {
                    // ===============================================================
                    S_Log._E_("aes192 비밀키 사용해 디코딩 수행 실시", arrayOf(
                        "ERROR :: aes192SecretKey 비밀키 , data 데이터 null 널 임"
                    ))
                    // ===============================================================
                }

            }
            catch (e: Exception) {
                S_Log._printStackTrace_(null, S_FinalMsg.LOG_BUG_STATE, null, e)
            }


            // [로그 출력 실시]
            //*
            // ===============================================================
            S_Log._D_("aes192 비밀키 사용해 디코딩 수행 실시", arrayOf(
                "INPUT [KEY] :: " + aes128SecretKey.toString(),
                "INPUT [IV] :: " + aes128Iv.toString(),
                "INPUT [DATA] :: $data",
                "RETURN :: $returnData"
            ))
            // ===============================================================
            // */


            // [리턴 반환 실시]
            return returnData

        }
 

[결과 출력]


반응형
Comments