po ocenie - gotowe
This commit is contained in:
parent
1a41a7bdb1
commit
c2749f7020
@ -16,7 +16,6 @@ import android.widget.Toast;
|
|||||||
import androidx.appcompat.app.AlertDialog;
|
import androidx.appcompat.app.AlertDialog;
|
||||||
import androidx.appcompat.app.AppCompatActivity;
|
import androidx.appcompat.app.AppCompatActivity;
|
||||||
|
|
||||||
import java.nio.charset.StandardCharsets;
|
|
||||||
import java.security.GeneralSecurityException;
|
import java.security.GeneralSecurityException;
|
||||||
import java.security.InvalidAlgorithmParameterException;
|
import java.security.InvalidAlgorithmParameterException;
|
||||||
import java.security.InvalidKeyException;
|
import java.security.InvalidKeyException;
|
||||||
@ -314,16 +313,15 @@ public class MainActivity extends AppCompatActivity {
|
|||||||
saveIvStringToShared(ivString);
|
saveIvStringToShared(ivString);
|
||||||
|
|
||||||
//tutaj generuje randomową sól2 używam ją do enkrypcji i zapisuje do skared
|
//tutaj generuje randomową sól2 używam ją do enkrypcji i zapisuje do skared
|
||||||
byte[] salt2 = Utility.generateSalt();
|
byte[] salt2Bytes = Utility.generateSalt();
|
||||||
String salt2String = salt2BytesToString(salt2);
|
String salt2String = bytesToSalt2String(salt2Bytes);
|
||||||
saveSalt2StringToPrefs(salt2String);
|
saveSalt2StringToPrefs(salt2String);
|
||||||
|
|
||||||
editor.putInt("notecount_" + HASHED_EMAIL, noteList.size());
|
editor.putInt("notecount_" + HASHED_EMAIL, noteList.size());
|
||||||
for(int i=0; i<noteList.size(); i++){
|
for(int i=0; i<noteList.size(); i++){
|
||||||
Note note = noteList.get(i);
|
Note note = noteList.get(i);
|
||||||
editor.putString(i + "_title_" + HASHED_EMAIL, UtilityAES.encrypt("AES/CBC/PKCS5Padding", note.getTitle(), UtilityAES.getKeyFromPassword(PAS, salt2), iv));
|
editor.putString(i + "_title_" + HASHED_EMAIL, UtilityAES.encrypt("AES/CBC/PKCS5Padding", note.getTitle(), UtilityAES.getKeyFromPassword(PAS, salt2Bytes), iv));
|
||||||
editor.putString(i + "_content_" + HASHED_EMAIL, UtilityAES.encrypt("AES/CBC/PKCS5Padding", note.getContent(), UtilityAES.getKeyFromPassword(PAS, salt2), iv));
|
editor.putString(i + "_content_" + HASHED_EMAIL, UtilityAES.encrypt("AES/CBC/PKCS5Padding", note.getContent(), UtilityAES.getKeyFromPassword(PAS, salt2Bytes), iv));
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
editor.apply();
|
editor.apply();
|
||||||
@ -339,17 +337,16 @@ public class MainActivity extends AppCompatActivity {
|
|||||||
IvParameterSpec iv = stringToIv(ivString);
|
IvParameterSpec iv = stringToIv(ivString);
|
||||||
|
|
||||||
//tutaj pobieram sól2 z shared i używam do dekrypcji
|
//tutaj pobieram sól2 z shared i używam do dekrypcji
|
||||||
String saltStringFromShared = getStringSaltFromShared();
|
String salt2String = getSalt2StringFromShared();
|
||||||
byte[] salt2 = stringSalt2toSalt(saltStringFromShared);
|
byte[] salt2Bytes = Salt2StringToBytes(salt2String);
|
||||||
|
|
||||||
for(int i=0; i<noteCount; i++){
|
for(int i=0; i<noteCount; i++){
|
||||||
String title = sharedPreferences.getString(i + "_title_" + HASHED_EMAIL, "");
|
String title = sharedPreferences.getString(i + "_title_" + HASHED_EMAIL, "");
|
||||||
String content = sharedPreferences.getString(i + "_content_" + HASHED_EMAIL, "");
|
String content = sharedPreferences.getString(i + "_content_" + HASHED_EMAIL, "");
|
||||||
|
|
||||||
Note note = new Note();
|
Note note = new Note();
|
||||||
//getSaltForUser(HASHED_EMAIL, true)
|
note.setTitle(UtilityAES.decrypt("AES/CBC/PKCS5Padding", title, UtilityAES.getKeyFromPassword(PAS, salt2Bytes), iv));
|
||||||
note.setTitle(UtilityAES.decrypt("AES/CBC/PKCS5Padding", title, UtilityAES.getKeyFromPassword(PAS, salt2), iv));
|
note.setContent(UtilityAES.decrypt("AES/CBC/PKCS5Padding", content, UtilityAES.getKeyFromPassword(PAS, salt2Bytes), iv));
|
||||||
note.setContent(UtilityAES.decrypt("AES/CBC/PKCS5Padding", content, UtilityAES.getKeyFromPassword(PAS, salt2), iv));
|
|
||||||
|
|
||||||
noteList.add(note);
|
noteList.add(note);
|
||||||
}
|
}
|
||||||
@ -365,16 +362,16 @@ public class MainActivity extends AppCompatActivity {
|
|||||||
editor.apply();
|
editor.apply();
|
||||||
}
|
}
|
||||||
|
|
||||||
private String getStringSaltFromShared(){
|
private String getSalt2StringFromShared(){
|
||||||
SharedPreferences sharedPreferences = getSharedPreferences(SHARED_NAME_NOTES, MODE_PRIVATE);
|
SharedPreferences sharedPreferences = getSharedPreferences(SHARED_NAME_NOTES, MODE_PRIVATE);
|
||||||
return sharedPreferences.getString("salt_2_" + HASHED_EMAIL, "err");
|
return sharedPreferences.getString("salt_2_" + HASHED_EMAIL, "err");
|
||||||
}
|
}
|
||||||
|
|
||||||
private static byte[] stringSalt2toSalt(String salt2) {
|
private static byte[] Salt2StringToBytes(String salt2) {
|
||||||
return Base64.getDecoder().decode(salt2);
|
return Base64.getDecoder().decode(salt2);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static String salt2BytesToString(byte[] salt2) {
|
private static String bytesToSalt2String(byte[] salt2) {
|
||||||
return Base64.getEncoder().encodeToString(salt2);
|
return Base64.getEncoder().encodeToString(salt2);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -388,8 +385,6 @@ public class MainActivity extends AppCompatActivity {
|
|||||||
editor.apply();
|
editor.apply();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
private String getIVStringFromShared(){
|
private String getIVStringFromShared(){
|
||||||
SharedPreferences sharedPreferences = getSharedPreferences(SHARED_NAME_CREDENTIALS, MODE_PRIVATE);
|
SharedPreferences sharedPreferences = getSharedPreferences(SHARED_NAME_CREDENTIALS, MODE_PRIVATE);
|
||||||
return sharedPreferences.getString("iv_" + HASHED_EMAIL, "err");
|
return sharedPreferences.getString("iv_" + HASHED_EMAIL, "err");
|
||||||
@ -446,7 +441,5 @@ public class MainActivity extends AppCompatActivity {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -84,9 +84,7 @@ public class Register extends AppCompatActivity {
|
|||||||
}
|
}
|
||||||
|
|
||||||
byte[] salt1 = Utility.generateSalt();
|
byte[] salt1 = Utility.generateSalt();
|
||||||
byte[] salt2 = Utility.generateSalt();
|
saveSaltForUser(hashedEmail, salt1);
|
||||||
|
|
||||||
saveSaltsForUser(hashedEmail, salt1, salt2);
|
|
||||||
|
|
||||||
hashedPassword = Utility.hashCredential(password, salt1);
|
hashedPassword = Utility.hashCredential(password, salt1);
|
||||||
|
|
||||||
@ -121,15 +119,12 @@ public class Register extends AppCompatActivity {
|
|||||||
return matcher.matches();
|
return matcher.matches();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void saveSaltsForUser(String hashedEmail, byte[] salt1, byte[] salt2){
|
private void saveSaltForUser(String hashedEmail, byte[] salt1){
|
||||||
SharedPreferences sharedPreferences = getSharedPreferences(SHARED_NAME_CREDENTIALS, MODE_PRIVATE);
|
SharedPreferences sharedPreferences = getSharedPreferences(SHARED_NAME_CREDENTIALS, MODE_PRIVATE);
|
||||||
SharedPreferences.Editor editor = sharedPreferences.edit();
|
SharedPreferences.Editor editor = sharedPreferences.edit();
|
||||||
|
|
||||||
String salt1String = Base64.getEncoder().encodeToString(salt1);
|
String salt1String = Base64.getEncoder().encodeToString(salt1);
|
||||||
String salt2String = Base64.getEncoder().encodeToString(salt2);
|
|
||||||
|
|
||||||
editor.putString("salt_" + hashedEmail, salt1String);
|
editor.putString("salt_" + hashedEmail, salt1String);
|
||||||
editor.putString("salt_2_" + hashedEmail, salt2String);
|
|
||||||
|
|
||||||
editor.apply();
|
editor.apply();
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user