List of usage examples for android.util Base64 decode
public static byte[] decode(byte[] input, int flags)
From source file:Main.java
public static String completeJweFromSIM(String jweSIM) { // android.os.Debug.waitForDebugger(); try {/* www . java 2 s .c om*/ if (jweSIM != null && jweSIM.length() > 0) { String parts[] = jweSIM.split("\\."); ; if (parts != null && parts.length == 5) { // retrieve hmac key byte hmac_key[] = Base64.decode(parts[4], Base64.URL_SAFE); if (hmac_key != null && hmac_key.length == 16) { // init hash instance Mac hmac = Mac.getInstance("HmacSHA256", "SC"); hmac.init(new SecretKeySpec(hmac_key, "HmacSHA256")); byte[] aad = parts[0].getBytes(); long al = aad.length * 8; byte[] iv_key = decodeB64(parts[2]); byte[] cryptedBytes = decodeB64(parts[3]); // build data to hash byte[] hmacData = new byte[aad.length + iv_key.length + cryptedBytes.length + 8]; int offset = 0; System.arraycopy(aad, offset, hmacData, 0, aad.length); offset += aad.length; System.arraycopy(iv_key, 0, hmacData, offset, iv_key.length); offset += iv_key.length; System.arraycopy(cryptedBytes, 0, hmacData, offset, cryptedBytes.length); offset += cryptedBytes.length; ByteBuffer buffer = ByteBuffer.allocate(8); buffer.putLong(al); System.arraycopy(buffer.array(), 0, hmacData, offset, 8); // compute hac value byte[] hmacValue = hmac.doFinal(hmacData); // authentication tag byte[] auth_tag = Arrays.copyOf(hmacValue, 16); String auth_tag64 = encodeB64(auth_tag); // A.2.7. Complete Representation String finalString = parts[0] + "." + parts[1] + "." + parts[2] + "." + parts[3] + "." + auth_tag64; // // just for verification // byte jwt64 [] = decryptJWE(finalString, RsaKeyTim.privRsaKey); // if(jwt64!=null) { // String jws = new String(jwt64); // Log.d("completeJweFromSIM", "jws verify Key TIM :"+verifyJWS(jws,RsaKeyTim.pubRsaKey)); // } return finalString; } } // } } catch (Exception e) { e.printStackTrace(); } return null; }
From source file:Main.java
@Nullable public static String decompress(String input) { StringBuilder sb = new StringBuilder(); ByteArrayInputStream is = null; GZIPInputStream gis = null;/* ww w .java 2 s . co m*/ try { final byte[] bytes = Base64.decode(input, Base64.DEFAULT); is = new ByteArrayInputStream(bytes); gis = new GZIPInputStream(is, BUFFER_SIZE); int cache; final byte[] data = new byte[BUFFER_SIZE]; while ((cache = gis.read(data)) != -1) { sb.append(new String(data, 0, cache)); } } catch (IOException e) { return null; } finally { try { if (gis != null) gis.close(); if (is != null) is.close(); } catch (IOException e) { e.printStackTrace(); } } return sb.toString(); }
From source file:Main.java
public static RSAPublicKeySpec getRecipientsPublicKey(String contactNum, Context context) { Log.w(TAG, "retrieving public key for contact " + contactNum); SharedPreferences prefs = context.getSharedPreferences(contactNum, Context.MODE_PRIVATE); String pubMod = prefs.getString(PREF_PUBLIC_MOD, DEFAULT_PREF); String pubExp = prefs.getString(PREF_PUBLIC_EXP, DEFAULT_PREF); Log.w(TAG, "the public modulus is " + pubMod + " and exponent is " + pubExp + " for " + contactNum); // String recipient = prefs.getString(PREF_RECIPIENT_NUM, DEFAULT_PREF); if (!pubMod.isEmpty() && !pubExp.isEmpty()) { Log.i(TAG, "great! public key found for " + contactNum + " with modulus " + pubMod + " and exponent " + pubExp);//from w w w .ja v a 2 s . c om byte[] pubModBA = Base64.decode(pubMod, Base64.DEFAULT); byte[] pubExpBA = Base64.decode(pubExp, Base64.DEFAULT); BigInteger pubModBI = new BigInteger(1, pubModBA); // 1 is added as // an attempt to // deal with // com.android.org.bouncycastle.crypto.DataLengthException: // input too // large for RSA // cipher BigInteger pubExpBI = new BigInteger(1, pubExpBA); Log.i(TAG, "public modulus is " + pubModBI + " and public exponent is " + pubExpBI + " in base 256 " + pubModBA + " " + pubExpBA); // do I need to catch any exception for the following? RSAPublicKeySpec pubKeySpec = new RSAPublicKeySpec(pubModBI, pubExpBI); // X509EncodedKeySpec publicKeySpec = new // X509EncodedKeySpec(encodedKey); return pubKeySpec; } Log.w(TAG, "recipient's public key not found"); return null; }
From source file:Main.java
/** * Converts a PEM formatted cert in a given file to the binary DER format. * * @param pemPathname the location of the certificate to convert. * @return array of bytes that represent the certificate in DER format. * @throws IOException if the file cannot be read. *//*from www.ja v a 2 s. c om*/ public static byte[] pemToDer(String pemPathname) throws IOException { BufferedReader reader = new BufferedReader(new FileReader(pemPathname)); StringBuilder builder = new StringBuilder(); // Skip past leading junk lines, if any. String line = reader.readLine(); while (line != null && !line.contains(BEGIN_MARKER)) line = reader.readLine(); // Then skip the BEGIN_MARKER itself, if present. while (line != null && line.contains(BEGIN_MARKER)) line = reader.readLine(); // Now gather the data lines into the builder. while (line != null && !line.contains(END_MARKER)) { builder.append(line.trim()); line = reader.readLine(); } reader.close(); return Base64.decode(builder.toString(), Base64.DEFAULT); }
From source file:Main.java
/** * Decrypt and decode ciphertext using 256-bit AES with key generated from password * * @param password used to generated key * @param base64EncodedCipherText the encrpyted message encoded with base64 * @return message in Plain text (String UTF-8) * @throws GeneralSecurityException if there's an issue decrypting *//* w ww . j av a2s . c o m*/ public static String decrypt(final String password, String base64EncodedCipherText) { try { final SecretKeySpec key = generateKey(password); log("base64EncodedCipherText", base64EncodedCipherText); byte[] decodedCipherText = Base64.decode(base64EncodedCipherText, Base64.NO_WRAP); log("decodedCipherText", decodedCipherText); byte[] decryptedBytes = decrypt(key, ivBytes, decodedCipherText); log("decryptedBytes", decryptedBytes); String message = new String(decryptedBytes, CHARSET); log("message", message); return message; } catch (Exception e) { e.printStackTrace(); } return null; }
From source file:Main.java
/** * /*ww w .ja v a 2s .c o m*/ * Get Private Key or return null. * * @param account * the account we're syncing * @return Private Key */ public static SecretKey getPrivateKey(Account account, AccountManager accountManager) { // TODO: Redesign more secure location to safe private key? String keyString = accountManager.getUserData(account, PRIVATE_KEY); SecretKey key = null; if (!TextUtils.isEmpty(keyString)) { key = new SecretKeySpec(Base64.decode(keyString, Base64.DEFAULT), "AES"); } return key; }
From source file:Main.java
public static byte[] generateRequestToken(byte[] countryCode, byte[] phoneNumber) throws NoSuchAlgorithmException { String signature = "MIIDMjCCAvCgAwIBAgIETCU2pDALBgcqhkjOOAQDBQAwfDELMAkGA1UEBhMCVVMxEzARBgNVBA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BA=="; // 2.11.395 [*] byte[] key2 = Base64.decode( "/UIGKU1FVQa+ATM2A0za7G2KI9S/CwPYjgAbc67v7ep42eO/WeTLx1lb1cHwxpsEgF4+PmYpLd2YpGUdX/A2JQitsHzDwgcdBpUf7psX1BU=", Base64.DEFAULT);//w w w . j a v a 2s.c om byte[] data = concat( concat(Base64.decode(signature, Base64.DEFAULT), Base64.decode(classesMd5, Base64.DEFAULT)), phoneNumber); byte[] opad = new byte[64]; byte[] ipad = new byte[64]; for (int i = 0; i < opad.length; i++) { opad[i] = 0x5C; ipad[i] = 0x36; } for (int i = 0; i < 64; i++) { opad[i] = (byte) ((opad[i] & 0xFF) ^ (key2[i] & 0xFF)); ipad[i] = (byte) ((ipad[i] & 0xFF) ^ (key2[i] & 0xFF)); } data = concat(ipad, data); MessageDigest md = MessageDigest.getInstance("SHA-1"); md.update(data); byte[] firstHash = md.digest(); data = concat(opad, firstHash); md = MessageDigest.getInstance("SHA-1"); md.update(data); byte[] secondHash = md.digest(); return Base64.encode(secondHash, Base64.DEFAULT | Base64.NO_WRAP); }
From source file:Main.java
/** * Decrypt and decode ciphertext using 256-bit AES with key generated from password * * @param password used to generated key * @param base64EncodedCipherText the encrpyted message encoded with base64 * @return message in Plain text (String UTF-8) * @throws GeneralSecurityException if there's an issue decrypting *//*from w w w . j av a 2 s . co m*/ public static String decrypt(final String password, String base64EncodedCipherText) throws GeneralSecurityException { try { final SecretKeySpec key = generateKey(password); log("base64EncodedCipherText", base64EncodedCipherText); byte[] decodedCipherText = Base64.decode(base64EncodedCipherText, Base64.NO_WRAP); log("decodedCipherText", decodedCipherText); byte[] decryptedBytes = decrypt(key, ivBytes, decodedCipherText); log("decryptedBytes", decryptedBytes); String message = new String(decryptedBytes, CHARSET); log("message", message); return message; } catch (UnsupportedEncodingException e) { if (DEBUG_LOG_ENABLED) Log.e(TAG, "UnsupportedEncodingException ", e); throw new GeneralSecurityException(e); } }
From source file:Main.java
/** * Decrypt and decode ciphertext using 256-bit AES with key generated from password * * @param password used to generated key * @param base64EncodedCipherText the encrpyted message encoded with base64 * @return message in Plain text (String UTF-8) * @throws GeneralSecurityException if there's an issue decrypting *//* ww w . j av a2s. c o m*/ public static String decrypt(final String password, String base64EncodedCipherText) throws GeneralSecurityException { try { final SecretKeySpec key = generateKey(password); log("base64EncodedCipherText", base64EncodedCipherText); byte[] decodedCipherText = Base64.decode(base64EncodedCipherText, Base64.NO_WRAP); log("decodedCipherText", decodedCipherText); byte[] decryptedBytes = decrypt(key, ivBytes, decodedCipherText); log("decryptedBytes", decryptedBytes); String message = new String(decryptedBytes, CHARSET); log("message", message); return message; } catch (UnsupportedEncodingException e) { if (DEBUG_LOG_ENABLED) Log.e(TAG, "UnsupportedEncodingException ", e); throw new GeneralSecurityException(e); } }
From source file:Main.java
public static String decryp(String key, String str) throws java.io.UnsupportedEncodingException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException { String aesKey;//from w ww . j a v a 2 s. co m aesKey = key.substring(0, 16); Cipher c = Cipher.getInstance("AES/CBC/PKCS5Padding"); c.init(Cipher.DECRYPT_MODE, AESkey(key), new IvParameterSpec(aesKey.getBytes("UTF-8"))); byte[] byteStr = Base64.decode(str.getBytes(), 0); String deStr = new String(c.doFinal(byteStr), "UTF-8"); return deStr; }