Java tutorial
/* * Copyright 2002-2012 the original author or authors. * * 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. */ package io.manasobi.utils; import javax.crypto.Cipher; import javax.crypto.KeyGenerator; import javax.crypto.SecretKey; import javax.crypto.SecretKeyFactory; import javax.crypto.spec.DESedeKeySpec; import javax.crypto.spec.SecretKeySpec; import org.apache.commons.codec.binary.Hex; import io.manasobi.exception.CryptoUtilsException; /** * DES?? ? / Utils. * * @author manasobi * @since 1.0.0 */ public final class CryptoUtils { private CryptoUtils() { } /** ? ? - TripleDES */ private static final String ALGORITHM = "DESede"; /** * ? ?. ?? ? ?? ? ?. * * @return ?? ?? Hex ? */ public static String generateHexDESKey() { byte[] rawKey = null; try { KeyGenerator keyGenerator = KeyGenerator.getInstance(ALGORITHM); SecretKey secretKey = keyGenerator.generateKey(); SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance(ALGORITHM); DESedeKeySpec desEdeSpec = (DESedeKeySpec) secretKeyFactory.getKeySpec(secretKey, javax.crypto.spec.DESedeKeySpec.class); rawKey = desEdeSpec.getKey(); } catch (Exception e) { throw new CryptoUtilsException(e.getMessage()); } return new String(Hex.encodeHex(rawKey)); } /** * . * * @param keyHex generateHexKey ? ? ?? Hex ? ? * @param data ?? byte * * @return ? ?? */ public static byte[] encryptByDES(String keyHex, byte[] data) { SecretKey key = getSecretDESKeyFromHex(keyHex); byte[] encryptedData = null; try { Cipher cipher = Cipher.getInstance(ALGORITHM); cipher.init(Cipher.ENCRYPT_MODE, key); encryptedData = cipher.doFinal(data); } catch (Exception e) { throw new CryptoUtilsException(e.getMessage()); } return encryptedData; } /** * ? ?? ? ? . * * @param keyHex generateHexKey ? ? ?? Hex ? ? * @param data ?? ? * @param encoding ? ? * * @return ? ?? ? */ public static String encryptByDES(String keyHex, String data, String encoding) { String encryptedString = null; try { byte[] encryptedData = encryptByDES(keyHex, data.getBytes(encoding)); encryptedString = new String(Hex.encodeHex(encryptedData)); } catch (Exception e) { throw new CryptoUtilsException(e.getMessage()); } return encryptedString; } /** * . * * @param keyHex generateHexKey ? ? ?? Hex ? ? * @param data ? ? * * @return ? ?? byte */ public static byte[] decryptByDES(String keyHex, byte[] data) { SecretKey key = getSecretDESKeyFromHex(keyHex); byte[] decryptedData = null; try { Cipher cipher = Cipher.getInstance(ALGORITHM); cipher.init(Cipher.DECRYPT_MODE, key); decryptedData = cipher.doFinal(data); } catch (Exception e) { throw new CryptoUtilsException(e.getMessage()); } return decryptedData; } /** * ? ?? , ? ?? . * * @param keyHex generateHexKey ? ? ?? Hex ? ? * @param data ? ? * @param encoding ? ? * * @return ? ? */ public static String decryptByDES(String keyHex, String data, String encoding) { String decryptedString = null; try { byte[] unhexedData = Hex.decodeHex(data.toCharArray()); byte[] decryptedData = decryptByDES(keyHex, unhexedData); decryptedString = new String(decryptedData, encoding); } catch (Exception e) { throw new CryptoUtilsException(e.getMessage()); } return decryptedString; } /** * hex ?? ? SecretKey ? ?. * * @param keyHex generateHexKey ? ? ?? Hex ? ? * * @return ?? SecretKey ? */ private static SecretKey getSecretDESKeyFromHex(String keyHex) { SecretKey key = null; try { byte[] keyBytes = Hex.decodeHex(keyHex.toCharArray()); key = new SecretKeySpec(keyBytes, ALGORITHM); } catch (Exception e) { throw new CryptoUtilsException(e.getMessage()); } return key; } }