Java Code Examples for javax.crypto.spec.DESKeySpec#DES_KEY_LEN

The following examples show how to use javax.crypto.spec.DESKeySpec#DES_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: DESKeyGenerator.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Generates the DES key.
 *
 * @return the new DES key
 */
protected SecretKey engineGenerateKey() {
    DESKey desKey = null;

    if (this.random == null) {
        this.random = SunJCE.getRandom();
    }

    try {
        byte[] key = new byte[DESKeySpec.DES_KEY_LEN];
        do {
            this.random.nextBytes(key);
            setParityBit(key, 0);
        } while (DESKeySpec.isWeak(key, 0));
        desKey = new DESKey(key);
    } catch (InvalidKeyException e) {
        // this is never thrown
    }

    return desKey;
}
 
Example 2
Source File: DESKeyGenerator.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Generates the DES key.
 *
 * @return the new DES key
 */
protected SecretKey engineGenerateKey() {
    DESKey desKey = null;

    if (this.random == null) {
        this.random = SunJCE.getRandom();
    }

    try {
        byte[] key = new byte[DESKeySpec.DES_KEY_LEN];
        do {
            this.random.nextBytes(key);
            setParityBit(key, 0);
        } while (DESKeySpec.isWeak(key, 0));
        desKey = new DESKey(key);
    } catch (InvalidKeyException e) {
        // this is never thrown
    }

    return desKey;
}
 
Example 3
Source File: DESKeyGenerator.java    From jdk8u_jdk with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Generates the DES key.
 *
 * @return the new DES key
 */
protected SecretKey engineGenerateKey() {
    DESKey desKey = null;

    if (this.random == null) {
        this.random = SunJCE.getRandom();
    }

    try {
        byte[] key = new byte[DESKeySpec.DES_KEY_LEN];
        do {
            this.random.nextBytes(key);
            setParityBit(key, 0);
        } while (DESKeySpec.isWeak(key, 0));
        desKey = new DESKey(key);
    } catch (InvalidKeyException e) {
        // this is never thrown
    }

    return desKey;
}
 
Example 4
Source File: DESKeyGenerator.java    From Bytecoder with Apache License 2.0 6 votes vote down vote up
/**
 * Generates the DES key.
 *
 * @return the new DES key
 */
protected SecretKey engineGenerateKey() {
    DESKey desKey = null;

    if (this.random == null) {
        this.random = SunJCE.getRandom();
    }

    try {
        byte[] key = new byte[DESKeySpec.DES_KEY_LEN];
        do {
            this.random.nextBytes(key);
            setParityBit(key, 0);
        } while (DESKeySpec.isWeak(key, 0));
        desKey = new DESKey(key);
    } catch (InvalidKeyException e) {
        // this is never thrown
    }

    return desKey;
}
 
Example 5
Source File: DESKeyGenerator.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Generates the DES key.
 *
 * @return the new DES key
 */
protected SecretKey engineGenerateKey() {
    DESKey desKey = null;

    if (this.random == null) {
        this.random = SunJCE.getRandom();
    }

    try {
        byte[] key = new byte[DESKeySpec.DES_KEY_LEN];
        do {
            this.random.nextBytes(key);
            setParityBit(key, 0);
        } while (DESKeySpec.isWeak(key, 0));
        desKey = new DESKey(key);
    } catch (InvalidKeyException e) {
        // this is never thrown
    }

    return desKey;
}
 
Example 6
Source File: VncAuthEncoder.java    From jfxvnc with Apache License 2.0 6 votes vote down vote up
private byte[] encryptPassword(VncAuthSecurityMessage msg) throws ProtocolException {
  if (msg.getChallenge().length != 16)
    throw new ProtocolException("invalid challenge length " + msg.getChallenge().length);
  try {
    byte[] keyBytes = new byte[DESKeySpec.DES_KEY_LEN];
    byte[] pwdBytes = String.valueOf(msg.getPassword()).getBytes(StandardCharsets.US_ASCII);

    for (int i = 0; i < keyBytes.length; i++) {
      keyBytes[i] = i < pwdBytes.length ? reverseBitsByte(pwdBytes[i]) : 0;
    }

    KeySpec desKeySpec = new DESKeySpec(keyBytes);
    SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance("DES");
    SecretKey secretKey = secretKeyFactory.generateSecret(desKeySpec);
    Cipher cipher = Cipher.getInstance("DES/ECB/NoPadding");
    cipher.init(Cipher.ENCRYPT_MODE, secretKey);

    return cipher.doFinal(msg.getChallenge());

  } catch (Exception e) {
    throw new ProtocolException("encrypt password failed", e);
  }
}
 
Example 7
Source File: DESKeyGenerator.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Generates the DES key.
 *
 * @return the new DES key
 */
protected SecretKey engineGenerateKey() {
    DESKey desKey = null;

    if (this.random == null) {
        this.random = SunJCE.getRandom();
    }

    try {
        byte[] key = new byte[DESKeySpec.DES_KEY_LEN];
        do {
            this.random.nextBytes(key);
            setParityBit(key, 0);
        } while (DESKeySpec.isWeak(key, 0));
        desKey = new DESKey(key);
    } catch (InvalidKeyException e) {
        // this is never thrown
    }

    return desKey;
}
 
Example 8
Source File: DESKeyGenerator.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Generates the DES key.
 *
 * @return the new DES key
 */
protected SecretKey engineGenerateKey() {
    DESKey desKey = null;

    if (this.random == null) {
        this.random = SunJCE.getRandom();
    }

    try {
        byte[] key = new byte[DESKeySpec.DES_KEY_LEN];
        do {
            this.random.nextBytes(key);
            setParityBit(key, 0);
        } while (DESKeySpec.isWeak(key, 0));
        desKey = new DESKey(key);
    } catch (InvalidKeyException e) {
        // this is never thrown
    }

    return desKey;
}
 
Example 9
Source File: DESKeyGenerator.java    From jdk8u-dev-jdk with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Generates the DES key.
 *
 * @return the new DES key
 */
protected SecretKey engineGenerateKey() {
    DESKey desKey = null;

    if (this.random == null) {
        this.random = SunJCE.getRandom();
    }

    try {
        byte[] key = new byte[DESKeySpec.DES_KEY_LEN];
        do {
            this.random.nextBytes(key);
            setParityBit(key, 0);
        } while (DESKeySpec.isWeak(key, 0));
        desKey = new DESKey(key);
    } catch (InvalidKeyException e) {
        // this is never thrown
    }

    return desKey;
}
 
Example 10
Source File: DESKeyGenerator.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
static void setParityBit(byte[] key, int offset) {
    if (key == null)
        return;

    for (int i = 0; i < DESKeySpec.DES_KEY_LEN; i++) {
        int b = key[offset] & 0xfe;
        b |= (Integer.bitCount(b) & 1) ^ 1;
        key[offset++] = (byte)b;
    }
}
 
Example 11
Source File: DESKeyGenerator.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
static void setParityBit(byte[] key, int offset) {
    if (key == null)
        return;

    for (int i = 0; i < DESKeySpec.DES_KEY_LEN; i++) {
        int b = key[offset] & 0xfe;
        b |= (Integer.bitCount(b) & 1) ^ 1;
        key[offset++] = (byte)b;
    }
}
 
Example 12
Source File: DESKeyGenerator.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
static void setParityBit(byte[] key, int offset) {
    if (key == null)
        return;

    for (int i = 0; i < DESKeySpec.DES_KEY_LEN; i++) {
        int b = key[offset] & 0xfe;
        b |= (Integer.bitCount(b) & 1) ^ 1;
        key[offset++] = (byte)b;
    }
}
 
Example 13
Source File: DESKeyGenerator.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
static void setParityBit(byte[] key, int offset) {
    if (key == null)
        return;

    for (int i = 0; i < DESKeySpec.DES_KEY_LEN; i++) {
        int b = key[offset] & 0xfe;
        b |= (Integer.bitCount(b) & 1) ^ 1;
        key[offset++] = (byte)b;
    }
}
 
Example 14
Source File: DESKeyGenerator.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
static void setParityBit(byte[] key, int offset) {
    if (key == null)
        return;

    for (int i = 0; i < DESKeySpec.DES_KEY_LEN; i++) {
        int b = key[offset] & 0xfe;
        b |= (Integer.bitCount(b) & 1) ^ 1;
        key[offset++] = (byte)b;
    }
}
 
Example 15
Source File: DESKeyGenerator.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
static void setParityBit(byte[] key, int offset) {
    if (key == null)
        return;

    for (int i = 0; i < DESKeySpec.DES_KEY_LEN; i++) {
        int b = key[offset] & 0xfe;
        b |= (Integer.bitCount(b) & 1) ^ 1;
        key[offset++] = (byte)b;
    }
}
 
Example 16
Source File: DESKeyGenerator.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
static void setParityBit(byte[] key, int offset) {
    if (key == null)
        return;

    for (int i = 0; i < DESKeySpec.DES_KEY_LEN; i++) {
        int b = key[offset] & 0xfe;
        b |= (Integer.bitCount(b) & 1) ^ 1;
        key[offset++] = (byte)b;
    }
}
 
Example 17
Source File: DESKey.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
/**
 * Uses the first 8 bytes in <code>key</code>, beginning at
 * <code>offset</code>, as the DES key
 *
 * @param key the buffer with the DES key bytes.
 * @param offset the offset in <code>key</code>, where the DES key bytes
 * start.
 *
 * @exception InvalidKeyException if less than 8 bytes are available for
 * the key.
 */
DESKey(byte[] key, int offset) throws InvalidKeyException {
    if (key == null || key.length - offset < DESKeySpec.DES_KEY_LEN) {
        throw new InvalidKeyException("Wrong key size");
    }
    this.key = new byte[DESKeySpec.DES_KEY_LEN];
    System.arraycopy(key, offset, this.key, 0, DESKeySpec.DES_KEY_LEN);
    DESKeyGenerator.setParityBit(this.key, 0);

    // 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 18
Source File: DESKeyGenerator.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
static void setParityBit(byte[] key, int offset) {
    if (key == null)
        return;

    for (int i = 0; i < DESKeySpec.DES_KEY_LEN; i++) {
        int b = key[offset] & 0xfe;
        b |= (Integer.bitCount(b) & 1) ^ 1;
        key[offset++] = (byte)b;
    }
}
 
Example 19
Source File: DESKey.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Uses the first 8 bytes in <code>key</code>, beginning at
 * <code>offset</code>, as the DES key
 *
 * @param key the buffer with the DES key bytes.
 * @param offset the offset in <code>key</code>, where the DES key bytes
 * start.
 *
 * @exception InvalidKeyException if less than 8 bytes are available for
 * the key.
 */
DESKey(byte[] key, int offset) throws InvalidKeyException {
    if (key == null || key.length - offset < DESKeySpec.DES_KEY_LEN) {
        throw new InvalidKeyException("Wrong key size");
    }
    this.key = new byte[DESKeySpec.DES_KEY_LEN];
    System.arraycopy(key, offset, this.key, 0, DESKeySpec.DES_KEY_LEN);
    DESKeyGenerator.setParityBit(this.key, 0);
}
 
Example 20
Source File: DESKey.java    From openjdk-jdk9 with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Uses the first 8 bytes in <code>key</code>, beginning at
 * <code>offset</code>, as the DES key
 *
 * @param key the buffer with the DES key bytes.
 * @param offset the offset in <code>key</code>, where the DES key bytes
 * start.
 *
 * @exception InvalidKeyException if less than 8 bytes are available for
 * the key.
 */
DESKey(byte[] key, int offset) throws InvalidKeyException {
    if (key == null || key.length - offset < DESKeySpec.DES_KEY_LEN) {
        throw new InvalidKeyException("Wrong key size");
    }
    this.key = new byte[DESKeySpec.DES_KEY_LEN];
    System.arraycopy(key, offset, this.key, 0, DESKeySpec.DES_KEY_LEN);
    DESKeyGenerator.setParityBit(this.key, 0);
}