Android AES Key Get getKeyBytes(String key)

Here you can find the source of getKeyBytes(String key)

Description

convert key from string to 256-bits (32-bytes) bytes

Declaration

public static byte[] getKeyBytes(String key)
        throws NoSuchAlgorithmException 

Method Source Code

//package com.java2s;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class Main {
    /**//www.j a  v a 2  s . co  m
     * convert key from string to 256-bits (32-bytes) bytes
     */
    public static byte[] getKeyBytes(String key)
            throws NoSuchAlgorithmException {
        MessageDigest md5 = MessageDigest.getInstance("md5");
        byte[] k1 = md5.digest(key.substring(0, key.length() / 2)
                .getBytes());
        byte[] k2 = md5.digest(key.substring(key.length() / 2).getBytes());
        byte[] k = new byte[32];
        for (int i = 0; i < 16; i++) {
            k[i] = k1[i];
            k[16 + i] = k2[i];
        }
        return k;
    }
}

Related

  1. createKey(String password)
  2. getRawKey(String key)
  3. getRawKey(byte[] seed)
  4. getRawKey(byte[] seed)
  5. getRawKey(byte[] seed)