encode AES Key - Android java.security

Android examples for java.security:AES

Description

encode AES Key

Demo Code


//package com.java2s;
import android.util.Base64;
import javax.crypto.Cipher;

import javax.crypto.spec.SecretKeySpec;
import java.io.*;

public class Main {
    public static String encodeKey(String key) throws Exception {
        SecretKeySpec skeySpec = new SecretKeySpec(getCryptoStringKey(),
                "AES");
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
        byte[] keyBytes = key.getBytes("UTF-8");
        return Base64.encodeToString(cipher.doFinal(keyBytes),
                Base64.DEFAULT);/*www . j  a  v  a2s . c o  m*/
    }

    private static byte[] getCryptoStringKey() {
        //  byte[] key = new byte[16];
        try {
            return ("d12Vp54R4sb0ymVF").getBytes("UTF-8");
        } catch (UnsupportedEncodingException x) {
            throw new RuntimeException(x);
        }
    }
}

Related Tutorials