2

In my application I i'm encoding a string and then generating a MAC id (using javax.crypto.Mac).But i tried to decode it back ,but i couldn't.Can you please help me out.Can you point out where I have done wrong?

Code

String userid = "AmilaI";
String time = gmtFormat.format(now)+ "Z";

String algorithmKey = time + userid;

SecretKeySpec sks = new SecretKeySpec(algorithmKey.getBytes("UTF-8"), "HmacSHA1");
Mac mac = Mac.getInstance("HmacSHA1");
mac.init(sks);
byte[] hashBytes = mac.doFinal(route.getBytes("UTF-8"));

String hmac = Base64.encodeBase64String(hashBytes);
hmac = hmac.replaceAll("\r\n", "");
System.out.println("Encrypted "+ hmac );

byte[] decoded = Base64.decodeBase64(hmac);
System.out.println("Decrypted " + new String(decoded, "UTF-8") + "\n");

How can I reverse the path and get alogorithmKey back so I can get the user id by decrypting?

2 Answers 2

6

HmacSHA1 is a hash so it means it works only one way, you cannot get the original value from it. You would need to use algorithm that is reversible.

Sign up to request clarification or add additional context in comments.

Comments

1

Try this

 public class Crypto {

        private static final String engine = "AES";
        private static final String crypto = "AES/CBC/PKCS5Padding";
        private static Context ctx;

        public Crypto(Context cntx) {
            ctx = cntx;
        }

        public byte[] cipher(byte[] data, int mode)
                throws NoSuchAlgorithmException, NoSuchPaddingException,
                InvalidKeyException, IllegalBlockSizeException,
                BadPaddingException, InvalidAlgorithmParameterException {
            KeyManager km = new KeyManager(ctx);
            SecretKeySpec sks = new SecretKeySpec(km.getId(), engine);
            IvParameterSpec iv = new IvParameterSpec(km.getIv());
            Cipher c = Cipher.getInstance(crypto);
            c.init(mode, sks, iv);
            return c.doFinal(data);
        }

        public byte[] encrypt(byte[] data) throws InvalidKeyException,
                NoSuchAlgorithmException, NoSuchPaddingException,
                IllegalBlockSizeException, BadPaddingException,
                InvalidAlgorithmParameterException {
            return cipher(data, Cipher.ENCRYPT_MODE);
        }

        public byte[] decrypt(byte[] data) throws InvalidKeyException,
                NoSuchAlgorithmException, NoSuchPaddingException,
                IllegalBlockSizeException, BadPaddingException,
                InvalidAlgorithmParameterException {
            return cipher(data, Cipher.DECRYPT_MODE);
        }

        public String armorEncrypt(byte[] data) throws InvalidKeyException,
                NoSuchAlgorithmException, NoSuchPaddingException,
                IllegalBlockSizeException, BadPaddingException,
                InvalidAlgorithmParameterException {
            return Base64.encodeToString(encrypt(data), Base64.DEFAULT);
        }

        public String armorDecrypt(String data) throws InvalidKeyException,
                NoSuchAlgorithmException, NoSuchPaddingException,
                IllegalBlockSizeException, BadPaddingException,
                InvalidAlgorithmParameterException {
            return new String(decrypt(Base64.decode(data, Base64.DEFAULT)));
        }
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.