package com.test; import java.io.UnsupportedEncodingException; import java.net.MalformedURLException; import java.net.URISyntaxException; import java.net.URLDecoder; import java.net.URLEncoder; import java.security.MessageDigest; import javax.crypto.Cipher; import javax.crypto.spec.IvParameterSpec; import javax.crypto.spec.SecretKeySpec; import org.apache.commons.codec.DecoderException; import org.apache.commons.codec.EncoderException; import org.apache.commons.codec.binary.Base64; public class SHA256 { public static void main(String[] args) throws UnsupportedEncodingException, EncoderException, DecoderException, MalformedURLException, URISyntaxException { // encryptString(); String strKey = "ItGjgFQB80c5WN5VT1Y9ZLZIRcR48JSvmee0e4Yu3ig%3D"; String encStr = encodeString(strKey,"xxxx","yyyy"); //System.out.println("encodeString: "+encStr); String url = "https://xxx.yyy.zzz.com/abc/def/AuthenticationToken?cred=" + encStr + "&key=" + strKey; //if (encStr != null) //System.out.println("final url: \t" + url); } public static String encodeString(String key, String username, String password) throws UnsupportedEncodingException, EncoderException, DecoderException, MalformedURLException, URISyntaxException { String userCred = username + (char) 30 + password; String urlDecodedKey = URLDecoder.decode(key, "UTF-8"); System.out.println("decodedkey: "+urlDecodedKey); byte[] simple1 = Base64.decodeBase64(urlDecodedKey); // sha256 hash for url decoded key byte[] sha256bytes = org.apache.commons.codec.digest.DigestUtils .sha256(simple1); byte[] iv = new byte[16]; // remove every other char for (int i = 0; i < 16; i++) { iv[i] = sha256bytes[i * 2]; } // encrypt and base64encode String encryptedString = encrypt(simple1, iv, userCred); // uriencode the base 64 string System.out.println("cred: "+encryptedString); String Decodedurl = "https://xxx.yyy.zzz.com/abc/def/AuthenticationToken?cred=" + encryptedString + "&key=" + urlDecodedKey; //System.out.println("Decoded url "+Decodedurl); encryptedString = URLEncoder.encode(encryptedString, "UTF-8"); return encryptedString; } public static String sha256(String base) { try { MessageDigest digest = MessageDigest.getInstance("SHA-256"); byte[] hash = digest.digest(base.getBytes("UTF-8")); StringBuffer hexString = new StringBuffer(); for (int i = 0; i < hash.length; i++) { String hex = Integer.toHexString(0xff & hash[i]); if (hex.length() == 1) hexString.append('0'); hexString.append(hex); } return hexString.toString(); } catch (Exception ex) { throw new RuntimeException(ex); } } public static String encrypt(byte[] key, byte[] initVector, String value) { try { // byte [] ivTemp = initVector.getBytes("UTF-8"); IvParameterSpec iv = new IvParameterSpec(initVector); SecretKeySpec skeySpec = new SecretKeySpec(key, "AES"); Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING"); cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv); byte[] encrypted = cipher.doFinal(value.getBytes("UTF-8")); // Arrays.toString(encrypted); return Base64.encodeBase64String(encrypted); // return new String(encrypted); } catch (Exception ex) { ex.printStackTrace(); } return null; } }