Currently I am working on SHA-1 Hash Algoritham can any one suggest me how can such algo be developed using the secret key?
Thanks :-)
Similar question : How can I calculate the SHA-256 hash of a string with a secret key in Android?.
Thanks.
Currently I am working on SHA-1 Hash Algoritham can any one suggest me how can such algo be developed using the secret key?
Thanks :-)
Similar question : How can I calculate the SHA-256 hash of a string with a secret key in Android?.
Thanks.
Are you talking of a salt or something ? I think you'd like to use an encryption you can resverse such as AES.
public static byte[] encrypt(String plainText, String encryptionKey) throws Exception
{
Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding", "SunJCE");
SecretKeySpec key = new SecretKeySpec(encryptionKey.getBytes("UTF-8"), "AES");
cipher.init(Cipher.ENCRYPT_MODE, key,new IvParameterSpec(new byte[cipher.getBlockSize()]));
return cipher.doFinal(plainText.getBytes("UTF-8"));
}
public static String decrypt(byte[] cipherText, String encryptionKey) throws Exception
{
Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding", "SunJCE");
SecretKeySpec key = new SecretKeySpec(encryptionKey.getBytes("UTF-8"), "AES");
cipher.init(Cipher.DECRYPT_MODE, key,new IvParameterSpec(new byte[cipher.getBlockSize()]));
return new String(cipher.doFinal(cipherText),"UTF-8");
}
A simple way to use it would be
String plaintext = "My plain text.";
String encryptionKey = "Super secret encryption key";
Log.d("AES", "Plain text is : " + plaintext );
byte[] cipher = encrypt(plaintext, encryptionKey);
Log.d("AES", "Encrypted string is : " + new String(cipher));
String decrypted = decrypt(cipher, encryptionKey);
Log.d("AES", "Decrypted string is : " + decrypted);
if (plaintext.equals(decrypted)) Log.d("AES", "Strings match !");