Skip to content

Commit

Permalink
Merge pull request #64 from zeoflow/bug/unsafe-encryption
Browse files Browse the repository at this point in the history
Updated algorithm for encryption
  • Loading branch information
teogor authored Oct 27, 2022
2 parents 3600a81 + fb69ad2 commit 75ce54f
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 13 deletions.
17 changes: 17 additions & 0 deletions .idea/deploymentTargetDropDown.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ buildscript {
}

plugins {
id 'com.android.application' version '7.4.0-alpha07' apply false
id 'com.android.library' version '7.4.0-alpha07' apply false
id 'com.android.application' version '7.3.0' apply false
id 'com.android.library' version '7.3.0' apply false
id 'org.jetbrains.kotlin.android' version '1.7.0' apply false
id 'org.jetbrains.kotlin.jvm' version '1.7.0' apply false
}
Binary file modified buildSrc/build/libs/buildSrc.jar
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1 +1 @@
implementation-classpath=G\:/Business/ZeoFlow/OpenSource/memo/buildSrc/build/classes/java/main;G\:/Business/ZeoFlow/OpenSource/memo/buildSrc/build/classes/groovy/main;G\:/Business/ZeoFlow/OpenSource/memo/buildSrc/build/classes/kotlin/main;G\:/Business/ZeoFlow/OpenSource/memo/buildSrc/build/resources/main
implementation-classpath=C\:/Users/me/Business/ZeoFlow/memo/buildSrc/build/classes/java/main;C\:/Users/me/Business/ZeoFlow/memo/buildSrc/build/classes/groovy/main;C\:/Users/me/Business/ZeoFlow/memo/buildSrc/build/classes/kotlin/main;C\:/Users/me/Business/ZeoFlow/memo/buildSrc/build/resources/main
16 changes: 7 additions & 9 deletions memo/src/main/java/com/zeoflow/memo/ConcealEncryption.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,11 @@ import java.util.*
import javax.crypto.*
import javax.crypto.spec.SecretKeySpec

open class ConcealEncryption protected constructor(
open class ConcealEncryption constructor(
private val encryptionKey: String,
init: Boolean
) : Encryption {
private val secretKey: SecretKey?

constructor(encryptionKey: String) : this(encryptionKey, true)
private val secretKey: SecretKey?

init {
val keyLength = 128
Expand All @@ -24,7 +22,7 @@ open class ConcealEncryption protected constructor(
val passwordBytes = encryptionKey.toByteArray(StandardCharsets.UTF_8)
val length = passwordBytes.size.coerceAtMost(keyBytes.size)
System.arraycopy(passwordBytes, 0, keyBytes, 0, length)
secretKey = SecretKeySpec(keyBytes, "AES")
secretKey = SecretKeySpec(keyBytes, "AES/GCM/NoPadding")
}

override fun encryptionKey(): String {
Expand All @@ -37,11 +35,11 @@ open class ConcealEncryption protected constructor(

@SuppressLint("GetInstance")
@Throws(Exception::class)
override fun encrypt(key: String?, plainText: String): String? {
override fun encrypt(key: String?, value: String): String? {
try {
val cipher = Cipher.getInstance("AES/ECB/PKCS5Padding")
cipher.init(Cipher.ENCRYPT_MODE, secretKey)
val cipherText = cipher.doFinal(plainText.toByteArray(StandardCharsets.UTF_8))
val cipherText = cipher.doFinal(value.toByteArray(StandardCharsets.UTF_8))
return Base64.encodeToString(cipherText, Base64.NO_WRAP)
} catch (e: NoSuchAlgorithmException) {
e.printStackTrace()
Expand All @@ -59,11 +57,11 @@ open class ConcealEncryption protected constructor(

@SuppressLint("GetInstance")
@Throws(Exception::class)
override fun decrypt(key: String?, cipherText: String?): String {
override fun decrypt(key: String?, value: String?): String {
val cipher = Cipher.getInstance("AES/ECB/PKCS5Padding")
cipher.init(Cipher.DECRYPT_MODE, secretKey)
return String(
cipher.doFinal(Base64.decode(cipherText, Base64.NO_WRAP)),
cipher.doFinal(Base64.decode(value, Base64.NO_WRAP)),
StandardCharsets.UTF_8
)
}
Expand Down
2 changes: 1 addition & 1 deletion memo/src/main/java/com/zeoflow/memo/MemoBuilder.kt
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class MemoBuilder @JvmOverloads constructor(context: Context = ApplicationInitia
get() {
return cryptoStorage
}
var encryption: Encryption = ConcealEncryption("c34f5345")
var encryption: Encryption = ConcealEncryption("default")
get() {
if (field is ConcealEncryption) {
if (!(field as ConcealEncryption).init()) {
Expand Down

0 comments on commit 75ce54f

Please sign in to comment.