Here you can find the source of generateKeyDigest(int keyLength, String password)
public static byte[] generateKeyDigest(int keyLength, String password)
//package com.java2s; /*/*from ww w . j a v a 2s.c om*/ * Copyright 2017 ZhangJiupeng * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import java.io.UnsupportedEncodingException; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.util.Arrays; public class Main { public static byte[] generateKeyDigest(int keyLength, String password) { try { MessageDigest digester = MessageDigest.getInstance("MD5"); int length = (keyLength + 15) / 16 * 16; byte[] passwordBytes = password.getBytes("UTF-8"); byte[] temp = digester.digest(passwordBytes); byte[] key = Arrays.copyOf(temp, length); for (int i = 1; i < length / 16; i++) { temp = Arrays.copyOf(temp, 16 + passwordBytes.length); System.arraycopy(passwordBytes, 0, temp, 16, passwordBytes.length); System.arraycopy(digester.digest(temp), 0, key, i * 16, 16); } return Arrays.copyOf(key, keyLength); } catch (NoSuchAlgorithmException | UnsupportedEncodingException e) { e.printStackTrace(); } return new byte[keyLength]; } public static byte[] getBytes(int length, int value) { if (length < 1 || length > 4) throw new RuntimeException("bad length"); byte[] bytes = new byte[4]; bytes[0] = (byte) ((value >> 24) & 0xff); bytes[1] = (byte) ((value >> 16) & 0xff); bytes[2] = (byte) ((value >> 8) & 0xff); bytes[3] = (byte) (value & 0xff); if (length == 4) return bytes; else return Arrays.copyOfRange(bytes, 4 - length, 4); } public static byte[] getBytes(int value) { return getBytes(4, value); } }