Cipher works
This commit is contained in:
parent
a43e227289
commit
03ab902051
@ -14,11 +14,14 @@ import android.view.View;
|
|||||||
import android.widget.EditText;
|
import android.widget.EditText;
|
||||||
|
|
||||||
import java.io.UnsupportedEncodingException;
|
import java.io.UnsupportedEncodingException;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.security.InvalidAlgorithmParameterException;
|
import java.security.InvalidAlgorithmParameterException;
|
||||||
import java.security.InvalidKeyException;
|
import java.security.InvalidKeyException;
|
||||||
import java.security.NoSuchAlgorithmException;
|
import java.security.NoSuchAlgorithmException;
|
||||||
|
import java.security.spec.AlgorithmParameterSpec;
|
||||||
import java.security.spec.InvalidKeySpecException;
|
import java.security.spec.InvalidKeySpecException;
|
||||||
import java.security.spec.InvalidParameterSpecException;
|
import java.security.spec.InvalidParameterSpecException;
|
||||||
|
import java.util.Base64;
|
||||||
|
|
||||||
import javax.crypto.BadPaddingException;
|
import javax.crypto.BadPaddingException;
|
||||||
import javax.crypto.Cipher;
|
import javax.crypto.Cipher;
|
||||||
@ -26,6 +29,7 @@ import javax.crypto.IllegalBlockSizeException;
|
|||||||
import javax.crypto.NoSuchPaddingException;
|
import javax.crypto.NoSuchPaddingException;
|
||||||
import javax.crypto.SecretKey;
|
import javax.crypto.SecretKey;
|
||||||
import javax.crypto.SecretKeyFactory;
|
import javax.crypto.SecretKeyFactory;
|
||||||
|
import javax.crypto.spec.IvParameterSpec;
|
||||||
import javax.crypto.spec.PBEKeySpec;
|
import javax.crypto.spec.PBEKeySpec;
|
||||||
import javax.crypto.spec.SecretKeySpec;
|
import javax.crypto.spec.SecretKeySpec;
|
||||||
|
|
||||||
@ -44,8 +48,9 @@ public class NotebookActivity extends AppCompatActivity {
|
|||||||
try {
|
try {
|
||||||
SecretKey secretKey = generateKey(savedPassValue);
|
SecretKey secretKey = generateKey(savedPassValue);
|
||||||
String encryptedText = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).getString("TEXT", null);
|
String encryptedText = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).getString("TEXT", null);
|
||||||
|
//String decryptedText = decryptMsg(encryptedText.getBytes(),secretKey);
|
||||||
if (encryptedText != null) {
|
if (encryptedText != null) {
|
||||||
String noteText = decryptMsg(encryptedText.getBytes(), secretKey);
|
String noteText = decryptMsg(encryptedText, secretKey);
|
||||||
text.setText(noteText);
|
text.setText(noteText);
|
||||||
} else {
|
} else {
|
||||||
text.setText("Twoja notatka");
|
text.setText("Twoja notatka");
|
||||||
@ -66,7 +71,8 @@ public class NotebookActivity extends AppCompatActivity {
|
|||||||
if (savedPassValue != null) {
|
if (savedPassValue != null) {
|
||||||
try {
|
try {
|
||||||
SecretKey secretKey = generateKey(savedPassValue);
|
SecretKey secretKey = generateKey(savedPassValue);
|
||||||
String encryptedText = new String(encryptMsg(textValue, secretKey));
|
String encryptedText = encryptMsg(textValue, secretKey);
|
||||||
|
String ddddd = decryptMsg(encryptedText, secretKey);
|
||||||
PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).edit().putString("TEXT", encryptedText).apply();
|
PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).edit().putString("TEXT", encryptedText).apply();
|
||||||
|
|
||||||
Snackbar.make(view, "Udało się zapisać", Snackbar.LENGTH_LONG)
|
Snackbar.make(view, "Udało się zapisać", Snackbar.LENGTH_LONG)
|
||||||
@ -90,27 +96,34 @@ public class NotebookActivity extends AppCompatActivity {
|
|||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
saltBytes = "".getBytes();
|
saltBytes = "".getBytes();
|
||||||
}
|
}
|
||||||
PBEKeySpec keySpec = new PBEKeySpec(password.toCharArray(), saltBytes, 10, 128);
|
PBEKeySpec keySpec = new PBEKeySpec(password.toCharArray(), saltBytes, 10, 256);
|
||||||
return new SecretKeySpec(SecretKeyFactory.getInstance("PBKDF2WithHmacSHA512").generateSecret(keySpec).getEncoded(), "AES");
|
return new SecretKeySpec(SecretKeyFactory.getInstance("PBKDF2WithHmacSHA512").generateSecret(keySpec).getEncoded(), "AES");
|
||||||
}
|
}
|
||||||
|
|
||||||
public byte[] encryptMsg(String message, SecretKey secret)
|
public String encryptMsg(String message, SecretKey secret)
|
||||||
throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidParameterSpecException, IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException {
|
throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidParameterSpecException, IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException, InvalidAlgorithmParameterException {
|
||||||
/* Encrypt the message. */
|
/* Encrypt the message. */
|
||||||
Cipher cipher = null;
|
Cipher cipher = null;
|
||||||
cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
|
cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
|
||||||
cipher.init(Cipher.ENCRYPT_MODE, secret);
|
AlgorithmParameterSpec ivSpec = new IvParameterSpec(new byte[16]);
|
||||||
byte[] cipherText = cipher.doFinal(message.getBytes("UTF-8"));
|
cipher.init(Cipher.ENCRYPT_MODE, secret, ivSpec);
|
||||||
return cipherText;
|
byte[] utfMessageBytes = message.getBytes(StandardCharsets.UTF_8);
|
||||||
|
//byte[] base64MessageBytes = Base64.getUrlDecoder().decode(utfMessageBytes);
|
||||||
|
byte[] cipherText = cipher.doFinal(utfMessageBytes);
|
||||||
|
String result = Base64.getEncoder().encodeToString(cipherText);
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String decryptMsg(byte[] cipherText, SecretKey secret)
|
public String decryptMsg(String message, SecretKey secret)
|
||||||
throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidParameterSpecException, InvalidAlgorithmParameterException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException, UnsupportedEncodingException {
|
throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidParameterSpecException, InvalidAlgorithmParameterException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException, UnsupportedEncodingException {
|
||||||
/* Decrypt the message, given derived encContentValues and initialization vector. */
|
/* Decrypt the message, given derived encContentValues and initialization vector. */
|
||||||
Cipher cipher = null;
|
Cipher cipher = null;
|
||||||
cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
|
cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
|
||||||
cipher.init(Cipher.DECRYPT_MODE, secret);
|
AlgorithmParameterSpec ivSpec = new IvParameterSpec(new byte[16]);
|
||||||
String decryptString = new String(cipher.doFinal(cipherText), "UTF-8");
|
cipher.init(Cipher.DECRYPT_MODE, secret, ivSpec);
|
||||||
|
byte[] messageBytes = Base64.getDecoder().decode(message);//message.getBytes(StandardCharsets.UTF_8);
|
||||||
|
byte[] decryptedBytes = cipher.doFinal(messageBytes);
|
||||||
|
String decryptString = new String(decryptedBytes, StandardCharsets.UTF_8);
|
||||||
return decryptString;
|
return decryptString;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user