Skip to content

Commit ba1df62

Browse files
authored
Merge pull request #37 from phash/ChangeKeyEncryption
Change key encryption
2 parents 815909b + 2a2e9df commit ba1df62

23 files changed

+357
-255
lines changed

.idea/caches/build_file_checksums.ser

0 Bytes
Binary file not shown.

app/build.gradle

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,10 @@ android {
3636
applicationId "de.phash.manuel.asw"
3737
minSdkVersion 23
3838
targetSdkVersion 28
39-
versionCode 15
40-
versionName "0.4.15 beta"
39+
versionCode 16
40+
versionName "0.5.16 beta"
4141
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
42-
versionNameSuffix ' hydrogen '
42+
versionNameSuffix ' helium '
4343
}
4444
buildTypes {
4545
release {
@@ -106,7 +106,7 @@ dependencies {
106106

107107
// https://mvnrepository.com/artifact/com.github.ben-manes.caffeine/caffeine
108108
implementation 'com.github.ben-manes.caffeine:caffeine:2.6.2'
109-
109+
implementation 'com.github.simbiose:Encryption:2.0.1'
110110
// https://mvnrepository.com/artifact/com.muquit.libsodiumjna/libsodium-jna
111111

112112
// https://mvnrepository.com/artifact/com.parse/parse-android

app/src/main/java/de/phash/manuel/asw/CreateAccountActivity.kt

Lines changed: 53 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,24 @@
2424

2525
package de.phash.manuel.asw
2626

27-
import android.content.ContentValues
27+
import android.content.DialogInterface
2828
import android.os.Bundle
29+
import android.support.v7.app.AlertDialog
2930
import android.support.v7.app.AppCompatActivity
31+
import android.util.Log
32+
import android.view.LayoutInflater
3033
import android.view.Menu
3134
import android.view.MenuItem
3235
import android.view.View
3336
import android.widget.Toast
3437
import de.phash.manuel.asw.database.MyDatabaseOpenHelper
3538
import de.phash.manuel.asw.database.database
3639
import de.phash.manuel.asw.semux.key.Key
37-
import de.phash.manuel.asw.util.EnCryptor
40+
import de.phash.manuel.asw.util.createAccount
41+
import de.phash.manuel.asw.util.isPasswordCorrect
42+
import de.phash.manuel.asw.util.isPasswordSet
3843
import kotlinx.android.synthetic.main.activity_create_account.*
44+
import kotlinx.android.synthetic.main.password_prompt.view.*
3945
import org.bouncycastle.util.encoders.Hex
4046
import org.jetbrains.anko.alert
4147
import org.jetbrains.anko.noButton
@@ -50,7 +56,14 @@ class CreateAccountActivity : AppCompatActivity() {
5056
setContentView(R.layout.activity_create_account)
5157
setSupportActionBar(findViewById(R.id.my_toolbar))
5258
updateViews()
53-
save()
59+
}
60+
61+
fun saveClick(view: View) {
62+
if (isPasswordSet(this)) {
63+
passwordSecured()
64+
} else {
65+
save("default")
66+
}
5467
}
5568

5669
fun updateViews() {
@@ -81,26 +94,51 @@ class CreateAccountActivity : AppCompatActivity() {
8194
}.show()
8295
}
8396

84-
fun save() {
85-
86-
val encryptorp = EnCryptor()
87-
val encryptors = EnCryptor()
88-
val encryptedPrivK = encryptors.encryptText(key.toAddressString() + "s", de.phash.manuel.asw.semux.key.Hex.encode0x(key.privateKey))
89-
val encryptedPublK = encryptorp.encryptText(key.toAddressString() + "p", de.phash.manuel.asw.semux.key.Hex.encode0x(key.publicKey))
9097

91-
val values = ContentValues()
92-
values.put("address", key.toAddressString())
93-
values.put("publickey", Hex.toHexString(encryptedPublK))
94-
values.put("privatekey", Hex.toHexString(encryptedPrivK))
95-
values.put("ivs", Hex.toHexString(encryptors.iv))
96-
values.put("ivp", Hex.toHexString(encryptorp.iv))
98+
fun save(password: String) {
99+
val semuxAddress = createAccount(key, password)
100+
val values = semuxAddress.toContentValues()
97101

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

102106
}
103107

108+
fun passwordSecured() {
109+
val dialogBuilder = AlertDialog.Builder(this)
110+
val inflater = LayoutInflater.from(this)
111+
val promptView = inflater.inflate(R.layout.password_prompt, null)
112+
dialogBuilder.setView(promptView)
113+
114+
dialogBuilder.setCancelable(true).setOnCancelListener(DialogInterface.OnCancelListener { dialog ->
115+
dialog.dismiss()
116+
settingsActivity(this)
117+
})
118+
.setPositiveButton("SEND") { dialog, which ->
119+
Log.i("PASSWORD", "positive button")
120+
if (promptView.enterOldPassword.text.toString().isEmpty()) {
121+
Toast.makeText(this, "Input does not match your current password", Toast.LENGTH_LONG).show()
122+
} else {
123+
if (isPasswordCorrect(this, promptView.enterOldPassword.text.toString())) {
124+
save(promptView.enterOldPassword.text.toString())
125+
126+
} else {
127+
Log.i("PASSWORD", "PW false")
128+
Toast.makeText(this, "Input does not match your current password", Toast.LENGTH_LONG).show()
129+
settingsActivity(this)
130+
}
131+
}
132+
}
133+
.setNegativeButton("CANCEL") { dialog, which ->
134+
Log.i("PASSWORD", "negative button")
135+
dialog.dismiss()
136+
settingsActivity(this)
137+
}
138+
val dialog: AlertDialog = dialogBuilder.create()
139+
dialog.show()
140+
}
141+
104142
override fun onCreateOptionsMenu(menu: Menu): Boolean {
105143
val inflater = menuInflater
106144
inflater.inflate(R.menu.menu, menu)

app/src/main/java/de/phash/manuel/asw/DashBoardActivity.kt

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ import de.phash.manuel.asw.semux.APIService.Companion.SEMUXFORMAT
4646
import de.phash.manuel.asw.semux.json.CheckBalance
4747
import de.phash.manuel.asw.util.checkBalanceForWallet
4848
import de.phash.manuel.asw.util.firebase
49+
import de.phash.manuel.asw.util.isPasswordSet
4950
import kotlinx.android.synthetic.main.activity_dash_board.*
5051
import org.jetbrains.anko.alert
5152
import java.math.BigDecimal
@@ -65,6 +66,7 @@ class DashBoardActivity : AppCompatActivity() {
6566
firebase("1", type = "dashboard", mFirebaseAnalytics = FirebaseAnalytics.getInstance(this))
6667
versionView.text = this.packageManager.getPackageInfo(this.packageName, 0).versionName
6768

69+
setPWButton.visibility = if (isPasswordSet(this)) INVISIBLE else VISIBLE
6870
}
6971

7072
override fun onRestart() {
@@ -86,18 +88,18 @@ class DashBoardActivity : AppCompatActivity() {
8688
}
8789

8890
fun onCreateClick(view: View) {
89-
val intent = Intent(this, CreateAccountActivity::class.java)
90-
startActivity(intent)
91+
createActivity(this)
9192
}
9293

9394
fun onImportClick(view: View) {
94-
val intent = Intent(this, ImportKeyActivity::class.java)
95-
startActivity(intent)
95+
importActivity(this)
96+
}
9697

98+
fun onSetPWClick(view: View) {
99+
setPasswordActivity(this)
97100
}
98101

99102
fun onBalancesClick(view: View) {
100-
101103
val intent = Intent(this, BalancesActivity::class.java)
102104
startActivity(intent)
103105
}
@@ -152,6 +154,7 @@ class DashBoardActivity : AppCompatActivity() {
152154
viewAccountsButton.visibility = VISIBLE
153155
dashLockedtextView.visibility = VISIBLE
154156
dashTotaltextView.visibility = VISIBLE
157+
setPWButton.visibility = INVISIBLE
155158
}
156159

157160
override fun onResume() {

app/src/main/java/de/phash/manuel/asw/ImportKeyActivity.kt

Lines changed: 56 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,12 @@
2424

2525
package de.phash.manuel.asw
2626

27-
import android.content.ContentValues
27+
import android.content.DialogInterface
2828
import android.os.Bundle
29+
import android.support.v7.app.AlertDialog
2930
import android.support.v7.app.AppCompatActivity
31+
import android.util.Log
32+
import android.view.LayoutInflater
3033
import android.view.Menu
3134
import android.view.MenuItem
3235
import android.view.View
@@ -36,9 +39,11 @@ import de.phash.manuel.asw.database.database
3639
import de.phash.manuel.asw.semux.key.CryptoException
3740
import de.phash.manuel.asw.semux.key.Hex
3841
import de.phash.manuel.asw.semux.key.Key
39-
import de.phash.manuel.asw.util.EnCryptor
42+
import de.phash.manuel.asw.util.createAccount
43+
import de.phash.manuel.asw.util.isPasswordCorrect
44+
import de.phash.manuel.asw.util.isPasswordSet
4045
import kotlinx.android.synthetic.main.activity_import_key.*
41-
import org.jetbrains.anko.design.snackbar
46+
import kotlinx.android.synthetic.main.password_prompt.view.*
4247

4348
class ImportKeyActivity : AppCompatActivity() {
4449

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

5156
fun importClick(view: View) {
57+
if (isPasswordSet(this)) {
58+
passwordSecured()
59+
} else {
60+
import("default")
61+
}
62+
}
63+
64+
fun passwordSecured() {
65+
val dialogBuilder = AlertDialog.Builder(this)
66+
val inflater = LayoutInflater.from(this)
67+
val promptView = inflater.inflate(R.layout.password_prompt, null)
68+
dialogBuilder.setView(promptView)
69+
70+
dialogBuilder.setCancelable(true).setOnCancelListener(DialogInterface.OnCancelListener { dialog ->
71+
dialog.dismiss()
72+
settingsActivity(this)
73+
})
74+
.setPositiveButton("SEND") { dialog, which ->
75+
Log.i("PASSWORD", "positive button")
76+
if (promptView.enterOldPassword.text.toString().isEmpty()) {
77+
Toast.makeText(this, "Input does not match your current password", Toast.LENGTH_LONG).show()
78+
} else {
79+
if (isPasswordCorrect(this, promptView.enterOldPassword.text.toString())) {
80+
import(promptView.enterOldPassword.text.toString())
81+
82+
} else {
83+
Log.i("PASSWORD", "PW false")
84+
Toast.makeText(this, "Input does not match your current password", Toast.LENGTH_LONG).show()
85+
settingsActivity(this)
86+
}
87+
}
88+
}
89+
.setNegativeButton("CANCEL") { dialog, which ->
90+
Log.i("PASSWORD", "negative button")
91+
dialog.dismiss()
92+
settingsActivity(this)
93+
}
94+
val dialog: AlertDialog = dialogBuilder.create()
95+
dialog.show()
96+
}
97+
98+
fun import(password: String) {
5299
val pkey = importEditText.text.toString()
53100
if (pkey.isEmpty())
54101
Toast.makeText(this, "Key may not be empty", Toast.LENGTH_LONG).show()
55102
else {
56-
57-
58103
try {
59104
val key = Key(Hex.decode0x(pkey))
60105

61-
val encryptorp = EnCryptor()
62-
val encryptors = EnCryptor()
63-
val encryptedPrivK = encryptors.encryptText(key.toAddressString() + "s", de.phash.manuel.asw.semux.key.Hex.encode0x(key.privateKey))
64-
val encryptedPublK = encryptorp.encryptText(key.toAddressString() + "p", de.phash.manuel.asw.semux.key.Hex.encode0x(key.publicKey))
65-
66-
67106
importAddress.text = key.toAddressString()
68107
importPubKey.text = Hex.encode0x(key.publicKey)
69108

70-
val values = ContentValues()
71-
values.put("address", key.toAddressString())
72-
values.put("publickey", org.bouncycastle.util.encoders.Hex.toHexString(encryptedPublK))
73-
values.put("privatekey", org.bouncycastle.util.encoders.Hex.toHexString(encryptedPrivK))
74-
values.put("ivs", org.bouncycastle.util.encoders.Hex.toHexString(encryptors.iv))
75-
values.put("ivp", org.bouncycastle.util.encoders.Hex.toHexString(encryptorp.iv))
76-
109+
val semuxAddress = createAccount(key, password)
110+
val values = semuxAddress.toContentValues()
77111
database.use { insert(MyDatabaseOpenHelper.SEMUXADDRESS_TABLENAME, null, values) }
112+
Toast.makeText(this, "key added", Toast.LENGTH_LONG).show()
113+
dashboardActivity(this)
114+
78115
} catch (e: CryptoException) {
79-
view.snackbar(e.localizedMessage)
80116

81-
Toast.makeText(this, "not a valid private key", Toast.LENGTH_LONG)
117+
Toast.makeText(this, "not a valid private key", Toast.LENGTH_LONG).show()
82118
}
83119

84120
}

app/src/main/java/de/phash/manuel/asw/ManageActivity.kt

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,7 @@ import de.phash.manuel.asw.database.database
4141
import de.phash.manuel.asw.semux.APIService
4242
import de.phash.manuel.asw.semux.ManageAccounts
4343
import de.phash.manuel.asw.semux.json.CheckBalance
44-
import de.phash.manuel.asw.util.checkBalanceForWallet
45-
import de.phash.manuel.asw.util.getSemuxAddress
46-
import de.phash.manuel.asw.util.isPasswordCorrect
47-
import de.phash.manuel.asw.util.isPasswordSet
44+
import de.phash.manuel.asw.util.*
4845
import kotlinx.android.synthetic.main.password_prompt.view.*
4946

5047
class ManageActivity : AppCompatActivity() {
@@ -57,7 +54,7 @@ class ManageActivity : AppCompatActivity() {
5754
setContentView(R.layout.activity_manage)
5855
setSupportActionBar(findViewById(R.id.my_toolbar))
5956
viewManager = LinearLayoutManager(this)
60-
viewAdapter = ManageAdapter(accountList, this, database)
57+
viewAdapter = ManageAdapter(accountList, this, DEFAULT_PW, database)
6158
recyclerView = findViewById<RecyclerView>(R.id.manageRecycler).apply {
6259
setHasFixedSize(true)
6360
layoutManager = viewManager

app/src/main/java/de/phash/manuel/asw/ManageAdapter.kt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,12 @@ import android.widget.Toast
3939
import de.phash.manuel.asw.database.MyDatabaseOpenHelper
4040
import de.phash.manuel.asw.semux.APIService
4141
import de.phash.manuel.asw.semux.ManageAccounts
42-
import de.phash.manuel.asw.semux.key.Hex
4342
import de.phash.manuel.asw.util.*
4443
import kotlinx.android.synthetic.main.password_prompt.view.*
4544
import java.math.BigDecimal
4645
import java.text.DecimalFormat
4746

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

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

9291

93-
val decryptedKey = DeCryptor().decryptData(account.account.address + "s", Hex.decode0x(account.account.privateKey), Hex.decode0x(account.account.ivs))
94-
holder.pkey?.text = decryptedKey
92+
val decryptedAcc = decryptAccount(account.account, password)
93+
94+
holder.pkey?.text = decryptedAcc.privateKey
9595
holder.removeButton.setOnClickListener(View.OnClickListener {
9696
removeClick(account)
9797
})
9898
holder.pkey.setOnClickListener(View.OnClickListener {
99-
copyItem(decryptedKey, "PRIVATE KEY")
99+
copyItem(decryptedAcc.privateKey ?: "", "PRIVATE KEY")
100100
})
101101
holder.copyPrivKeyBtn.setOnClickListener(View.OnClickListener {
102-
copyItem(decryptedKey, "PRIVATE KEY")
102+
copyItem(decryptedAcc.privateKey ?: "", "PRIVATE KEY")
103103
})
104104

105105

app/src/main/java/de/phash/manuel/asw/PasswordActivity.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,11 @@ import android.util.Log
3232
import android.view.LayoutInflater
3333
import android.view.View
3434
import android.widget.Toast
35+
import de.phash.manuel.asw.database.database
3536
import de.phash.manuel.asw.util.isPasswordCorrect
3637
import de.phash.manuel.asw.util.isPasswordSet
3738
import de.phash.manuel.asw.util.persistNewPassword
39+
import de.phash.manuel.asw.util.updateAllAddresses
3840
import kotlinx.android.synthetic.main.activity_passwords.*
3941
import kotlinx.android.synthetic.main.password_prompt.view.*
4042

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

0 commit comments

Comments
 (0)