po ocenie - gotowe

This commit is contained in:
mikgaw@st.amu.edu.pl 2023-12-08 12:41:01 +01:00
parent 1a41a7bdb1
commit c2749f7020
2 changed files with 13 additions and 25 deletions

View File

@ -16,7 +16,6 @@ import android.widget.Toast;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import java.nio.charset.StandardCharsets;
import java.security.GeneralSecurityException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
@ -314,16 +313,15 @@ public class MainActivity extends AppCompatActivity {
saveIvStringToShared(ivString);
//tutaj generuje randomową sól2 używam do enkrypcji i zapisuje do skared
byte[] salt2 = Utility.generateSalt();
String salt2String = salt2BytesToString(salt2);
byte[] salt2Bytes = Utility.generateSalt();
String salt2String = bytesToSalt2String(salt2Bytes);
saveSalt2StringToPrefs(salt2String);
editor.putInt("notecount_" + HASHED_EMAIL, noteList.size());
for(int i=0; i<noteList.size(); 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 + "_content_" + HASHED_EMAIL, UtilityAES.encrypt("AES/CBC/PKCS5Padding", note.getContent(), 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, salt2Bytes), iv));
}
editor.apply();
@ -339,17 +337,16 @@ public class MainActivity extends AppCompatActivity {
IvParameterSpec iv = stringToIv(ivString);
//tutaj pobieram sól2 z shared i używam do dekrypcji
String saltStringFromShared = getStringSaltFromShared();
byte[] salt2 = stringSalt2toSalt(saltStringFromShared);
String salt2String = getSalt2StringFromShared();
byte[] salt2Bytes = Salt2StringToBytes(salt2String);
for(int i=0; i<noteCount; i++){
String title = sharedPreferences.getString(i + "_title_" + HASHED_EMAIL, "");
String content = sharedPreferences.getString(i + "_content_" + HASHED_EMAIL, "");
Note note = new Note();
//getSaltForUser(HASHED_EMAIL, true)
note.setTitle(UtilityAES.decrypt("AES/CBC/PKCS5Padding", title, UtilityAES.getKeyFromPassword(PAS, salt2), iv));
note.setContent(UtilityAES.decrypt("AES/CBC/PKCS5Padding", content, UtilityAES.getKeyFromPassword(PAS, salt2), iv));
note.setTitle(UtilityAES.decrypt("AES/CBC/PKCS5Padding", title, UtilityAES.getKeyFromPassword(PAS, salt2Bytes), iv));
note.setContent(UtilityAES.decrypt("AES/CBC/PKCS5Padding", content, UtilityAES.getKeyFromPassword(PAS, salt2Bytes), iv));
noteList.add(note);
}
@ -365,16 +362,16 @@ public class MainActivity extends AppCompatActivity {
editor.apply();
}
private String getStringSaltFromShared(){
private String getSalt2StringFromShared(){
SharedPreferences sharedPreferences = getSharedPreferences(SHARED_NAME_NOTES, MODE_PRIVATE);
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);
}
private static String salt2BytesToString(byte[] salt2) {
private static String bytesToSalt2String(byte[] salt2) {
return Base64.getEncoder().encodeToString(salt2);
}
@ -388,8 +385,6 @@ public class MainActivity extends AppCompatActivity {
editor.apply();
}
private String getIVStringFromShared(){
SharedPreferences sharedPreferences = getSharedPreferences(SHARED_NAME_CREDENTIALS, MODE_PRIVATE);
return sharedPreferences.getString("iv_" + HASHED_EMAIL, "err");
@ -446,7 +441,5 @@ public class MainActivity extends AppCompatActivity {
}
}

View File

@ -84,9 +84,7 @@ public class Register extends AppCompatActivity {
}
byte[] salt1 = Utility.generateSalt();
byte[] salt2 = Utility.generateSalt();
saveSaltsForUser(hashedEmail, salt1, salt2);
saveSaltForUser(hashedEmail, salt1);
hashedPassword = Utility.hashCredential(password, salt1);
@ -121,15 +119,12 @@ public class Register extends AppCompatActivity {
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.Editor editor = sharedPreferences.edit();
String salt1String = Base64.getEncoder().encodeToString(salt1);
String salt2String = Base64.getEncoder().encodeToString(salt2);
editor.putString("salt_" + hashedEmail, salt1String);
editor.putString("salt_2_" + hashedEmail, salt2String);
editor.apply();
}