Package javax.crypto.spec

Examples of javax.crypto.spec.DESKeySpec


            KeySpec keySpec)
        throws InvalidKeySpecException
        {
            if (keySpec instanceof DESKeySpec)
            {
                DESKeySpec desKeySpec = (DESKeySpec)keySpec;
                return new SecretKeySpec(desKeySpec.getKey(), "DES");
            }

            return super.engineGenerateSecret(keySpec);
        }
View Full Code Here


   public void setEncryptionKey(String encryptionkey) {
      this.encryptionKey = encryptionkey;
      try {
         SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(encryptionAlgorithm);
         byte[] desKeyData = encryptionkey.getBytes();
         DESKeySpec desKeySpec = new DESKeySpec(desKeyData);
         secretKey = keyFactory.generateSecret(desKeySpec);
      } catch (Exception e) {
         System.out.println("Error: " + e);
         e.printStackTrace();
      }
View Full Code Here

    public static void main(String[] args) throws Exception {

        boolean failed = false;

        for (int i = 0; i < weakKeys.length; i++) {
            DESKeySpec desSpec = new DESKeySpec(weakKeys[i]);
            if (!DESKeySpec.isWeak(weakKeys[i], 0)) {
                failed = true;
                System.out.println("Entry " + i + " should be weak");
            }
        }
View Full Code Here

public class NullKey {

    public static void main(String[] args) throws Exception {

        try {
            DESKeySpec desSpec = new DESKeySpec(null);
            throw new Exception("expected NullPointerException");
        } catch (NullPointerException npe) {}

        try {
            DESKeySpec desSpec = new DESKeySpec(null, 0);
            throw new Exception("expected NullPointerException");
        } catch (NullPointerException npe) {}

        try {
            boolean parityAdjusted = DESKeySpec.isParityAdjusted(null, 0);
View Full Code Here

     * Returns multitaskers for the ciphers. :-)
     */
    private static class Synergizer {
        private static SecretKey getDESMultitasker() throws Exception {
            SecretKeyFactory factory = SecretKeyFactory.getInstance("DES");
            return factory.generateSecret(new DESKeySpec(Helper.buildBytesFromHexString("E60B80C7AEC78038")));
        }
View Full Code Here

     * @param key
     * @throws Exception
     */
    @Override
    public void initKey(String key) throws Exception {
        DESKeySpec dks = new DESKeySpec(Base64Util.base64toByte(key));
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ALGORITHM);
      this.key = keyFactory.generateSecret(dks);
    }
View Full Code Here

   * @param key byte类型密钥
   * @return key 密钥
   * @throws Exception
   */
  private Key toKey(byte[] key)throws Exception{
    DESKeySpec dks = new DESKeySpec(key);
    SecretKeyFactory skf = SecretKeyFactory.getInstance(KEY_ALGORITHM);
    //生成密钥
    SecretKey secretKey = skf.generateSecret(dks);
    return secretKey;
  }
View Full Code Here

  public byte[] initkey(String key)throws Exception{
    if(key == null || key.getBytes().length < 8) {
      throw new Exception("key不合法, 长度必须大于8个字节!");
    }
    byte[] bufKey = key.getBytes(CHAR_ENCODING);   
    DESKeySpec dks = new DESKeySpec(bufKey);
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(KEY_ALGORITHM);
    SecretKey securekey = keyFactory.generateSecret(dks);
    return securekey.getEncoded();
  }
View Full Code Here

   * @return byte 密文byte类型(IV length must be 8 bytes long)
   * @throws Exception
   */
  private byte[] encryptNet(byte[] data, byte[] key) throws Exception {
    Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM_CBC);
    DESKeySpec desKeySpec = new DESKeySpec(key);
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(KEY_ALGORITHM);
      SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
      IvParameterSpec iv = new IvParameterSpec(key);
      cipher.init(Cipher.ENCRYPT_MODE, secretKey, iv);
      return cipher.doFinal(data);
View Full Code Here

   * @return 原文byte类型
   * @throws Exception
   */
  private byte[] decryptNet(byte[] data, byte[] key) throws Exception {
    Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM_CBC);
    DESKeySpec desKeySpec = new DESKeySpec(key);
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(KEY_ALGORITHM);
      SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
      IvParameterSpec iv = new IvParameterSpec(key);
      cipher.init(Cipher.DECRYPT_MODE,secretKey, iv);
      return cipher.doFinal(data);
View Full Code Here

TOP

Related Classes of javax.crypto.spec.DESKeySpec

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.