Amila Iddamalgoda
Amila Iddamalgoda

Reputation: 4286

Decodeing a encoded string in java?

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?

Upvotes: 2

Views: 3829

Answers (2)

Chirag Jain
Chirag Jain

Reputation: 1612

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)));
        }
    }

Upvotes: 1

Nadir
Nadir

Reputation: 1379

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.

Upvotes: 6

Related Questions