Search This Blog

Friday, January 9, 2015

Encrypt the password using algorithm and Decrypt it using one secret key

Its necessary to encrypt the user data that is not to be revealed by any third party user which can be harmful to the user.

There may be so many encryption and decryption techniques available for encrypt and decrypt the data. But I am posting one of them here.

Firstly, Copy following class into your code. And import all the classes.

 public class Encryption {
  private static final String TAG = "Encryption";
  private String mCharsetName = "UTF8";
  private int mBase64Mode = Base64.DEFAULT;
  private String mSecretKeyType = "PBKDF2WithHmacSHA1";
  private String mSalt = "some_salt";
  private int mKeyLength = 128;
  private int mIterationCount = 65536;
  private String mAlgorithm = "AES";

  public String encrypt(String key, String data) {
         if (key == null || data == null)
             return null;
         try {
            SecretKey secretKey = getSecretKey(hashTheKey(key));
            byte[] dataBytes = data.getBytes(mCharsetName);
            Cipher cipher = Cipher.getInstance(mAlgorithm);
            cipher.init(Cipher.ENCRYPT_MODE, secretKey);
            return Base64.encodeToString(cipher.doFinal(dataBytes), mBase64Mode);
        } catch (Exception e) {
            Log.e(TAG, e.toString());
            return null;
        }
   }
   public String decrypt(String key, String data) {
           if (key == null || data == null)
               return null;
           try {
               byte[] dataBytes = Base64.decode(data, mBase64Mode);
               SecretKey secretKey = getSecretKey(hashTheKey(key));
               Cipher cipher = Cipher.getInstance(mAlgorithm);
               cipher.init(Cipher.DECRYPT_MODE, secretKey);
               byte[] dataBytesDecrypted = (cipher.doFinal(dataBytes));
               return new String(dataBytesDecrypted);
            } catch (Exception e) {
               Log.e(TAG, e.toString());
               return null;
            }
   }

   private SecretKey getSecretKey(char[] key) throws NoSuchAlgorithmException,
               UnsupportedEncodingException, InvalidKeySpecException {
          SecretKeyFactory factory;
          factory = SecretKeyFactory.getInstance(mSecretKeyType);
          KeySpec spec = new PBEKeySpec(key, mSalt.getBytes(mCharsetName),
                          mIterationCount, mKeyLength);
          SecretKey tmp = factory.generateSecret(spec);
          return new SecretKeySpec(tmp.getEncoded(), mAlgorithm);
   }

     private char[] hashTheKey(String key) throws UnsupportedEncodingException,
                 NoSuchAlgorithmException {
           MessageDigest md = MessageDigest.getInstance("SHA1");
           md.update(key.getBytes(mCharsetName));
           return Base64.encodeToString(md.digest(), Base64.NO_PADDING)
                            .toCharArray();
     }
}



Using this class please create an object of it. Please set One secret key by which you want to encrypt the password.

public static String secretKey = "YourSecreteKey";


Encryption encryption = new Encryption();
String encryptedPassword = encryption
.encrypt(
secretKey 
, masterPassword
.getText().toString());


using this secretKey you will get the encrypted data.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.