Java 类javax.crypto.spec.DESedeKeySpec 实例源码

项目:alfresco-core    文件:AlfrescoKeyStoreImpl.java   
protected Key getSecretKey(KeyInformation keyInformation) throws NoSuchAlgorithmException, InvalidKeyException, InvalidKeySpecException
{
    byte[] keyData = keyInformation.getKeyData();

    if(keyData == null)
    {
        if(keyInformation.getKeyAlgorithm().equals("DESede"))
        {
            // no key data provided, generate key data automatically
            keyData = generateKeyData();
        }
        else
        {
            throw new AlfrescoRuntimeException("Unable to generate secret key: key algorithm is not DESede and no keyData provided");
        }
    }

    DESedeKeySpec keySpec = new DESedeKeySpec(keyData);
    SecretKeyFactory kf = SecretKeyFactory.getInstance(keyInformation.getKeyAlgorithm());
    SecretKey secretKey = kf.generateSecret(keySpec);
    return secretKey;
}
项目:OpenJSharp    文件:Des3DkCrypto.java   
protected Cipher getCipher(byte[] key, byte[] ivec, int mode)
    throws GeneralSecurityException {
    // NoSuchAlgorithException
    SecretKeyFactory factory = SecretKeyFactory.getInstance("desede");

    // InvalidKeyException
    KeySpec spec = new DESedeKeySpec(key, 0);

    // InvalidKeySpecException
    SecretKey secretKey = factory.generateSecret(spec);

    // IV
    if (ivec == null) {
        ivec = ZERO_IV;
    }

    // NoSuchAlgorithmException, NoSuchPaddingException
    // NoSuchProviderException
    Cipher cipher = Cipher.getInstance("DESede/CBC/NoPadding");
    IvParameterSpec encIv = new IvParameterSpec(ivec, 0, ivec.length);

    // InvalidKeyException, InvalidAlgorithParameterException
    cipher.init(mode, secretKey, encIv);

    return cipher;
}
项目:OpenJSharp    文件:DESedeKeyFactory.java   
/**
 * Generates a <code>SecretKey</code> object from the provided key
 * specification (key material).
 *
 * @param keySpec the specification (key material) of the secret key
 *
 * @return the secret key
 *
 * @exception InvalidKeySpecException if the given key specification
 * is inappropriate for this key factory to produce a public key.
 */
protected SecretKey engineGenerateSecret(KeySpec keySpec)
    throws InvalidKeySpecException {

    try {
        if (keySpec instanceof DESedeKeySpec) {
            return new DESedeKey(((DESedeKeySpec)keySpec).getKey());
        }
        if (keySpec instanceof SecretKeySpec) {
            return new DESedeKey(((SecretKeySpec)keySpec).getEncoded());

        }
        throw new InvalidKeySpecException
            ("Inappropriate key specification");
    } catch (InvalidKeyException e) {
        throw new InvalidKeySpecException(e.getMessage());
    }
}
项目:OpenJSharp    文件:DESedeKeyFactory.java   
/**
 * Returns a specification (key material) of the given key
 * in the requested format.
 *
 * @param key the key
 *
 * @param keySpec the requested format in which the key material shall be
 * returned
 *
 * @return the underlying key specification (key material) in the
 * requested format
 *
 * @exception InvalidKeySpecException if the requested key specification is
 * inappropriate for the given key, or the given key cannot be processed
 * (e.g., the given key has an unrecognized algorithm or format).
 */
protected KeySpec engineGetKeySpec(SecretKey key, Class<?> keySpec)
    throws InvalidKeySpecException {

    try {
        if ((key instanceof SecretKey)
            && (key.getAlgorithm().equalsIgnoreCase("DESede"))
            && (key.getFormat().equalsIgnoreCase("RAW"))) {

            // Check if requested key spec is amongst the valid ones
            if (DESedeKeySpec.class.isAssignableFrom(keySpec)) {
                return new DESedeKeySpec(key.getEncoded());

            } else {
                throw new InvalidKeySpecException
                    ("Inappropriate key specification");
            }

        } else {
            throw new InvalidKeySpecException
                ("Inappropriate key format/algorithm");
        }
    } catch (InvalidKeyException e) {
        throw new InvalidKeySpecException("Secret key has wrong size");
    }
}
项目:ITSM    文件:Endecrypt.java   
/**
 * 3-DES加密
 *
 * @param byte[]
 *          src 要进行3-DES加密的byte[]
 * @param byte[]
 *          enKey 3-DES加密密钥
 * @return byte[] 3-DES加密后的byte[]
 */

public byte[] Encrypt(byte[] src, byte[] enKey) {
  byte[] encryptedData = null;
  try {
    DESedeKeySpec dks = new DESedeKeySpec(enKey);
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DESede");
    SecretKey key = keyFactory.generateSecret(dks);
    Cipher cipher = Cipher.getInstance("DESede");
    cipher.init(Cipher.ENCRYPT_MODE, key);
    encryptedData = cipher.doFinal(src);
  } catch (Exception e) {
    e.printStackTrace();
  }
  return encryptedData;
}
项目:ITSM    文件:Endecrypt.java   
/**
 *
 * 进行3-DES解密(密钥匙等同于加密的密钥匙)。
 *
 * @param byte[]
 *          src 要进行3-DES解密byte[]
 * @param String
 *          spkey分配的SPKEY
 * @return String 3-DES解密后的String
 */
public static String deCrypt(byte[] debase64, String spKey) {
  String strDe = null;
  Cipher cipher = null;
  try {
    cipher = Cipher.getInstance("DESede");
    byte[] key = getEnKey(spKey);
    DESedeKeySpec dks = new DESedeKeySpec(key);
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DESede");
    SecretKey sKey = keyFactory.generateSecret(dks);
    cipher.init(Cipher.DECRYPT_MODE, sKey);
    byte ciphertext[] = cipher.doFinal(debase64);
    strDe = new String(ciphertext, "UTF-16LE");
  } catch (Exception ex) {
    strDe = "";
    ex.printStackTrace();
  }
  return strDe;
}
项目:wolfcrypt-jni    文件:WolfCryptKeyAgreement.java   
@Override
protected SecretKey engineGenerateSecret(String algorithm)
    throws IllegalStateException, NoSuchAlgorithmException,
           InvalidKeyException {

    byte secret[] = engineGenerateSecret();

    if (debug.DEBUG)
        log("generating SecretKey for " + algorithm);

    if (algorithm.equals("DES")) {
        return (SecretKey)new DESKeySpec(secret);

    } else if (algorithm.equals("DESede")) {
        return (SecretKey)new DESedeKeySpec(secret);

    } else {
        /* AES and default */
        return new SecretKeySpec(secret, algorithm);
    }
}
项目:jdk8u-jdk    文件:Des3DkCrypto.java   
protected Cipher getCipher(byte[] key, byte[] ivec, int mode)
    throws GeneralSecurityException {
    // NoSuchAlgorithException
    SecretKeyFactory factory = SecretKeyFactory.getInstance("desede");

    // InvalidKeyException
    KeySpec spec = new DESedeKeySpec(key, 0);

    // InvalidKeySpecException
    SecretKey secretKey = factory.generateSecret(spec);

    // IV
    if (ivec == null) {
        ivec = ZERO_IV;
    }

    // NoSuchAlgorithmException, NoSuchPaddingException
    // NoSuchProviderException
    Cipher cipher = Cipher.getInstance("DESede/CBC/NoPadding");
    IvParameterSpec encIv = new IvParameterSpec(ivec, 0, ivec.length);

    // InvalidKeyException, InvalidAlgorithParameterException
    cipher.init(mode, secretKey, encIv);

    return cipher;
}
项目:jdk8u-jdk    文件:DESedeKeyFactory.java   
/**
 * Generates a <code>SecretKey</code> object from the provided key
 * specification (key material).
 *
 * @param keySpec the specification (key material) of the secret key
 *
 * @return the secret key
 *
 * @exception InvalidKeySpecException if the given key specification
 * is inappropriate for this key factory to produce a public key.
 */
protected SecretKey engineGenerateSecret(KeySpec keySpec)
    throws InvalidKeySpecException {

    try {
        if (keySpec instanceof DESedeKeySpec) {
            return new DESedeKey(((DESedeKeySpec)keySpec).getKey());
        }
        if (keySpec instanceof SecretKeySpec) {
            return new DESedeKey(((SecretKeySpec)keySpec).getEncoded());

        }
        throw new InvalidKeySpecException
            ("Inappropriate key specification");
    } catch (InvalidKeyException e) {
        throw new InvalidKeySpecException(e.getMessage());
    }
}
项目:jdk8u-jdk    文件:DESedeKeyFactory.java   
/**
 * Returns a specification (key material) of the given key
 * in the requested format.
 *
 * @param key the key
 *
 * @param keySpec the requested format in which the key material shall be
 * returned
 *
 * @return the underlying key specification (key material) in the
 * requested format
 *
 * @exception InvalidKeySpecException if the requested key specification is
 * inappropriate for the given key, or the given key cannot be processed
 * (e.g., the given key has an unrecognized algorithm or format).
 */
protected KeySpec engineGetKeySpec(SecretKey key, Class<?> keySpec)
    throws InvalidKeySpecException {

    try {
        if ((key instanceof SecretKey)
            && (key.getAlgorithm().equalsIgnoreCase("DESede"))
            && (key.getFormat().equalsIgnoreCase("RAW"))) {

            // Check if requested key spec is amongst the valid ones
            if (DESedeKeySpec.class.isAssignableFrom(keySpec)) {
                return new DESedeKeySpec(key.getEncoded());

            } else {
                throw new InvalidKeySpecException
                    ("Inappropriate key specification");
            }

        } else {
            throw new InvalidKeySpecException
                ("Inappropriate key format/algorithm");
        }
    } catch (InvalidKeyException e) {
        throw new InvalidKeySpecException("Secret key has wrong size");
    }
}
项目:openjdk-jdk10    文件:DESedeKey.java   
/**
 * Uses the first 24 bytes in <code>key</code>, beginning at
 * <code>offset</code>, as the DES-EDE key
 *
 * @param key the buffer with the DES-EDE key
 * @param offset the offset in <code>key</code>, where the DES-EDE key
 * starts
 *
 * @exception InvalidKeyException if the given key has a wrong size
 */
DESedeKey(byte[] key, int offset) throws InvalidKeyException {

    if (key==null || ((key.length-offset)<DESedeKeySpec.DES_EDE_KEY_LEN)) {
        throw new InvalidKeyException("Wrong key size");
    }
    this.key = new byte[DESedeKeySpec.DES_EDE_KEY_LEN];
    System.arraycopy(key, offset, this.key, 0,
                     DESedeKeySpec.DES_EDE_KEY_LEN);
    DESKeyGenerator.setParityBit(this.key, 0);
    DESKeyGenerator.setParityBit(this.key, 8);
    DESKeyGenerator.setParityBit(this.key, 16);

    // Use the cleaner to zero the key when no longer referenced
    final byte[] k = this.key;
    CleanerFactory.cleaner().register(this,
            () -> java.util.Arrays.fill(k, (byte)0x00));
}
项目:openjdk-jdk10    文件:DESedeKeyFactory.java   
/**
 * Generates a <code>SecretKey</code> object from the provided key
 * specification (key material).
 *
 * @param keySpec the specification (key material) of the secret key
 *
 * @return the secret key
 *
 * @exception InvalidKeySpecException if the given key specification
 * is inappropriate for this key factory to produce a public key.
 */
protected SecretKey engineGenerateSecret(KeySpec keySpec)
    throws InvalidKeySpecException {

    try {
        if (keySpec instanceof DESedeKeySpec) {
            return new DESedeKey(((DESedeKeySpec)keySpec).getKey());
        }
        if (keySpec instanceof SecretKeySpec) {
            return new DESedeKey(((SecretKeySpec)keySpec).getEncoded());

        }
        throw new InvalidKeySpecException
            ("Inappropriate key specification");
    } catch (InvalidKeyException e) {
        throw new InvalidKeySpecException(e.getMessage());
    }
}
项目:openjdk-jdk10    文件:DESedeKeyFactory.java   
/**
 * Returns a specification (key material) of the given key
 * in the requested format.
 *
 * @param key the key
 *
 * @param keySpec the requested format in which the key material shall be
 * returned
 *
 * @return the underlying key specification (key material) in the
 * requested format
 *
 * @exception InvalidKeySpecException if the requested key specification is
 * inappropriate for the given key, or the given key cannot be processed
 * (e.g., the given key has an unrecognized algorithm or format).
 */
protected KeySpec engineGetKeySpec(SecretKey key, Class<?> keySpec)
    throws InvalidKeySpecException {

    try {
        if ((key instanceof SecretKey)
            && (key.getAlgorithm().equalsIgnoreCase("DESede"))
            && (key.getFormat().equalsIgnoreCase("RAW"))) {

            // Check if requested key spec is amongst the valid ones
            if (DESedeKeySpec.class.isAssignableFrom(keySpec)) {
                return new DESedeKeySpec(key.getEncoded());

            } else {
                throw new InvalidKeySpecException
                    ("Inappropriate key specification");
            }

        } else {
            throw new InvalidKeySpecException
                ("Inappropriate key format/algorithm");
        }
    } catch (InvalidKeyException e) {
        throw new InvalidKeySpecException("Secret key has wrong size");
    }
}
项目:openjdk-jdk10    文件:Des3DkCrypto.java   
protected Cipher getCipher(byte[] key, byte[] ivec, int mode)
    throws GeneralSecurityException {
    // NoSuchAlgorithException
    SecretKeyFactory factory = SecretKeyFactory.getInstance("desede");

    // InvalidKeyException
    KeySpec spec = new DESedeKeySpec(key, 0);

    // InvalidKeySpecException
    SecretKey secretKey = factory.generateSecret(spec);

    // IV
    if (ivec == null) {
        ivec = ZERO_IV;
    }

    // NoSuchAlgorithmException, NoSuchPaddingException
    // NoSuchProviderException
    Cipher cipher = Cipher.getInstance("DESede/CBC/NoPadding");
    IvParameterSpec encIv = new IvParameterSpec(ivec, 0, ivec.length);

    // InvalidKeyException, InvalidAlgorithParameterException
    cipher.init(mode, secretKey, encIv);

    return cipher;
}
项目:Mevius-IO    文件:MeviusTransferPacket.java   
public static MeviusTransferPacket getInstance(PublicKey publickey, MeviusPacket packet)
        throws MeviusCipherException {
    try {
        DESedeKeySpec desKeySpec = new DESedeKeySpec(((String) MeviusCipherKey.randomDESKey().getKey()).getBytes());
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DESede");
        Key key = keyFactory.generateSecret(desKeySpec);
        Cipher c = Cipher.getInstance("RSA/ECB/PKCS1PADDING", "SunJCE");
        c.init(Cipher.ENCRYPT_MODE, publickey);
        byte[] bkey = convertObj(key, c);
        c = Cipher.getInstance("DESede/ECB/PKCS5Padding");
        c.init(Cipher.ENCRYPT_MODE, key);
        byte[] bobj = convertObj(packet, c);
        return new MeviusTransferPacket(bkey, bobj);
    } catch (InvalidKeyException | NoSuchAlgorithmException | NoSuchProviderException | NoSuchPaddingException
            | IOException | InvalidKeySpecException | IllegalBlockSizeException | BadPaddingException e) {
        e.printStackTrace();
        throw new MeviusCipherException(e.getLocalizedMessage());
    }
}
项目:openjdk9    文件:DESedeKeyFactory.java   
/**
 * Generates a <code>SecretKey</code> object from the provided key
 * specification (key material).
 *
 * @param keySpec the specification (key material) of the secret key
 *
 * @return the secret key
 *
 * @exception InvalidKeySpecException if the given key specification
 * is inappropriate for this key factory to produce a public key.
 */
protected SecretKey engineGenerateSecret(KeySpec keySpec)
    throws InvalidKeySpecException {

    try {
        if (keySpec instanceof DESedeKeySpec) {
            return new DESedeKey(((DESedeKeySpec)keySpec).getKey());
        }
        if (keySpec instanceof SecretKeySpec) {
            return new DESedeKey(((SecretKeySpec)keySpec).getEncoded());

        }
        throw new InvalidKeySpecException
            ("Inappropriate key specification");
    } catch (InvalidKeyException e) {
        throw new InvalidKeySpecException(e.getMessage());
    }
}
项目:openjdk9    文件:DESedeKeyFactory.java   
/**
 * Returns a specification (key material) of the given key
 * in the requested format.
 *
 * @param key the key
 *
 * @param keySpec the requested format in which the key material shall be
 * returned
 *
 * @return the underlying key specification (key material) in the
 * requested format
 *
 * @exception InvalidKeySpecException if the requested key specification is
 * inappropriate for the given key, or the given key cannot be processed
 * (e.g., the given key has an unrecognized algorithm or format).
 */
protected KeySpec engineGetKeySpec(SecretKey key, Class<?> keySpec)
    throws InvalidKeySpecException {

    try {
        if ((key instanceof SecretKey)
            && (key.getAlgorithm().equalsIgnoreCase("DESede"))
            && (key.getFormat().equalsIgnoreCase("RAW"))) {

            // Check if requested key spec is amongst the valid ones
            if (DESedeKeySpec.class.isAssignableFrom(keySpec)) {
                return new DESedeKeySpec(key.getEncoded());

            } else {
                throw new InvalidKeySpecException
                    ("Inappropriate key specification");
            }

        } else {
            throw new InvalidKeySpecException
                ("Inappropriate key format/algorithm");
        }
    } catch (InvalidKeyException e) {
        throw new InvalidKeySpecException("Secret key has wrong size");
    }
}
项目:openjdk9    文件:Des3DkCrypto.java   
protected Cipher getCipher(byte[] key, byte[] ivec, int mode)
    throws GeneralSecurityException {
    // NoSuchAlgorithException
    SecretKeyFactory factory = SecretKeyFactory.getInstance("desede");

    // InvalidKeyException
    KeySpec spec = new DESedeKeySpec(key, 0);

    // InvalidKeySpecException
    SecretKey secretKey = factory.generateSecret(spec);

    // IV
    if (ivec == null) {
        ivec = ZERO_IV;
    }

    // NoSuchAlgorithmException, NoSuchPaddingException
    // NoSuchProviderException
    Cipher cipher = Cipher.getInstance("DESede/CBC/NoPadding");
    IvParameterSpec encIv = new IvParameterSpec(ivec, 0, ivec.length);

    // InvalidKeyException, InvalidAlgorithParameterException
    cipher.init(mode, secretKey, encIv);

    return cipher;
}
项目:JerseyRestful    文件:Des3.java   
/**
 * ECB加密,不要IV
 *
 * @param key  密钥
 * @param data 明文
 * @return Base64编码的密文
 * @throws Exception
 */
public static byte[] des3EncodeECB(byte[] key, byte[] data)
        throws Exception {

    SecretKey deskey = null;
    DESedeKeySpec spec = new DESedeKeySpec(key);
    SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("desede");
    deskey = keyfactory.generateSecret(spec);

    Cipher cipher = Cipher.getInstance("desede" + "/ECB/PKCS5Padding");

    cipher.init(Cipher.ENCRYPT_MODE, deskey);
    byte[] bOut = cipher.doFinal(data);

    return bOut;
}
项目:JerseyRestful    文件:Des3.java   
/**
 * CBC加密
 *
 * @param key   密钥
 * @param keyiv IV
 * @param data  明文
 * @return Base64编码的密文
 * @throws Exception
 */
public static byte[] des3EncodeCBC(byte[] key, byte[] keyiv, byte[] data)
        throws Exception {

    SecretKey deskey = null;
    DESedeKeySpec spec = new DESedeKeySpec(key);
    SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("desede");
    deskey = keyfactory.generateSecret(spec);

    Cipher cipher = Cipher.getInstance("desede" + "/CBC/PKCS5Padding");
    IvParameterSpec ips = new IvParameterSpec(keyiv);
    cipher.init(Cipher.ENCRYPT_MODE, deskey, ips);
    byte[] bOut = cipher.doFinal(data);

    return bOut;
}
项目:conscrypt    文件:DESEDESecretKeyFactory.java   
@Override
protected SecretKey engineGenerateSecret(KeySpec keySpec) throws InvalidKeySpecException {
    if (keySpec == null) {
        throw new InvalidKeySpecException("Null KeySpec");
    }
    if (keySpec instanceof SecretKeySpec) {
        SecretKeySpec key = (SecretKeySpec) keySpec;
        try {
            if (!DESedeKeySpec.isParityAdjusted(key.getEncoded(), 0)) {
                throw new InvalidKeySpecException(
                        "SecretKeySpec is not a parity-adjusted DESEDE key");
            }
        } catch (InvalidKeyException e) {
            throw new InvalidKeySpecException(e);
        }
        return key;
    } else if (keySpec instanceof DESedeKeySpec) {
        DESedeKeySpec desKeySpec = (DESedeKeySpec) keySpec;
        return new SecretKeySpec(desKeySpec.getKey(), "DESEDE");
    } else {
        throw new InvalidKeySpecException(
                "Unsupported KeySpec class: " + keySpec.getClass().getName());
    }
}
项目:conscrypt    文件:KeyGeneratorImpl.java   
@Override
protected byte[] doKeyGeneration(int keyBytes) {
    byte[] keyData = new byte[DESedeKeySpec.DES_EDE_KEY_LEN];
    secureRandom.nextBytes(keyData);
    // Set the parity bit for each byte
    for (int i = 0; i < keyData.length; i++) {
        if (Integer.bitCount(keyData[i]) % 2 == 0) {
            keyData[i] = (byte) (keyData[i] ^ 1);
        }
    }
    if (keyBytes == 14) {
        // The user requested an A-B-A key
        System.arraycopy(keyData, 0, keyData, 16, 8);
    }
    return keyData;
}
项目:xmlsec-gost    文件:BobKeyResolver.java   
/**
 * Method engineResolveSecretKey
 *
 * @param element
 * @param BaseURI
 * @param storage
 *
 * @throws KeyResolverException
 */
public SecretKey engineLookupAndResolveSecretKey(
    Element element, String BaseURI, StorageResolver storage
) throws KeyResolverException {
    if (engineCanResolve(element, BaseURI, storage)) {
        try {
            DESedeKeySpec keySpec =
                new DESedeKeySpec("abcdefghijklmnopqrstuvwx".getBytes("ASCII"));
            SecretKeyFactory keyFactory =
                SecretKeyFactory.getInstance("DESede");
            SecretKey key = keyFactory.generateSecret(keySpec);

            return key;
        }
        catch (Exception e) {
            throw new KeyResolverException("Something badly wrong in creation of bob's key");
        }
    }

    return null;
}
项目:xmlsec-gost    文件:Decrypter.java   
private static SecretKey loadKeyEncryptionKey() throws Exception {
    String fileName = "build/kek";
    String jceAlgorithmName = "DESede";

    File kekFile = new File(fileName);

    DESedeKeySpec keySpec =
        new DESedeKeySpec(JavaUtils.getBytesFromFile(fileName));
    SecretKeyFactory skf =
         SecretKeyFactory.getInstance(jceAlgorithmName);
    SecretKey key = skf.generateSecret(keySpec);

    System.out.println(
        "Key encryption key loaded from " + kekFile.toURI().toURL().toString()
    );
    return key;
}
项目:esup-nfc-tag-server    文件:TripleDES.java   
/**
 * Encrypt using 3DES: DESede/CBC/NoPadding.
 * 
 * @param myIV  Initialization vector
 * @param myKey Secret key (24 Bytes)
 * @param myMsg Message to encrypt
 * @return      The encrypted message, or <code>null</code> on error.
 */
public static byte[] encrypt(byte[] myIV, byte[] myKey, byte[] myMsg) {
    byte[] cipherText = null;

    try {
        IvParameterSpec iv = new IvParameterSpec(myIV);
        DESedeKeySpec desKey = new DESedeKeySpec(myKey);
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DESede");
        SecretKey key = keyFactory.generateSecret(desKey);

        Cipher cipher = Cipher.getInstance("DESede/CBC/NoPadding");
        cipher.init(Cipher.ENCRYPT_MODE, key, iv);
        cipherText = cipher.doFinal(myMsg);
    } catch (Exception e) {
        //TODO: multicatch only Java 1.7+
        e.printStackTrace();
        return null;
    }

    return cipherText;
}
项目:esup-nfc-tag-server    文件:TripleDES.java   
public static byte[] decrypt(byte[] myIV, byte[] myKey, byte[] myMsg, int offset, int length) {
    byte[] plainText = null;

    try {
        IvParameterSpec iv = new IvParameterSpec(myIV);
        DESedeKeySpec desKey = new DESedeKeySpec(myKey);
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DESede");
        SecretKey key = keyFactory.generateSecret(desKey);

        Cipher cipher = Cipher.getInstance("DESede/CBC/NoPadding");
        cipher.init(Cipher.DECRYPT_MODE, key, iv);
        //plainText = cipher.doFinal(myMsg);
        plainText = cipher.doFinal(myMsg, offset, length);
    } catch (Exception e) {
        //TODO: multicatch only Java 1.7+
        e.printStackTrace();
    }

    return plainText;
}
项目:jdk8u_jdk    文件:Des3DkCrypto.java   
protected Cipher getCipher(byte[] key, byte[] ivec, int mode)
    throws GeneralSecurityException {
    // NoSuchAlgorithException
    SecretKeyFactory factory = SecretKeyFactory.getInstance("desede");

    // InvalidKeyException
    KeySpec spec = new DESedeKeySpec(key, 0);

    // InvalidKeySpecException
    SecretKey secretKey = factory.generateSecret(spec);

    // IV
    if (ivec == null) {
        ivec = ZERO_IV;
    }

    // NoSuchAlgorithmException, NoSuchPaddingException
    // NoSuchProviderException
    Cipher cipher = Cipher.getInstance("DESede/CBC/NoPadding");
    IvParameterSpec encIv = new IvParameterSpec(ivec, 0, ivec.length);

    // InvalidKeyException, InvalidAlgorithParameterException
    cipher.init(mode, secretKey, encIv);

    return cipher;
}
项目:jdk8u_jdk    文件:DESedeKeyFactory.java   
/**
 * Generates a <code>SecretKey</code> object from the provided key
 * specification (key material).
 *
 * @param keySpec the specification (key material) of the secret key
 *
 * @return the secret key
 *
 * @exception InvalidKeySpecException if the given key specification
 * is inappropriate for this key factory to produce a public key.
 */
protected SecretKey engineGenerateSecret(KeySpec keySpec)
    throws InvalidKeySpecException {

    try {
        if (keySpec instanceof DESedeKeySpec) {
            return new DESedeKey(((DESedeKeySpec)keySpec).getKey());
        }
        if (keySpec instanceof SecretKeySpec) {
            return new DESedeKey(((SecretKeySpec)keySpec).getEncoded());

        }
        throw new InvalidKeySpecException
            ("Inappropriate key specification");
    } catch (InvalidKeyException e) {
        throw new InvalidKeySpecException(e.getMessage());
    }
}
项目:jdk8u_jdk    文件:DESedeKeyFactory.java   
/**
 * Returns a specification (key material) of the given key
 * in the requested format.
 *
 * @param key the key
 *
 * @param keySpec the requested format in which the key material shall be
 * returned
 *
 * @return the underlying key specification (key material) in the
 * requested format
 *
 * @exception InvalidKeySpecException if the requested key specification is
 * inappropriate for the given key, or the given key cannot be processed
 * (e.g., the given key has an unrecognized algorithm or format).
 */
protected KeySpec engineGetKeySpec(SecretKey key, Class<?> keySpec)
    throws InvalidKeySpecException {

    try {
        if ((key instanceof SecretKey)
            && (key.getAlgorithm().equalsIgnoreCase("DESede"))
            && (key.getFormat().equalsIgnoreCase("RAW"))) {

            // Check if requested key spec is amongst the valid ones
            if (DESedeKeySpec.class.isAssignableFrom(keySpec)) {
                return new DESedeKeySpec(key.getEncoded());

            } else {
                throw new InvalidKeySpecException
                    ("Inappropriate key specification");
            }

        } else {
            throw new InvalidKeySpecException
                ("Inappropriate key format/algorithm");
        }
    } catch (InvalidKeyException e) {
        throw new InvalidKeySpecException("Secret key has wrong size");
    }
}
项目:lookaside_java-1.8.0-openjdk    文件:Des3DkCrypto.java   
protected Cipher getCipher(byte[] key, byte[] ivec, int mode)
    throws GeneralSecurityException {
    // NoSuchAlgorithException
    SecretKeyFactory factory = SecretKeyFactory.getInstance("desede");

    // InvalidKeyException
    KeySpec spec = new DESedeKeySpec(key, 0);

    // InvalidKeySpecException
    SecretKey secretKey = factory.generateSecret(spec);

    // IV
    if (ivec == null) {
        ivec = ZERO_IV;
    }

    // NoSuchAlgorithmException, NoSuchPaddingException
    // NoSuchProviderException
    Cipher cipher = Cipher.getInstance("DESede/CBC/NoPadding");
    IvParameterSpec encIv = new IvParameterSpec(ivec, 0, ivec.length);

    // InvalidKeyException, InvalidAlgorithParameterException
    cipher.init(mode, secretKey, encIv);

    return cipher;
}
项目:lookaside_java-1.8.0-openjdk    文件:DESedeKeyFactory.java   
/**
 * Generates a <code>SecretKey</code> object from the provided key
 * specification (key material).
 *
 * @param keySpec the specification (key material) of the secret key
 *
 * @return the secret key
 *
 * @exception InvalidKeySpecException if the given key specification
 * is inappropriate for this key factory to produce a public key.
 */
protected SecretKey engineGenerateSecret(KeySpec keySpec)
    throws InvalidKeySpecException {

    try {
        if (keySpec instanceof DESedeKeySpec) {
            return new DESedeKey(((DESedeKeySpec)keySpec).getKey());
        }
        if (keySpec instanceof SecretKeySpec) {
            return new DESedeKey(((SecretKeySpec)keySpec).getEncoded());

        }
        throw new InvalidKeySpecException
            ("Inappropriate key specification");
    } catch (InvalidKeyException e) {
        throw new InvalidKeySpecException(e.getMessage());
    }
}
项目:lookaside_java-1.8.0-openjdk    文件:DESedeKeyFactory.java   
/**
 * Returns a specification (key material) of the given key
 * in the requested format.
 *
 * @param key the key
 *
 * @param keySpec the requested format in which the key material shall be
 * returned
 *
 * @return the underlying key specification (key material) in the
 * requested format
 *
 * @exception InvalidKeySpecException if the requested key specification is
 * inappropriate for the given key, or the given key cannot be processed
 * (e.g., the given key has an unrecognized algorithm or format).
 */
protected KeySpec engineGetKeySpec(SecretKey key, Class<?> keySpec)
    throws InvalidKeySpecException {

    try {
        if ((key instanceof SecretKey)
            && (key.getAlgorithm().equalsIgnoreCase("DESede"))
            && (key.getFormat().equalsIgnoreCase("RAW"))) {

            // Check if requested key spec is amongst the valid ones
            if (DESedeKeySpec.class.isAssignableFrom(keySpec)) {
                return new DESedeKeySpec(key.getEncoded());

            } else {
                throw new InvalidKeySpecException
                    ("Inappropriate key specification");
            }

        } else {
            throw new InvalidKeySpecException
                ("Inappropriate key format/algorithm");
        }
    } catch (InvalidKeyException e) {
        throw new InvalidKeySpecException("Secret key has wrong size");
    }
}
项目:BaseLibrary    文件:DESUtils.java   
/**
 * 数据加解密3DES所需要的Key
 */
public static SecretKey get3DESKey() {
    try {
        // 生成key
        KeyGenerator keyGenerator = KeyGenerator.getInstance("DESede");
        keyGenerator.init(168);// can 168 or 112/new SecureRandom()
        SecretKey secretKey = keyGenerator.generateKey();
        byte[] bytesKey = secretKey.getEncoded();
        // 转化key
        DESedeKeySpec deSedeKeySpec = new DESedeKeySpec(bytesKey);
        SecretKeyFactory factory = SecretKeyFactory.getInstance("DESede");
        return factory.generateSecret(deSedeKeySpec);
    } catch (Exception e) {
        Log.e(TAG,"生成KEY出错:"+e.getMessage());
    }
    return null;
}
项目:meazza    文件:EncryptUtils.java   
/**
 * 使用 3DES 加密,然后使用 Base64 编码。
 *
 * @param str
 *            源字符串
 * @param key
 *            密钥
 * @return 加密后字符串
 * @see {@link #decodeBy3DESAndBase64(String, String)}
 */
@Deprecated
public static String encodeBy3DESAndBase64(String str, String key) {
    String encoded = null;

    try {
        SecretKeyFactory factory = SecretKeyFactory.getInstance(DES_ALGORITHM);
        SecretKey secretKey = factory.generateSecret(new DESedeKeySpec(key.getBytes()));

        Cipher cipher = Cipher.getInstance(DES_ALGORITHM);
        cipher.init(Cipher.ENCRYPT_MODE, secretKey);
        byte[] cipherText = cipher.doFinal(str.getBytes());
        encoded = Base64.encodeBase64String(cipherText);
    } catch (Exception e) {
        logger.error("Encode by 3DES error", e);
    }

    return encoded;
}
项目:meazza    文件:EncryptUtils.java   
/**
 * 使用 Base64 解码,然后使用 3DES 解密。
 *
 * @param str
 *            加密的字符串
 * @param key
 *            密钥
 * @return 解密后字符串
 * @see {@link #encodeBy3DESAndBase64(String, String)}
 */
@Deprecated
public static String decodeBy3DESAndBase64(String str, String key) {
    String decoded = null;

    try {
        SecretKeyFactory factory = SecretKeyFactory.getInstance(DES_ALGORITHM);
        SecretKey secretKey = factory.generateSecret(new DESedeKeySpec(key.getBytes()));

        Cipher cipher = Cipher.getInstance(DES_ALGORITHM);
        cipher.init(Cipher.DECRYPT_MODE, secretKey);
        byte[] clearText = cipher.doFinal(Base64.decodeBase64(str));
        decoded = new String(clearText, DEFAULT_CHARSET);
    } catch (Exception e) {
        logger.error("Decode by 3DES error", e);
    }

    return decoded;
}
项目:javify    文件:DESedeSecretKeyFactoryImpl.java   
protected KeySpec engineGetKeySpec(SecretKey key, Class spec)
    throws InvalidKeySpecException
{
  if (spec.equals(DESedeKeySpec.class))
    try
      {
        return new DESedeKeySpec(key.getEncoded());
      }
    catch (InvalidKeyException ike)
      {
        InvalidKeySpecException ikse = new InvalidKeySpecException(
            "can't create DESede key spec");
        ikse.initCause(ike);
        throw ikse;
      }
  return super.engineGetKeySpec(key, spec);
}
项目:web-sso    文件:KnightDECoder.java   
/**
 * 初始化密钥
 * @param seed
 * @return
 * @throws Exception
 */
public static Key initSecretKey(String seed) throws Exception{
    //返回生成指定算法的密钥 KeyGenerator
    KeyGenerator keyGenerator =   KeyGenerator.getInstance(KEY_ALGORIHM);
    //初始化此密钥生成器,使其具有确定的密钥大小
    SecureRandom secureRandom =  SecureRandom.getInstance("SHA1PRNG");
    secureRandom.setSeed(seed.getBytes());
    keyGenerator.init(KEY_SITE,secureRandom);

    //生成一个密钥
    SecretKey secretKey  = keyGenerator.generateKey();
    //实例化DES 密钥规则
    DESedeKeySpec deSedeKeySpec = new DESedeKeySpec(secretKey.getEncoded());
    //实例化密钥工厂
    SecretKeyFactory secretKeyFactory =  SecretKeyFactory.getInstance(KEY_ALGORIHM);
    //生成密钥
    return secretKeyFactory.generateSecret(deSedeKeySpec);
}
项目:jvm-stm    文件:DESedeSecretKeyFactoryImpl.java   
protected KeySpec engineGetKeySpec(SecretKey key, Class spec)
    throws InvalidKeySpecException
{
  if (spec.equals(DESedeKeySpec.class))
    try
      {
        return new DESedeKeySpec(key.getEncoded());
      }
    catch (InvalidKeyException ike)
      {
        InvalidKeySpecException ikse = new InvalidKeySpecException(
            "can't create DESede key spec");
        ikse.initCause(ike);
        throw ikse;
      }
  return super.engineGetKeySpec(key, spec);
}
项目:infobip-open-jdk-8    文件:Des3DkCrypto.java   
protected Cipher getCipher(byte[] key, byte[] ivec, int mode)
    throws GeneralSecurityException {
    // NoSuchAlgorithException
    SecretKeyFactory factory = SecretKeyFactory.getInstance("desede");

    // InvalidKeyException
    KeySpec spec = new DESedeKeySpec(key, 0);

    // InvalidKeySpecException
    SecretKey secretKey = factory.generateSecret(spec);

    // IV
    if (ivec == null) {
        ivec = ZERO_IV;
    }

    // NoSuchAlgorithmException, NoSuchPaddingException
    // NoSuchProviderException
    Cipher cipher = Cipher.getInstance("DESede/CBC/NoPadding");
    IvParameterSpec encIv = new IvParameterSpec(ivec, 0, ivec.length);

    // InvalidKeyException, InvalidAlgorithParameterException
    cipher.init(mode, secretKey, encIv);

    return cipher;
}
项目:infobip-open-jdk-8    文件:DESedeKeyFactory.java   
/**
 * Generates a <code>SecretKey</code> object from the provided key
 * specification (key material).
 *
 * @param keySpec the specification (key material) of the secret key
 *
 * @return the secret key
 *
 * @exception InvalidKeySpecException if the given key specification
 * is inappropriate for this key factory to produce a public key.
 */
protected SecretKey engineGenerateSecret(KeySpec keySpec)
    throws InvalidKeySpecException {

    try {
        if (keySpec instanceof DESedeKeySpec) {
            return new DESedeKey(((DESedeKeySpec)keySpec).getKey());
        }
        if (keySpec instanceof SecretKeySpec) {
            return new DESedeKey(((SecretKeySpec)keySpec).getEncoded());

        }
        throw new InvalidKeySpecException
            ("Inappropriate key specification");
    } catch (InvalidKeyException e) {
        throw new InvalidKeySpecException(e.getMessage());
    }
}