Skip to content

Commit

Permalink
Merge pull request #37 from phash/ChangeKeyEncryption
Browse files Browse the repository at this point in the history
Change key encryption
  • Loading branch information
phash authored Sep 26, 2018
2 parents 815909b + 2a2e9df commit ba1df62
Show file tree
Hide file tree
Showing 23 changed files with 357 additions and 255 deletions.
Binary file modified .idea/caches/build_file_checksums.ser
Binary file not shown.
8 changes: 4 additions & 4 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ android {
applicationId "de.phash.manuel.asw"
minSdkVersion 23
targetSdkVersion 28
versionCode 15
versionName "0.4.15 beta"
versionCode 16
versionName "0.5.16 beta"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
versionNameSuffix ' hydrogen '
versionNameSuffix ' helium '
}
buildTypes {
release {
Expand Down Expand Up @@ -106,7 +106,7 @@ dependencies {

// https://mvnrepository.com/artifact/com.github.ben-manes.caffeine/caffeine
implementation 'com.github.ben-manes.caffeine:caffeine:2.6.2'

implementation 'com.github.simbiose:Encryption:2.0.1'
// https://mvnrepository.com/artifact/com.muquit.libsodiumjna/libsodium-jna

// https://mvnrepository.com/artifact/com.parse/parse-android
Expand Down
68 changes: 53 additions & 15 deletions app/src/main/java/de/phash/manuel/asw/CreateAccountActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,24 @@

package de.phash.manuel.asw

import android.content.ContentValues
import android.content.DialogInterface
import android.os.Bundle
import android.support.v7.app.AlertDialog
import android.support.v7.app.AppCompatActivity
import android.util.Log
import android.view.LayoutInflater
import android.view.Menu
import android.view.MenuItem
import android.view.View
import android.widget.Toast
import de.phash.manuel.asw.database.MyDatabaseOpenHelper
import de.phash.manuel.asw.database.database
import de.phash.manuel.asw.semux.key.Key
import de.phash.manuel.asw.util.EnCryptor
import de.phash.manuel.asw.util.createAccount
import de.phash.manuel.asw.util.isPasswordCorrect
import de.phash.manuel.asw.util.isPasswordSet
import kotlinx.android.synthetic.main.activity_create_account.*
import kotlinx.android.synthetic.main.password_prompt.view.*
import org.bouncycastle.util.encoders.Hex
import org.jetbrains.anko.alert
import org.jetbrains.anko.noButton
Expand All @@ -50,7 +56,14 @@ class CreateAccountActivity : AppCompatActivity() {
setContentView(R.layout.activity_create_account)
setSupportActionBar(findViewById(R.id.my_toolbar))
updateViews()
save()
}

fun saveClick(view: View) {
if (isPasswordSet(this)) {
passwordSecured()
} else {
save("default")
}
}

fun updateViews() {
Expand Down Expand Up @@ -81,26 +94,51 @@ class CreateAccountActivity : AppCompatActivity() {
}.show()
}

fun save() {

val encryptorp = EnCryptor()
val encryptors = EnCryptor()
val encryptedPrivK = encryptors.encryptText(key.toAddressString() + "s", de.phash.manuel.asw.semux.key.Hex.encode0x(key.privateKey))
val encryptedPublK = encryptorp.encryptText(key.toAddressString() + "p", de.phash.manuel.asw.semux.key.Hex.encode0x(key.publicKey))

val values = ContentValues()
values.put("address", key.toAddressString())
values.put("publickey", Hex.toHexString(encryptedPublK))
values.put("privatekey", Hex.toHexString(encryptedPrivK))
values.put("ivs", Hex.toHexString(encryptors.iv))
values.put("ivp", Hex.toHexString(encryptorp.iv))
fun save(password: String) {
val semuxAddress = createAccount(key, password)
val values = semuxAddress.toContentValues()

database.use { insert(MyDatabaseOpenHelper.SEMUXADDRESS_TABLENAME, null, values) }
Toast.makeText(this, "new account created! Save your private key!", Toast.LENGTH_LONG)
//balanceActivity(this)

}

fun passwordSecured() {
val dialogBuilder = AlertDialog.Builder(this)
val inflater = LayoutInflater.from(this)
val promptView = inflater.inflate(R.layout.password_prompt, null)
dialogBuilder.setView(promptView)

dialogBuilder.setCancelable(true).setOnCancelListener(DialogInterface.OnCancelListener { dialog ->
dialog.dismiss()
settingsActivity(this)
})
.setPositiveButton("SEND") { dialog, which ->
Log.i("PASSWORD", "positive button")
if (promptView.enterOldPassword.text.toString().isEmpty()) {
Toast.makeText(this, "Input does not match your current password", Toast.LENGTH_LONG).show()
} else {
if (isPasswordCorrect(this, promptView.enterOldPassword.text.toString())) {
save(promptView.enterOldPassword.text.toString())

} else {
Log.i("PASSWORD", "PW false")
Toast.makeText(this, "Input does not match your current password", Toast.LENGTH_LONG).show()
settingsActivity(this)
}
}
}
.setNegativeButton("CANCEL") { dialog, which ->
Log.i("PASSWORD", "negative button")
dialog.dismiss()
settingsActivity(this)
}
val dialog: AlertDialog = dialogBuilder.create()
dialog.show()
}

override fun onCreateOptionsMenu(menu: Menu): Boolean {
val inflater = menuInflater
inflater.inflate(R.menu.menu, menu)
Expand Down
13 changes: 8 additions & 5 deletions app/src/main/java/de/phash/manuel/asw/DashBoardActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import de.phash.manuel.asw.semux.APIService.Companion.SEMUXFORMAT
import de.phash.manuel.asw.semux.json.CheckBalance
import de.phash.manuel.asw.util.checkBalanceForWallet
import de.phash.manuel.asw.util.firebase
import de.phash.manuel.asw.util.isPasswordSet
import kotlinx.android.synthetic.main.activity_dash_board.*
import org.jetbrains.anko.alert
import java.math.BigDecimal
Expand All @@ -65,6 +66,7 @@ class DashBoardActivity : AppCompatActivity() {
firebase("1", type = "dashboard", mFirebaseAnalytics = FirebaseAnalytics.getInstance(this))
versionView.text = this.packageManager.getPackageInfo(this.packageName, 0).versionName

setPWButton.visibility = if (isPasswordSet(this)) INVISIBLE else VISIBLE
}

override fun onRestart() {
Expand All @@ -86,18 +88,18 @@ class DashBoardActivity : AppCompatActivity() {
}

fun onCreateClick(view: View) {
val intent = Intent(this, CreateAccountActivity::class.java)
startActivity(intent)
createActivity(this)
}

fun onImportClick(view: View) {
val intent = Intent(this, ImportKeyActivity::class.java)
startActivity(intent)
importActivity(this)
}

fun onSetPWClick(view: View) {
setPasswordActivity(this)
}

fun onBalancesClick(view: View) {

val intent = Intent(this, BalancesActivity::class.java)
startActivity(intent)
}
Expand Down Expand Up @@ -152,6 +154,7 @@ class DashBoardActivity : AppCompatActivity() {
viewAccountsButton.visibility = VISIBLE
dashLockedtextView.visibility = VISIBLE
dashTotaltextView.visibility = VISIBLE
setPWButton.visibility = INVISIBLE
}

override fun onResume() {
Expand Down
76 changes: 56 additions & 20 deletions app/src/main/java/de/phash/manuel/asw/ImportKeyActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,12 @@

package de.phash.manuel.asw

import android.content.ContentValues
import android.content.DialogInterface
import android.os.Bundle
import android.support.v7.app.AlertDialog
import android.support.v7.app.AppCompatActivity
import android.util.Log
import android.view.LayoutInflater
import android.view.Menu
import android.view.MenuItem
import android.view.View
Expand All @@ -36,9 +39,11 @@ import de.phash.manuel.asw.database.database
import de.phash.manuel.asw.semux.key.CryptoException
import de.phash.manuel.asw.semux.key.Hex
import de.phash.manuel.asw.semux.key.Key
import de.phash.manuel.asw.util.EnCryptor
import de.phash.manuel.asw.util.createAccount
import de.phash.manuel.asw.util.isPasswordCorrect
import de.phash.manuel.asw.util.isPasswordSet
import kotlinx.android.synthetic.main.activity_import_key.*
import org.jetbrains.anko.design.snackbar
import kotlinx.android.synthetic.main.password_prompt.view.*

class ImportKeyActivity : AppCompatActivity() {

Expand All @@ -49,36 +54,67 @@ class ImportKeyActivity : AppCompatActivity() {
}

fun importClick(view: View) {
if (isPasswordSet(this)) {
passwordSecured()
} else {
import("default")
}
}

fun passwordSecured() {
val dialogBuilder = AlertDialog.Builder(this)
val inflater = LayoutInflater.from(this)
val promptView = inflater.inflate(R.layout.password_prompt, null)
dialogBuilder.setView(promptView)

dialogBuilder.setCancelable(true).setOnCancelListener(DialogInterface.OnCancelListener { dialog ->
dialog.dismiss()
settingsActivity(this)
})
.setPositiveButton("SEND") { dialog, which ->
Log.i("PASSWORD", "positive button")
if (promptView.enterOldPassword.text.toString().isEmpty()) {
Toast.makeText(this, "Input does not match your current password", Toast.LENGTH_LONG).show()
} else {
if (isPasswordCorrect(this, promptView.enterOldPassword.text.toString())) {
import(promptView.enterOldPassword.text.toString())

} else {
Log.i("PASSWORD", "PW false")
Toast.makeText(this, "Input does not match your current password", Toast.LENGTH_LONG).show()
settingsActivity(this)
}
}
}
.setNegativeButton("CANCEL") { dialog, which ->
Log.i("PASSWORD", "negative button")
dialog.dismiss()
settingsActivity(this)
}
val dialog: AlertDialog = dialogBuilder.create()
dialog.show()
}

fun import(password: String) {
val pkey = importEditText.text.toString()
if (pkey.isEmpty())
Toast.makeText(this, "Key may not be empty", Toast.LENGTH_LONG).show()
else {


try {
val key = Key(Hex.decode0x(pkey))

val encryptorp = EnCryptor()
val encryptors = EnCryptor()
val encryptedPrivK = encryptors.encryptText(key.toAddressString() + "s", de.phash.manuel.asw.semux.key.Hex.encode0x(key.privateKey))
val encryptedPublK = encryptorp.encryptText(key.toAddressString() + "p", de.phash.manuel.asw.semux.key.Hex.encode0x(key.publicKey))


importAddress.text = key.toAddressString()
importPubKey.text = Hex.encode0x(key.publicKey)

val values = ContentValues()
values.put("address", key.toAddressString())
values.put("publickey", org.bouncycastle.util.encoders.Hex.toHexString(encryptedPublK))
values.put("privatekey", org.bouncycastle.util.encoders.Hex.toHexString(encryptedPrivK))
values.put("ivs", org.bouncycastle.util.encoders.Hex.toHexString(encryptors.iv))
values.put("ivp", org.bouncycastle.util.encoders.Hex.toHexString(encryptorp.iv))

val semuxAddress = createAccount(key, password)
val values = semuxAddress.toContentValues()
database.use { insert(MyDatabaseOpenHelper.SEMUXADDRESS_TABLENAME, null, values) }
Toast.makeText(this, "key added", Toast.LENGTH_LONG).show()
dashboardActivity(this)

} catch (e: CryptoException) {
view.snackbar(e.localizedMessage)

Toast.makeText(this, "not a valid private key", Toast.LENGTH_LONG)
Toast.makeText(this, "not a valid private key", Toast.LENGTH_LONG).show()
}

}
Expand Down
7 changes: 2 additions & 5 deletions app/src/main/java/de/phash/manuel/asw/ManageActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,7 @@ import de.phash.manuel.asw.database.database
import de.phash.manuel.asw.semux.APIService
import de.phash.manuel.asw.semux.ManageAccounts
import de.phash.manuel.asw.semux.json.CheckBalance
import de.phash.manuel.asw.util.checkBalanceForWallet
import de.phash.manuel.asw.util.getSemuxAddress
import de.phash.manuel.asw.util.isPasswordCorrect
import de.phash.manuel.asw.util.isPasswordSet
import de.phash.manuel.asw.util.*
import kotlinx.android.synthetic.main.password_prompt.view.*

class ManageActivity : AppCompatActivity() {
Expand All @@ -57,7 +54,7 @@ class ManageActivity : AppCompatActivity() {
setContentView(R.layout.activity_manage)
setSupportActionBar(findViewById(R.id.my_toolbar))
viewManager = LinearLayoutManager(this)
viewAdapter = ManageAdapter(accountList, this, database)
viewAdapter = ManageAdapter(accountList, this, DEFAULT_PW, database)
recyclerView = findViewById<RecyclerView>(R.id.manageRecycler).apply {
setHasFixedSize(true)
layoutManager = viewManager
Expand Down
12 changes: 6 additions & 6 deletions app/src/main/java/de/phash/manuel/asw/ManageAdapter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,12 @@ import android.widget.Toast
import de.phash.manuel.asw.database.MyDatabaseOpenHelper
import de.phash.manuel.asw.semux.APIService
import de.phash.manuel.asw.semux.ManageAccounts
import de.phash.manuel.asw.semux.key.Hex
import de.phash.manuel.asw.util.*
import kotlinx.android.synthetic.main.password_prompt.view.*
import java.math.BigDecimal
import java.text.DecimalFormat

class ManageAdapter(private val myDataset: ArrayList<ManageAccounts>, private val context: Context, private val database: MyDatabaseOpenHelper) :
class ManageAdapter(private val myDataset: ArrayList<ManageAccounts>, private val context: Context, private val password: String, private val database: MyDatabaseOpenHelper) :
RecyclerView.Adapter<ManageAdapter.MyViewHolder>() {

class MyViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
Expand Down Expand Up @@ -90,16 +89,17 @@ class ManageAdapter(private val myDataset: ArrayList<ManageAccounts>, private va
})


val decryptedKey = DeCryptor().decryptData(account.account.address + "s", Hex.decode0x(account.account.privateKey), Hex.decode0x(account.account.ivs))
holder.pkey?.text = decryptedKey
val decryptedAcc = decryptAccount(account.account, password)

holder.pkey?.text = decryptedAcc.privateKey
holder.removeButton.setOnClickListener(View.OnClickListener {
removeClick(account)
})
holder.pkey.setOnClickListener(View.OnClickListener {
copyItem(decryptedKey, "PRIVATE KEY")
copyItem(decryptedAcc.privateKey ?: "", "PRIVATE KEY")
})
holder.copyPrivKeyBtn.setOnClickListener(View.OnClickListener {
copyItem(decryptedKey, "PRIVATE KEY")
copyItem(decryptedAcc.privateKey ?: "", "PRIVATE KEY")
})


Expand Down
3 changes: 3 additions & 0 deletions app/src/main/java/de/phash/manuel/asw/PasswordActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,11 @@ import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.widget.Toast
import de.phash.manuel.asw.database.database
import de.phash.manuel.asw.util.isPasswordCorrect
import de.phash.manuel.asw.util.isPasswordSet
import de.phash.manuel.asw.util.persistNewPassword
import de.phash.manuel.asw.util.updateAllAddresses
import kotlinx.android.synthetic.main.activity_passwords.*
import kotlinx.android.synthetic.main.password_prompt.view.*

Expand Down Expand Up @@ -75,6 +77,7 @@ class PasswordActivity : AppCompatActivity() {
Toast.makeText(this, "Input does not match your current password", Toast.LENGTH_LONG).show()
} else {
if (isPasswordCorrect(this, promptView.enterOldPassword.text.toString())) {
updateAllAddresses(database, enterNewPassword.text.toString())
persistNewPassword(this, enterNewPassword.text.toString())
Toast.makeText(this, "New password set", Toast.LENGTH_LONG).show()
settingsActivity(this)
Expand Down
Loading

0 comments on commit ba1df62

Please sign in to comment.