Java Key Create getKey(String keyString)

Here you can find the source of getKey(String keyString)

Description

get Key

License

Apache License

Declaration

private static Key getKey(String keyString) throws IOException, GeneralSecurityException 

Method Source Code


//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;
    }
}

Related

  1. getKey(int size)
  2. getKey(KeyStore keystore, String alias, String password)
  3. getKey(KeyStore keyStore, String password, String orgName)
  4. getKey(KeyStore ks, String alias, String pass)
  5. getKey(Object obj)
  6. getKey(String keyString)
  7. getKey(String salt, String password)
  8. getKey(String siteSecret)
  9. getKeyedDigest(String strSrc, String key)