Java Code Examples for javax.crypto.spec.DESedeKeySpec#DES_EDE_KEY_LEN

The following examples show how to use javax.crypto.spec.DESedeKeySpec#DES_EDE_KEY_LEN . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DESedeKey.java    From Bytecoder with Apache License 2.0 6 votes vote down vote up
/**
 * 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));
}
 
Example 2
Source File: GenerateSecretKey.java    From alfresco-core with GNU Lesser General Public License v3.0 5 votes vote down vote up
public byte[] generateKeyData()
{
    try
    {
        SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
        random.setSeed(System.currentTimeMillis());
        byte bytes[] = new byte[DESedeKeySpec.DES_EDE_KEY_LEN];
        random.nextBytes(bytes);
        return bytes;
    }
    catch(Exception e)
    {
        throw new RuntimeException("Unable to generate secret key", e);
    }
}
 
Example 3
Source File: DESedeKeyGenerator.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Generates the Triple DES key.
 *
 * @return the new Triple DES key
 */
protected SecretKey engineGenerateKey() {
    if (this.random == null) {
        this.random = SunJCE.getRandom();
    }

    byte[] rawkey = new byte[DESedeKeySpec.DES_EDE_KEY_LEN];

    if (keysize == 168) {
        // 3 intermediate keys
        this.random.nextBytes(rawkey);

        // Do parity adjustment for each intermediate key
        DESKeyGenerator.setParityBit(rawkey, 0);
        DESKeyGenerator.setParityBit(rawkey, 8);
        DESKeyGenerator.setParityBit(rawkey, 16);
    } else {
        // 2 intermediate keys
        byte[] tmpkey = new byte[16];
        this.random.nextBytes(tmpkey);
        DESKeyGenerator.setParityBit(tmpkey, 0);
        DESKeyGenerator.setParityBit(tmpkey, 8);
        System.arraycopy(tmpkey, 0, rawkey, 0, tmpkey.length);
        // Copy the first 8 bytes into the last
        System.arraycopy(tmpkey, 0, rawkey, 16, 8);
        java.util.Arrays.fill(tmpkey, (byte)0x00);
    }

    DESedeKey desEdeKey = null;
    try {
        desEdeKey = new DESedeKey(rawkey);
    } catch (InvalidKeyException ike) {
        // this never happens
        throw new RuntimeException(ike.getMessage());
    }

    java.util.Arrays.fill(rawkey, (byte)0x00);

    return desEdeKey;
}
 
Example 4
Source File: DESedeKey.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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);
}
 
Example 5
Source File: DESedeKeyGenerator.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Generates the Triple DES key.
 *
 * @return the new Triple DES key
 */
protected SecretKey engineGenerateKey() {
    if (this.random == null) {
        this.random = SunJCE.getRandom();
    }

    byte[] rawkey = new byte[DESedeKeySpec.DES_EDE_KEY_LEN];

    if (keysize == 168) {
        // 3 intermediate keys
        this.random.nextBytes(rawkey);

        // Do parity adjustment for each intermediate key
        DESKeyGenerator.setParityBit(rawkey, 0);
        DESKeyGenerator.setParityBit(rawkey, 8);
        DESKeyGenerator.setParityBit(rawkey, 16);
    } else {
        // 2 intermediate keys
        byte[] tmpkey = new byte[16];
        this.random.nextBytes(tmpkey);
        DESKeyGenerator.setParityBit(tmpkey, 0);
        DESKeyGenerator.setParityBit(tmpkey, 8);
        System.arraycopy(tmpkey, 0, rawkey, 0, tmpkey.length);
        // Copy the first 8 bytes into the last
        System.arraycopy(tmpkey, 0, rawkey, 16, 8);
        java.util.Arrays.fill(tmpkey, (byte)0x00);
    }

    DESedeKey desEdeKey = null;
    try {
        desEdeKey = new DESedeKey(rawkey);
    } catch (InvalidKeyException ike) {
        // this never happens
        throw new RuntimeException(ike.getMessage());
    }

    java.util.Arrays.fill(rawkey, (byte)0x00);

    return desEdeKey;
}
 
Example 6
Source File: DESedeKey.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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);
}
 
Example 7
Source File: DESedeKeyGenerator.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
/**
 * Generates the Triple DES key.
 *
 * @return the new Triple DES key
 */
protected SecretKey engineGenerateKey() {
    if (this.random == null) {
        this.random = SunJCE.getRandom();
    }

    byte[] rawkey = new byte[DESedeKeySpec.DES_EDE_KEY_LEN];

    if (keysize == 168) {
        // 3 intermediate keys
        this.random.nextBytes(rawkey);

        // Do parity adjustment for each intermediate key
        DESKeyGenerator.setParityBit(rawkey, 0);
        DESKeyGenerator.setParityBit(rawkey, 8);
        DESKeyGenerator.setParityBit(rawkey, 16);
    } else {
        // 2 intermediate keys
        byte[] tmpkey = new byte[16];
        this.random.nextBytes(tmpkey);
        DESKeyGenerator.setParityBit(tmpkey, 0);
        DESKeyGenerator.setParityBit(tmpkey, 8);
        System.arraycopy(tmpkey, 0, rawkey, 0, tmpkey.length);
        // Copy the first 8 bytes into the last
        System.arraycopy(tmpkey, 0, rawkey, 16, 8);
        java.util.Arrays.fill(tmpkey, (byte)0x00);
    }

    DESedeKey desEdeKey = null;
    try {
        desEdeKey = new DESedeKey(rawkey);
    } catch (InvalidKeyException ike) {
        // this never happens
        throw new RuntimeException(ike.getMessage());
    }

    java.util.Arrays.fill(rawkey, (byte)0x00);

    return desEdeKey;
}
 
Example 8
Source File: DESedeKey.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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);
}
 
Example 9
Source File: DESedeKey.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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);
}
 
Example 10
Source File: AlfrescoKeyStoreImpl.java    From alfresco-core with GNU Lesser General Public License v3.0 5 votes vote down vote up
private byte[] generateKeyData()
{
    try
    {
        SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
        byte bytes[] = new byte[DESedeKeySpec.DES_EDE_KEY_LEN];
        random.nextBytes(bytes);
        return bytes;
    }
    catch(Exception e)
    {
        throw new RuntimeException("Unable to generate secret key", e);
    }
}
 
Example 11
Source File: DESedeKeyGenerator.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Generates the Triple DES key.
 *
 * @return the new Triple DES key
 */
protected SecretKey engineGenerateKey() {
    if (this.random == null) {
        this.random = SunJCE.getRandom();
    }

    byte[] rawkey = new byte[DESedeKeySpec.DES_EDE_KEY_LEN];

    if (keysize == 168) {
        // 3 intermediate keys
        this.random.nextBytes(rawkey);

        // Do parity adjustment for each intermediate key
        DESKeyGenerator.setParityBit(rawkey, 0);
        DESKeyGenerator.setParityBit(rawkey, 8);
        DESKeyGenerator.setParityBit(rawkey, 16);
    } else {
        // 2 intermediate keys
        byte[] tmpkey = new byte[16];
        this.random.nextBytes(tmpkey);
        DESKeyGenerator.setParityBit(tmpkey, 0);
        DESKeyGenerator.setParityBit(tmpkey, 8);
        System.arraycopy(tmpkey, 0, rawkey, 0, tmpkey.length);
        // Copy the first 8 bytes into the last
        System.arraycopy(tmpkey, 0, rawkey, 16, 8);
        java.util.Arrays.fill(tmpkey, (byte)0x00);
    }

    DESedeKey desEdeKey = null;
    try {
        desEdeKey = new DESedeKey(rawkey);
    } catch (InvalidKeyException ike) {
        // this never happens
        throw new RuntimeException(ike.getMessage());
    }

    java.util.Arrays.fill(rawkey, (byte)0x00);

    return desEdeKey;
}
 
Example 12
Source File: KeyStoreTests.java    From alfresco-repository with GNU Lesser General Public License v3.0 5 votes vote down vote up
public byte[] generateKeyData() throws NoSuchAlgorithmException
{
	SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
	random.setSeed(System.currentTimeMillis());
	byte bytes[] = new byte[DESedeKeySpec.DES_EDE_KEY_LEN];
	random.nextBytes(bytes);
	return bytes;
}
 
Example 13
Source File: EncryptionTests.java    From alfresco-repository with GNU Lesser General Public License v3.0 5 votes vote down vote up
public byte[] generateKeyData() throws NoSuchAlgorithmException
{
	SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
	random.setSeed(System.currentTimeMillis());
	byte bytes[] = new byte[DESedeKeySpec.DES_EDE_KEY_LEN];
	random.nextBytes(bytes);
	return bytes;
}
 
Example 14
Source File: DESedeKeyGenerator.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Generates the Triple DES key.
 *
 * @return the new Triple DES key
 */
protected SecretKey engineGenerateKey() {
    if (this.random == null) {
        this.random = SunJCE.getRandom();
    }

    byte[] rawkey = new byte[DESedeKeySpec.DES_EDE_KEY_LEN];

    if (keysize == 168) {
        // 3 intermediate keys
        this.random.nextBytes(rawkey);

        // Do parity adjustment for each intermediate key
        DESKeyGenerator.setParityBit(rawkey, 0);
        DESKeyGenerator.setParityBit(rawkey, 8);
        DESKeyGenerator.setParityBit(rawkey, 16);
    } else {
        // 2 intermediate keys
        byte[] tmpkey = new byte[16];
        this.random.nextBytes(tmpkey);
        DESKeyGenerator.setParityBit(tmpkey, 0);
        DESKeyGenerator.setParityBit(tmpkey, 8);
        System.arraycopy(tmpkey, 0, rawkey, 0, tmpkey.length);
        // Copy the first 8 bytes into the last
        System.arraycopy(tmpkey, 0, rawkey, 16, 8);
        java.util.Arrays.fill(tmpkey, (byte)0x00);
    }

    DESedeKey desEdeKey = null;
    try {
        desEdeKey = new DESedeKey(rawkey);
    } catch (InvalidKeyException ike) {
        // this never happens
        throw new RuntimeException(ike.getMessage());
    }

    java.util.Arrays.fill(rawkey, (byte)0x00);

    return desEdeKey;
}
 
Example 15
Source File: DESedeKey.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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);
}
 
Example 16
Source File: DESedeKeyGenerator.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Generates the Triple DES key.
 *
 * @return the new Triple DES key
 */
protected SecretKey engineGenerateKey() {
    if (this.random == null) {
        this.random = SunJCE.getRandom();
    }

    byte[] rawkey = new byte[DESedeKeySpec.DES_EDE_KEY_LEN];

    if (keysize == 168) {
        // 3 intermediate keys
        this.random.nextBytes(rawkey);

        // Do parity adjustment for each intermediate key
        DESKeyGenerator.setParityBit(rawkey, 0);
        DESKeyGenerator.setParityBit(rawkey, 8);
        DESKeyGenerator.setParityBit(rawkey, 16);
    } else {
        // 2 intermediate keys
        byte[] tmpkey = new byte[16];
        this.random.nextBytes(tmpkey);
        DESKeyGenerator.setParityBit(tmpkey, 0);
        DESKeyGenerator.setParityBit(tmpkey, 8);
        System.arraycopy(tmpkey, 0, rawkey, 0, tmpkey.length);
        // Copy the first 8 bytes into the last
        System.arraycopy(tmpkey, 0, rawkey, 16, 8);
        java.util.Arrays.fill(tmpkey, (byte)0x00);
    }

    DESedeKey desEdeKey = null;
    try {
        desEdeKey = new DESedeKey(rawkey);
    } catch (InvalidKeyException ike) {
        // this never happens
        throw new RuntimeException(ike.getMessage());
    }

    java.util.Arrays.fill(rawkey, (byte)0x00);

    return desEdeKey;
}
 
Example 17
Source File: DESedeKey.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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);
}
 
Example 18
Source File: DESedeKeyGenerator.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Generates the Triple DES key.
 *
 * @return the new Triple DES key
 */
protected SecretKey engineGenerateKey() {
    if (this.random == null) {
        this.random = SunJCE.getRandom();
    }

    byte[] rawkey = new byte[DESedeKeySpec.DES_EDE_KEY_LEN];

    if (keysize == 168) {
        // 3 intermediate keys
        this.random.nextBytes(rawkey);

        // Do parity adjustment for each intermediate key
        DESKeyGenerator.setParityBit(rawkey, 0);
        DESKeyGenerator.setParityBit(rawkey, 8);
        DESKeyGenerator.setParityBit(rawkey, 16);
    } else {
        // 2 intermediate keys
        byte[] tmpkey = new byte[16];
        this.random.nextBytes(tmpkey);
        DESKeyGenerator.setParityBit(tmpkey, 0);
        DESKeyGenerator.setParityBit(tmpkey, 8);
        System.arraycopy(tmpkey, 0, rawkey, 0, tmpkey.length);
        // Copy the first 8 bytes into the last
        System.arraycopy(tmpkey, 0, rawkey, 16, 8);
        java.util.Arrays.fill(tmpkey, (byte)0x00);
    }

    DESedeKey desEdeKey = null;
    try {
        desEdeKey = new DESedeKey(rawkey);
    } catch (InvalidKeyException ike) {
        // this never happens
        throw new RuntimeException(ike.getMessage());
    }

    java.util.Arrays.fill(rawkey, (byte)0x00);

    return desEdeKey;
}
 
Example 19
Source File: DESedeKey.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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);
}
 
Example 20
Source File: DESedeKey.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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);
}