Skip to content Skip to sidebar Skip to footer

How Can I Encrypte My Password - Android Studio

does someone know how to encrypte the password which the user add`s into the password field? I tried this tutorial but I didn't get it work. https://gist.github.com/aogilvie/62670

Solution 1:

publicclassAESCrypt
{
    privatestaticfinalStringALGORITHM="AES";
    privatestaticfinalStringKEY="1Hbfh667adfDEJ78";

    publicstatic String encrypt(String value)throws Exception
    {
        Keykey= generateKey();
        Ciphercipher= Cipher.getInstance(AESCrypt.ALGORITHM);
        cipher.init(Cipher.ENCRYPT_MODE, key);
        byte [] encryptedByteValue = cipher.doFinal(value.getBytes("utf-8"));
        StringencryptedValue64= Base64.encodeToString(encryptedByteValue, Base64.DEFAULT);
        return encryptedValue64;

    }

    publicstatic String decrypt(String value)throws Exception
    {
        Keykey= generateKey();
        Ciphercipher= Cipher.getInstance(AESCrypt.ALGORITHM);
        cipher.init(Cipher.DECRYPT_MODE, key);
        byte[] decryptedValue64 = Base64.decode(value, Base64.DEFAULT);
        byte [] decryptedByteValue = cipher.doFinal(decryptedValue64);
        StringdecryptedValue=newString(decryptedByteValue,"utf-8");
        return decryptedValue;

    }

    privatestatic Key generateKey()throws Exception
    {
        Keykey=newSecretKeySpec(AESCrypt.KEY.getBytes(),AESCrypt.ALGORITHM);
        return key;
    }
}

Use this will solve your problem.

Solution 2:

Quote this post Difference between Hashing a Password and Encrypting it I would recommend you to use hashing (no encrypting) to store passwords. You can use i.e. md5 (not reccomend), sha1, sha2...

Exampled implementation of SHA1: How to SHA1 hash a string in Android?

Solution 3:

This is the easiest solution ever existed for normal encryption. First, add this in your build gradle file:

    implementation 'com.scottyab:aescrypt:0.0.1'

Then use the bellow code for encryption and decryption:

// To EncryptString password = "password";
String message = "hello world";	
try {
    String encryptedMsg = AESCrypt.encrypt(password, message);
}catch (GeneralSecurityException e){
    //handle error
}

// To DecryptString password = "password";
String encryptedMsg = "2B22cS3UC5s35WBihLBo8w==";
try {
    String messageAfterDecrypt = AESCrypt.decrypt(password, encryptedMsg);
}catch (GeneralSecurityException e){
     //handle error - could be due to incorrect password or tampered encryptedMsg
}

Post a Comment for "How Can I Encrypte My Password - Android Studio"