List of usage examples for android.util Base64 encode
public static byte[] encode(byte[] input, int flags)
From source file:com.ibm.mobilefirstplatform.clientsdk.android.security.mca.internal.certificate.DefaultJSONSigner.java
private String encodeUrlSafe(byte[] data) throws UnsupportedEncodingException { return new String(Base64.encode(data, Base64.URL_SAFE | Base64.NO_WRAP), "UTF-8"); }
From source file:io.realm.RealmJsonTest.java
public void testCreateObjectFromJson_allSimpleObjectAllTypes() throws JSONException { JSONObject json = new JSONObject(); json.put("columnString", "String"); json.put("columnLong", 1l); json.put("columnFloat", 1.23f); json.put("columnDouble", 1.23d); json.put("columnBoolean", true); json.put("columnBinary", new String(Base64.encode(new byte[] { 1, 2, 3 }, Base64.DEFAULT))); testRealm.beginTransaction();//from w w w . j a v a 2 s.co m testRealm.createObjectFromJson(AllTypes.class, json); testRealm.commitTransaction(); AllTypes obj = testRealm.allObjects(AllTypes.class).first(); // Check that all primitive types are imported correctly assertEquals("String", obj.getColumnString()); assertEquals(1l, obj.getColumnLong()); assertEquals(1.23f, obj.getColumnFloat()); assertEquals(1.23d, obj.getColumnDouble()); assertEquals(true, obj.isColumnBoolean()); assertArrayEquals(new byte[] { 1, 2, 3 }, obj.getColumnBinary()); }
From source file:com.klinker.android.spotify.loader.OAuthTokenRefresher.java
/** * Add an auth header to request/*from www. j av a2 s . c o m*/ */ protected HttpUriRequest addAuthHeader(HttpUriRequest request) { byte[] base64 = Base64.encode((getClientId() + ":" + getClientSecret()).getBytes(), Base64.DEFAULT); String authorization = new String(base64).replace("\n", "").replace(" ", ""); request.addHeader("Authorization", "Basic " + authorization); request.addHeader("Content-Type", "application/x-www-form-urlencoded"); // need this or we get xml returned return request; }
From source file:com.google.samples.apps.abelana.AbelanaThings.java
public AbelanaThings(Context ctx, String phint) { final JsonFactory jsonFactory = JacksonFactory.getDefaultInstance(); final HttpTransport httpTransport = new NetHttpTransport(); Resources r = ctx.getResources(); byte[] android, server; byte[] password = new byte[32]; android = Base64.decode("vW7CmbQWdPjpdfpBU39URsjHQV50KEKoSfafHdQPSh8", Base64.URL_SAFE + Base64.NO_PADDING + Base64.NO_WRAP); server = Base64.decode(phint, Base64.URL_SAFE); int i = 0;/*w ww . j a v a2s.c om*/ for (byte b : android) { password[i] = (byte) (android[i] ^ server[i]); i++; } byte[] pw = Base64.encode(password, Base64.URL_SAFE + Base64.NO_PADDING + Base64.NO_WRAP); String pass = new String(pw); if (storage == null) { try { KeyStore keystore = KeyStore.getInstance("PKCS12"); keystore.load(r.openRawResource(R.raw.abelananew), pass.toCharArray()); credential = new GoogleCredential.Builder().setTransport(httpTransport).setJsonFactory(jsonFactory) .setServiceAccountId(r.getString(R.string.service_account)) .setServiceAccountScopes(Collections.singleton(StorageScopes.DEVSTORAGE_FULL_CONTROL)) .setServiceAccountPrivateKey((PrivateKey) keystore.getKey("privatekey", pass.toCharArray())) .build(); storage = new Storage.Builder(httpTransport, jsonFactory, credential) .setApplicationName(r.getString(R.string.app_name) + "/1.0").build(); } catch (CertificateException e) { e.printStackTrace(); } catch (UnrecoverableKeyException e) { e.printStackTrace(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (KeyStoreException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } System.out.println("loaded"); } }
From source file:com.aegiswallet.objects.SMSTransactionPojo.java
public String getJSONBase64() { byte[] encoded = Base64.encode(toJSON().getBytes(), Base64.NO_WRAP); return new String(encoded); }
From source file:conexionSiabra.Oauth.java
private static String hmac_sha1(String value, String key) { try {//from www . jav a 2 s . c om SecretKey secretKey = null; byte[] keyBytes = key.getBytes(); secretKey = new SecretKeySpec(keyBytes, "HmacSHA1"); Mac mac = Mac.getInstance("HmacSHA1"); mac.init(secretKey); byte[] text = value.getBytes(); return new String(Base64.encode(mac.doFinal(text), 0)).trim(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (InvalidKeyException e) { e.printStackTrace(); } return null; }
From source file:im.whistle.crypt.Crypt.java
/** * Encrypts a message./*from w ww . j av a2 s .co m*/ * @param args Arguments: data, publicKey[, privateKey] * @param callback Callback */ public static void encrypt(JSONArray args, AsyncCallback<JSONArray> callback) { try { PRNGProvider.init(); // Ensure OpenSSL fix // Get the arguments String data = args.getString(0); String pub = args.getString(1); String priv = null; if (args.length() == 3) { priv = args.getString(2); } String sig = null; // Convert everything into byte arrays byte[] dataRaw = data.getBytes("utf-8"); byte[] pubRaw = Base64.decode(stripKey(pub), Base64.DEFAULT); // Generate random AES key and IV byte[] aesKey = new byte[AES_BYTES]; new SecureRandom().nextBytes(aesKey); byte[] aesIv = new byte[16]; // Block size new SecureRandom().nextBytes(aesIv); Cipher c = Cipher.getInstance("AES/CBC/PKCS7Padding", "BC"); c.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(aesKey, "AES"), new IvParameterSpec(aesIv)); // Encrypt data with AES byte[] encData = c.doFinal(dataRaw); // Encrypt aes data with RSA X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(pubRaw); KeyFactory kf = KeyFactory.getInstance("RSA", "BC"); c = Cipher.getInstance("RSA/None/OAEPWithSHA-1AndMGF1Padding", "BC"); c.init(Cipher.ENCRYPT_MODE, kf.generatePublic(publicKeySpec)); c.update(aesKey); c.update(aesIv); byte[] encKey = c.doFinal(); // Concatenate and transform byte[] encRaw = new byte[encKey.length + encData.length]; System.arraycopy(encKey, 0, encRaw, 0, encKey.length); System.arraycopy(encData, 0, encRaw, encKey.length, encData.length); encKey = null; encData = null; String enc = new String(Base64.encode(encRaw /* needed for sign */, Base64.NO_WRAP), "utf-8"); // Sign if (priv != null) { // Fail on error (no try-catch) byte[] privRaw = Base64.decode(stripKey(priv), Base64.DEFAULT); PKCS8EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(privRaw); Signature s = Signature.getInstance("SHA1withRSA", "BC"); s.initSign(kf.generatePrivate(privateKeySpec)); s.update(encRaw); sig = new String(Base64.encode(s.sign(), Base64.NO_WRAP), "utf-8"); } JSONArray res = new JSONArray(); res.put(enc); res.put(sig); callback.success(res); } catch (Exception ex) { Log.w("whistle", "Encrypt error: " + ex.getMessage(), ex); callback.error(ex); } }
From source file:roommateapp.info.net.FileDownloader.java
/** * Parallel execution//from ww w .jav a 2 s . c o m */ @SuppressLint("UseValueOf") @Override protected Object doInBackground(Object... params) { if (!this.isHolidayFile) { /* Progressbar einblenden */ this.ac.runOnUiThread(new Runnable() { public void run() { toggleProgressbar(); } }); } boolean success = true; String eingabe = (String) params[0]; if (eingabe != null) { try { URL url = new URL(eingabe); // Get filename String fileName = eingabe; StringTokenizer tokenizer = new StringTokenizer(fileName, "/", false); int tokens = tokenizer.countTokens(); for (int i = 0; i < tokens; i++) { fileName = tokenizer.nextToken(); } // Create file this.downloadedFile = new File(this.roommateDirectory, fileName); // Download and write file try { // Password and username if it's HTACCESS String authData = new String(Base64.encode((username + ":" + pw).getBytes(), Base64.NO_WRAP)); URLConnection urlcon = url.openConnection(); // Authorisation if it's a userfile if (eingabe.startsWith(RoommateConfig.URL)) { urlcon.setRequestProperty("Authorization", "Basic " + authData); urlcon.setDoInput(true); } InputStream inputstream = urlcon.getInputStream(); ByteArrayBuffer baf = new ByteArrayBuffer(50); int current = 0; while ((current = inputstream.read()) != -1) { baf.append((byte) current); } FileOutputStream fos = new FileOutputStream(this.downloadedFile); fos.write(baf.toByteArray()); fos.close(); } catch (IOException e) { success = false; e.printStackTrace(); } } catch (MalformedURLException e) { success = false; e.printStackTrace(); } } else { success = false; } return new Boolean(success); }
From source file:com.google.samples.apps.abelana.AbelanaThings.java
private static String signData(String data) { try {//w w w . j av a 2 s . com if (signer == null) { signer = Signature.getInstance("SHA256withRSA"); signer.initSign(credential.getServiceAccountPrivateKey()); } signer.update(data.getBytes("UTF-8")); byte[] rawSignature = signer.sign(); return new String(Base64.encode(rawSignature, Base64.DEFAULT)); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (InvalidKeyException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (SignatureException e) { e.printStackTrace(); } return null; }
From source file:gowtham.com.desknote.MyListener.java
private String bitmap2Base64(Bitmap b) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); //Bitmap gray = toGrayscale(b); // Windows needs atleast 48x48 image, otherwise, notification is not shown Bitmap smaller = b.createScaledBitmap(b, 48, 48, false); // PNG is lossless. So, quality setting is unused smaller.compress(Bitmap.CompressFormat.PNG, 100, baos); byte[] buf = baos.toByteArray(); return new String(Base64.encode(buf, Base64.NO_WRAP)); }