티스토리 뷰
암호화 (AES256, BLOWFISH)
복호화
Hash 함수를 통과 하기전의 원본 데이터를 메시지(message)라고 부르고, 통과된 이후의 데이터를 다이제스트(digest)라고 부른다.
1. AES/ECB/PKCS5PADDING 방식
키
plaintext
cipher
ciphertext
2. AES/CBC/PKCS5PADDING 방식
키
iv
plaintext
cipher
ciphertext
3. key값의 길이에 따라 AES128, AES192, AES256 으로 구분됨
AES128 : 키값 16bytes
AES192 : 키값 24bytes
AES256 : 키값 32bytes
MainActivity.kt
package com.example.encryptionexample
import android.os.Bundle
import android.util.Base64
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import javax.crypto.Cipher
import javax.crypto.spec.IvParameterSpec
import javax.crypto.spec.SecretKeySpec
class MainActivity : AppCompatActivity() {
companion object {
const val SECRET_KEY = "ABCDEFGH12345678"
const val SECRET_IV = "1234567812345678"
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val encrypted = "안녕하세요!!!".encryptECB()
val decrypted = encrypted.decryptECB()
Log.e("", encrypted)
Log.e("", decrypted)
val encryptedCBC = "Hello~!!!".encryptCBC()
val decryptedCBC = encryptedCBC.decryptCBC()
Log.e("", encryptedCBC)
Log.e("", decryptedCBC)
}
/**
* ECB 암호화
*/
private fun String.encryptECB(): String{
val keySpec = SecretKeySpec(SECRET_KEY.toByteArray(), "AES") /// 키
val cipher = Cipher.getInstance("AES/ECB/PKCS5PADDING") //싸이퍼
cipher.init(Cipher.ENCRYPT_MODE, keySpec) // 암호화/복호화 모드
val ciphertext = cipher.doFinal(this.toByteArray())
val encodedByte = Base64.encode(ciphertext, Base64.DEFAULT)
return String(encodedByte)
}
/**
* ECB 복호화
*/
private fun String.decryptECB(): String {
val keySpec = SecretKeySpec(SECRET_KEY.toByteArray(), "AES")
var decodedByte: ByteArray = Base64.decode(this, Base64.DEFAULT)
val cipher = Cipher.getInstance("AES/ECB/PKCS5PADDING")
cipher.init(Cipher.DECRYPT_MODE, keySpec)
val output = cipher.doFinal(decodedByte)
return String(output)
}
/**
* CBC 암호화
*/
private fun String.encryptCBC(): String{
val iv = IvParameterSpec(SECRET_IV.toByteArray())
val keySpec = SecretKeySpec(SECRET_KEY.toByteArray(), "AES") /// 키
val cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING") //싸이퍼
cipher.init(Cipher.ENCRYPT_MODE, keySpec, iv) // 암호화/복호화 모드
val crypted = cipher.doFinal(this.toByteArray())
val encodedByte = Base64.encode(crypted, Base64.DEFAULT)
return String(encodedByte)
}
/**
* CBC 복호화
*/
private fun String.decryptCBC(): String {
var decodedByte: ByteArray = Base64.decode(this, Base64.DEFAULT)
val iv = IvParameterSpec(SECRET_IV.toByteArray())
val keySpec = SecretKeySpec(SECRET_KEY.toByteArray(), "AES")
val cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING")
cipher.init(Cipher.DECRYPT_MODE, keySpec, iv)
val output = cipher.doFinal(decodedByte)
return String(output)
}
}
참고.
https://developer.android.com/guide/topics/security/cryptography?hl=ko
Encoding ? Enctyption ?
https://stackoverflow.com/questions/4657416/difference-between-encoding-and-encryption
CBC? ECB?
https://m.blog.naver.com/PostView.naver?isHttpsRedirect=true&blogId=jsky10503&logNo=221258926405
http://happinessoncode.com/2019/04/06/java-cipher-algorithm-mode-padding/
'Android > Kotlin' 카테고리의 다른 글
[Kotlin] Lifecycle, LifecycleOwner, LifecycleObserver (0) | 2021.06.05 |
---|---|
[Kotlin] registerForActivityResult (0) | 2021.05.27 |
[Kotlin] 위치 권한 (0) | 2021.05.27 |
[Kotlin] View (0) | 2021.05.25 |
[Kotlin] dp와 px (0) | 2021.05.25 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- 안드로이드 #코틀린 #Android #Kotlin
- 혀가 길지 않은 개발자
- java
- ArrayList
- Architecture Pattern
- TabLayout
- coroutine
- activity
- DataBinding
- CoordinatorLayout
- XML
- ViewModel
- handler
- MVVM
- Kotlin
- 코틀린
- 자바
- Android
- Design Pattern
- recyclerview
- JSONArray
- ViewPager2
- Intent
- fragment
- Livedata
- 안드로이드
- James Kim
- Vue.js #Vue.js + javascript
- View
- JSONObject
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함