Android Base64 Encode encryptToBase64Text(String key, String src)

Here you can find the source of encryptToBase64Text(String key, String src)

Description

encrypt To Base Text

Declaration

public static String encryptToBase64Text(String key, String src)
            throws Exception 

Method Source Code

//package com.java2s;

import javax.crypto.Cipher;

import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import android.util.Base64;

public class Main {
    private final static String encoding = "utf-8";
    private static byte[] iv = { 1, 2, 3, 4, 5, 6, 7, 8 };

    public static String encryptToBase64Text(String key, String src)
            throws Exception {
        byte[] result = encryptToBytes(key, src);
        return new String(Base64.encode(result, Base64.DEFAULT));
    }//w ww . ja  v a 2s .  c  o  m

    public static byte[] encryptToBytes(String key, String src)
            throws Exception {
        return encrypt(key.getBytes(), src.getBytes(encoding));
    }

    private static byte[] encrypt(byte[] key, byte[] src) throws Exception {
        IvParameterSpec zeroIv = new IvParameterSpec(iv);
        SecretKeySpec spec = new SecretKeySpec(key, "DES");
        Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, spec, zeroIv);
        return cipher.doFinal(src);
    }
}

Related

  1. encode(byte[] src)
  2. to64(String a)
  3. toBase64(byte[] barray)
  4. encodeBase64(byte abyte0[])
  5. encodeBase64(String input)
  6. generateBase64String(String inString)
  7. ImageBase64Decode(String base64_str)
  8. longKeyId2Base64String(long keyId)
  9. convertToBase64(byte[] in)