Here you can find the source of getKey(String keyString)
private static Key getKey(String keyString) throws IOException, GeneralSecurityException
//package com.java2s; /*/*from w ww .j ava2 s . com*/ * Copyright 2009 Joseph Fifield * * 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.IOException; import java.security.GeneralSecurityException; import java.security.Key; import javax.crypto.SecretKey; import javax.crypto.SecretKeyFactory; import javax.crypto.spec.DESKeySpec; public class Main { private static final String ALGORITHM = "DES"; private static Key getKey(String keyString) throws IOException, GeneralSecurityException { byte[] bytes = hexStringToByteArray(keyString); DESKeySpec keySpec = new DESKeySpec(bytes); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ALGORITHM); SecretKey key = keyFactory.generateSecret(keySpec); return key; } private static byte[] hexStringToByteArray(String str) throws IOException { byte[] bytes = new byte[str.length() / 2]; for (int i = 0; i < bytes.length; i++) { int index = i * 2; String s = str.substring(index, index + 2); int value = Integer.parseInt(s, 16); bytes[i] = (byte) value; } return bytes; } }