I recommend that you use SHA512, MD5 is a less secure algorithm, since it is possible that there are coincidences between different Strings.
This method has to pass two Strings, the first is the password, user or whatever you want to encrypt. The second is a string to make the encryption method more random.
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public String get_SHA_512_SecurePassword(String passwordToHash, String salt){
String generatedPassword = null;
try {
MessageDigest md = MessageDigest.getInstance("SHA-512");
md.update(salt.getBytes("UTF-8"));
byte[] bytes = md.digest(passwordToHash.getBytes("UTF-8"));
StringBuilder sb = new StringBuilder();
for(int i=0; i< bytes.length ;i++){
sb.append(Integer.toString((bytes[i] & 0xff) + 0x100, 16).substring(1));
}
generatedPassword = sb.toString();
}
catch (NoSuchAlgorithmException e){
e.printStackTrace();
}
return generatedPassword;
}
Then, as you have been told in other solutions, you should only compare it with what is written in that file. Logically, in the file the data must be already encrypted, but anyone could read it. When you receive the data entered by the user, you encrypt and compare it with what is written in the file.
Source: hash a password with sha 512 in java
EDITED
In case you have to encrypt and decrypt, use the class Cipher .
This class allows you cryptographic encryption for encryption and decryption.
An example:
public static SecretKey generateKey()
throws NoSuchAlgorithmException, InvalidKeySpecException
{
return secret = new SecretKeySpec(password.getBytes(), "AES");
}
public static byte[] encryptMsg(String message, SecretKey secret)
throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidParameterSpecException, IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException
{
/* Encrypt the message. */
Cipher cipher = null;
cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, secret);
byte[] cipherText = cipher.doFinal(message.getBytes("UTF-8"));
return cipherText;
}
public static String decryptMsg(byte[] cipherText, SecretKey secret)
throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidParameterSpecException, InvalidAlgorithmParameterException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException, UnsupportedEncodingException
{
/* Decrypt the message, given derived encContentValues and initialization vector. */
Cipher cipher = null;
cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, secret);
String decryptString = new String(cipher.doFinal(cipherText), "UTF-8");
return decryptString;
}
To encrypt:
SecretKey secret = generateKey();
encryptMsg(String toEncrypt, secret))
To decrypt:
decryptMsg(byte[] toDecrypt, secret))
You just have to save the encrypted data in a file and decrypt it before sending it. Although keep in mind that from Android N this class will be obsolete.
Sources: easy way to encrypt decrypt , < a href="https://android-developers.googleblog.com/2016/06/security-crypto-provider-deprecated-in.htm"> security crypto provider deprecated