javax.crypto.spec.DESKeySpec#isWeak()源码实例Demo

下面列出了javax.crypto.spec.DESKeySpec#isWeak() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: jdk8u-jdk   文件: RsaMd5DesCksumType.java
/**
 * Decrypts keyed checksum.
 * @param enc_cksum the buffer for encrypted checksum.
 * @param key the key.
 * @return the checksum.
 *
 * @modified by Yanni Zhang, 12/08/99.
 */
private byte[] decryptKeyedChecksum(byte[] enc_cksum, byte[] key) throws KrbCryptoException {
    //compute modified key
    byte[] new_key = new byte[keySize()];
    System.arraycopy(key, 0, new_key, 0, key.length);
    for (int i = 0; i < new_key.length; i++)
    new_key[i] = (byte)(new_key[i] ^ 0xf0);
    //check for weak keys
    try {
        if (DESKeySpec.isWeak(new_key, 0)) {
            new_key[7] = (byte)(new_key[7] ^ 0xF0);
        }
    } catch (InvalidKeyException ex) {
        // swallow, since it should never happen
    }
    byte[] ivec = new byte[new_key.length];

    byte[] cksum = new byte[enc_cksum.length];
    Des.cbc_encrypt(enc_cksum, cksum, new_key, ivec, false);
    return cksum;
}
 
源代码2 项目: jdk8u-jdk   文件: DesMacCksumType.java
/**
 * Verifies keyed checksum.
 * @param data the data.
 * @param size the length of data.
 * @param key the key used to encrypt the checksum.
 * @param checksum
 * @return true if verification is successful.
 *
 * @modified by Yanni Zhang, 12/08/99.
 */
public boolean verifyKeyedChecksum(byte[] data, int size,
    byte[] key, byte[] checksum, int usage) throws KrbCryptoException {
    byte[] cksum = decryptKeyedChecksum(checksum, key);

    byte[] new_data = new byte[size + confounderSize()];
    System.arraycopy(cksum, 0, new_data, 0, confounderSize());
    System.arraycopy(data, 0, new_data, confounderSize(), size);

    //check for weak keys
    try {
        if (DESKeySpec.isWeak(key, 0)) {
            key[7] = (byte)(key[7] ^ 0xF0);
        }
    } catch (InvalidKeyException ex) {
        // swallow, since it should never happen
    }
    byte[] ivec = new byte[key.length];
    byte[] new_cksum = Des.des_cksum(ivec, new_data, key);
    byte[] orig_cksum = new byte[cksumSize() - confounderSize()];
    System.arraycopy(cksum,  confounderSize(), orig_cksum, 0,
                     cksumSize() - confounderSize());
    return isChecksumEqual(orig_cksum, new_cksum);
}
 
源代码3 项目: jdk8u-dev-jdk   文件: CheckWeakKeys.java
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");
            }
        }

        if (failed) {
            throw new Exception("Failed test!!!");
        }

        System.out.println("Passed test.");
    }
 
源代码4 项目: Bytecoder   文件: DESKeyGenerator.java
/**
 * 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;
}
 
源代码5 项目: openjdk-8   文件: DESKeyGenerator.java
/**
 * 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;
}
 
源代码6 项目: jdk8u-jdk   文件: RsaMd5DesCksumType.java
/**
 * Decrypts keyed checksum.
 * @param enc_cksum the buffer for encrypted checksum.
 * @param key the key.
 * @return the checksum.
 *
 * @modified by Yanni Zhang, 12/08/99.
 */
private byte[] decryptKeyedChecksum(byte[] enc_cksum, byte[] key) throws KrbCryptoException {
    //compute modified key
    byte[] new_key = new byte[keySize()];
    System.arraycopy(key, 0, new_key, 0, key.length);
    for (int i = 0; i < new_key.length; i++)
    new_key[i] = (byte)(new_key[i] ^ 0xf0);
    //check for weak keys
    try {
        if (DESKeySpec.isWeak(new_key, 0)) {
            new_key[7] = (byte)(new_key[7] ^ 0xF0);
        }
    } catch (InvalidKeyException ex) {
        // swallow, since it should never happen
    }
    byte[] ivec = new byte[new_key.length];

    byte[] cksum = new byte[enc_cksum.length];
    Des.cbc_encrypt(enc_cksum, cksum, new_key, ivec, false);
    return cksum;
}
 
源代码7 项目: jdk8u-dev-jdk   文件: DesMacKCksumType.java
/**
 * Calculates keyed checksum.
 * @param data the data used to generate the checksum.
 * @param size length of the data.
 * @param key the key used to encrypt the checksum.
 * @return keyed checksum.
 *
 * @modified by Yanni Zhang, 12/08/99.
 */
public byte[] calculateKeyedChecksum(byte[] data, int size, byte[] key,
    int usage) throws KrbCryptoException {
    //check for weak keys
    try {
        if (DESKeySpec.isWeak(key, 0)) {
            key[7] = (byte)(key[7] ^ 0xF0);
        }
    } catch (InvalidKeyException ex) {
        // swallow, since it should never happen
    }
    byte[] ivec = new byte[key.length];
    System.arraycopy(key, 0, ivec, 0, key.length);
    byte[] cksum = Des.des_cksum(ivec, data, key);
    return cksum;
}
 
源代码8 项目: TencentKona-8   文件: DesMacCksumType.java
/**
 * Verifies keyed checksum.
 * @param data the data.
 * @param size the length of data.
 * @param key the key used to encrypt the checksum.
 * @param checksum
 * @return true if verification is successful.
 *
 * @modified by Yanni Zhang, 12/08/99.
 */
public boolean verifyChecksum(byte[] data, int size,
    byte[] key, byte[] checksum, int usage) throws KrbCryptoException {
    byte[] cksum = decryptKeyedChecksum(checksum, key);

    byte[] new_data = new byte[size + confounderSize()];
    System.arraycopy(cksum, 0, new_data, 0, confounderSize());
    System.arraycopy(data, 0, new_data, confounderSize(), size);

    //check for weak keys
    try {
        if (DESKeySpec.isWeak(key, 0)) {
            key[7] = (byte)(key[7] ^ 0xF0);
        }
    } catch (InvalidKeyException ex) {
        // swallow, since it should never happen
    }
    byte[] ivec = new byte[key.length];
    byte[] new_cksum = Des.des_cksum(ivec, new_data, key);
    byte[] orig_cksum = new byte[cksumSize() - confounderSize()];
    System.arraycopy(cksum,  confounderSize(), orig_cksum, 0,
                     cksumSize() - confounderSize());
    return isChecksumEqual(orig_cksum, new_cksum);
}
 
源代码9 项目: jdk8u-jdk   文件: DesMacCksumType.java
/**
 * Verifies keyed checksum.
 * @param data the data.
 * @param size the length of data.
 * @param key the key used to encrypt the checksum.
 * @param checksum
 * @return true if verification is successful.
 *
 * @modified by Yanni Zhang, 12/08/99.
 */
public boolean verifyKeyedChecksum(byte[] data, int size,
    byte[] key, byte[] checksum, int usage) throws KrbCryptoException {
    byte[] cksum = decryptKeyedChecksum(checksum, key);

    byte[] new_data = new byte[size + confounderSize()];
    System.arraycopy(cksum, 0, new_data, 0, confounderSize());
    System.arraycopy(data, 0, new_data, confounderSize(), size);

    //check for weak keys
    try {
        if (DESKeySpec.isWeak(key, 0)) {
            key[7] = (byte)(key[7] ^ 0xF0);
        }
    } catch (InvalidKeyException ex) {
        // swallow, since it should never happen
    }
    byte[] ivec = new byte[key.length];
    byte[] new_cksum = Des.des_cksum(ivec, new_data, key);
    byte[] orig_cksum = new byte[cksumSize() - confounderSize()];
    System.arraycopy(cksum,  confounderSize(), orig_cksum, 0,
                     cksumSize() - confounderSize());
    return isChecksumEqual(orig_cksum, new_cksum);
}
 
源代码10 项目: openjdk-8   文件: DesMacKCksumType.java
/**
 * Calculates keyed checksum.
 * @param data the data used to generate the checksum.
 * @param size length of the data.
 * @param key the key used to encrypt the checksum.
 * @return keyed checksum.
 *
 * @modified by Yanni Zhang, 12/08/99.
 */
public byte[] calculateKeyedChecksum(byte[] data, int size, byte[] key,
    int usage) throws KrbCryptoException {
    //check for weak keys
    try {
        if (DESKeySpec.isWeak(key, 0)) {
            key[7] = (byte)(key[7] ^ 0xF0);
        }
    } catch (InvalidKeyException ex) {
        // swallow, since it should never happen
    }
    byte[] ivec = new byte[key.length];
    System.arraycopy(key, 0, ivec, 0, key.length);
    byte[] cksum = Des.des_cksum(ivec, data, key);
    return cksum;
}
 
源代码11 项目: openjdk-jdk9   文件: DesMacCksumType.java
/**
 * Verifies keyed checksum.
 * @param data the data.
 * @param size the length of data.
 * @param key the key used to encrypt the checksum.
 * @param checksum the checksum.
 * @return true if verification is successful.
 *
 * @modified by Yanni Zhang, 12/08/99.
 */
public boolean verifyKeyedChecksum(byte[] data, int size,
    byte[] key, byte[] checksum, int usage) throws KrbCryptoException {
    byte[] cksum = decryptKeyedChecksum(checksum, key);

    byte[] new_data = new byte[size + confounderSize()];
    System.arraycopy(cksum, 0, new_data, 0, confounderSize());
    System.arraycopy(data, 0, new_data, confounderSize(), size);

    //check for weak keys
    try {
        if (DESKeySpec.isWeak(key, 0)) {
            key[7] = (byte)(key[7] ^ 0xF0);
        }
    } catch (InvalidKeyException ex) {
        // swallow, since it should never happen
    }
    byte[] ivec = new byte[key.length];
    byte[] new_cksum = Des.des_cksum(ivec, new_data, key);
    byte[] orig_cksum = new byte[cksumSize() - confounderSize()];
    System.arraycopy(cksum,  confounderSize(), orig_cksum, 0,
                     cksumSize() - confounderSize());
    return isChecksumEqual(orig_cksum, new_cksum);
}
 
源代码12 项目: openjdk-8   文件: Des3DkCrypto.java
private static byte[] keyCorrection(byte[] key) {
    // check for weak key
    try {
        if (DESKeySpec.isWeak(key, 0)) {
            key[7] = (byte)(key[7] ^ 0xF0);
        }
    } catch (InvalidKeyException ex) {
        // swallow, since it should never happen
    }
    return key;
}
 
源代码13 项目: jdk8u_jdk   文件: Des3DkCrypto.java
private static byte[] keyCorrection(byte[] key) {
    // check for weak key
    try {
        if (DESKeySpec.isWeak(key, 0)) {
            key[7] = (byte)(key[7] ^ 0xF0);
        }
    } catch (InvalidKeyException ex) {
        // swallow, since it should never happen
    }
    return key;
}
 
源代码14 项目: openjdk-jdk8u-backup   文件: RsaMd5DesCksumType.java
/**
 * Calculates keyed checksum.
 * @param data the data used to generate the checksum.
 * @param size length of the data.
 * @param key the key used to encrypt the checksum.
 * @return keyed checksum.
 *
 * @modified by Yanni Zhang, 12/08/99.
 */
public byte[] calculateKeyedChecksum(byte[] data, int size, byte[] key,
    int usage) throws KrbCryptoException {
    //prepend confounder
    byte[] new_data = new byte[size + confounderSize()];
    byte[] conf = Confounder.bytes(confounderSize());
    System.arraycopy(conf, 0, new_data, 0, confounderSize());
    System.arraycopy(data, 0, new_data, confounderSize(), size);

    //calculate md5 cksum
    byte[] mdc_cksum = calculateChecksum(new_data, new_data.length);
    byte[] cksum = new byte[cksumSize()];
    System.arraycopy(conf, 0, cksum, 0, confounderSize());
    System.arraycopy(mdc_cksum, 0, cksum, confounderSize(),
                     cksumSize() - confounderSize());

    //compute modified key
    byte[] new_key = new byte[keySize()];
    System.arraycopy(key, 0, new_key, 0, key.length);
    for (int i = 0; i < new_key.length; i++)
    new_key[i] = (byte)(new_key[i] ^ 0xf0);
    //check for weak keys
    try {
        if (DESKeySpec.isWeak(new_key, 0)) {
            new_key[7] = (byte)(new_key[7] ^ 0xF0);
        }
    } catch (InvalidKeyException ex) {
        // swallow, since it should never happen
    }
    byte[] ivec = new byte[new_key.length];

    //des-cbc encrypt
    byte[] enc_cksum = new byte[cksum.length];
    Des.cbc_encrypt(cksum, enc_cksum, new_key, ivec, true);
    return enc_cksum;
}
 
源代码15 项目: TencentKona-8   文件: Des3DkCrypto.java
private static byte[] keyCorrection(byte[] key) {
    // check for weak key
    try {
        if (DESKeySpec.isWeak(key, 0)) {
            key[7] = (byte)(key[7] ^ 0xF0);
        }
    } catch (InvalidKeyException ex) {
        // swallow, since it should never happen
    }
    return key;
}
 
源代码16 项目: jdk8u-jdk   文件: RsaMd5DesCksumType.java
/**
 * Calculates keyed checksum.
 * @param data the data used to generate the checksum.
 * @param size length of the data.
 * @param key the key used to encrypt the checksum.
 * @return keyed checksum.
 *
 * @modified by Yanni Zhang, 12/08/99.
 */
public byte[] calculateKeyedChecksum(byte[] data, int size, byte[] key,
    int usage) throws KrbCryptoException {
    //prepend confounder
    byte[] new_data = new byte[size + confounderSize()];
    byte[] conf = Confounder.bytes(confounderSize());
    System.arraycopy(conf, 0, new_data, 0, confounderSize());
    System.arraycopy(data, 0, new_data, confounderSize(), size);

    //calculate md5 cksum
    byte[] mdc_cksum = calculateChecksum(new_data, new_data.length);
    byte[] cksum = new byte[cksumSize()];
    System.arraycopy(conf, 0, cksum, 0, confounderSize());
    System.arraycopy(mdc_cksum, 0, cksum, confounderSize(),
                     cksumSize() - confounderSize());

    //compute modified key
    byte[] new_key = new byte[keySize()];
    System.arraycopy(key, 0, new_key, 0, key.length);
    for (int i = 0; i < new_key.length; i++)
    new_key[i] = (byte)(new_key[i] ^ 0xf0);
    //check for weak keys
    try {
        if (DESKeySpec.isWeak(new_key, 0)) {
            new_key[7] = (byte)(new_key[7] ^ 0xF0);
        }
    } catch (InvalidKeyException ex) {
        // swallow, since it should never happen
    }
    byte[] ivec = new byte[new_key.length];

    //des-cbc encrypt
    byte[] enc_cksum = new byte[cksum.length];
    Des.cbc_encrypt(cksum, enc_cksum, new_key, ivec, true);
    return enc_cksum;
}
 
源代码17 项目: jdk8u60   文件: RsaMd5DesCksumType.java
/**
 * Calculates keyed checksum.
 * @param data the data used to generate the checksum.
 * @param size length of the data.
 * @param key the key used to encrypt the checksum.
 * @return keyed checksum.
 *
 * @modified by Yanni Zhang, 12/08/99.
 */
public byte[] calculateKeyedChecksum(byte[] data, int size, byte[] key,
    int usage) throws KrbCryptoException {
    //prepend confounder
    byte[] new_data = new byte[size + confounderSize()];
    byte[] conf = Confounder.bytes(confounderSize());
    System.arraycopy(conf, 0, new_data, 0, confounderSize());
    System.arraycopy(data, 0, new_data, confounderSize(), size);

    //calculate md5 cksum
    byte[] mdc_cksum = calculateChecksum(new_data, new_data.length);
    byte[] cksum = new byte[cksumSize()];
    System.arraycopy(conf, 0, cksum, 0, confounderSize());
    System.arraycopy(mdc_cksum, 0, cksum, confounderSize(),
                     cksumSize() - confounderSize());

    //compute modified key
    byte[] new_key = new byte[keySize()];
    System.arraycopy(key, 0, new_key, 0, key.length);
    for (int i = 0; i < new_key.length; i++)
    new_key[i] = (byte)(new_key[i] ^ 0xf0);
    //check for weak keys
    try {
        if (DESKeySpec.isWeak(new_key, 0)) {
            new_key[7] = (byte)(new_key[7] ^ 0xF0);
        }
    } catch (InvalidKeyException ex) {
        // swallow, since it should never happen
    }
    byte[] ivec = new byte[new_key.length];

    //des-cbc encrypt
    byte[] enc_cksum = new byte[cksum.length];
    Des.cbc_encrypt(cksum, enc_cksum, new_key, ivec, true);
    return enc_cksum;
}
 
源代码18 项目: dragonwell8_jdk   文件: EncryptionKey.java
/**
 * Generates a sub-sessionkey from a given session key.
 *
 * Used in AcceptSecContextToken and KrbApReq by acceptor- and initiator-
 * side respectively.
 */
public EncryptionKey(EncryptionKey key) throws KrbCryptoException {
    // generate random sub-session key
    keyValue = Confounder.bytes(key.keyValue.length);
    for (int i = 0; i < keyValue.length; i++) {
      keyValue[i] ^= key.keyValue[i];
    }
    keyType = key.keyType;

    // check for key parity and weak keys
    try {
        // check for DES key
        if ((keyType == EncryptedData.ETYPE_DES_CBC_MD5) ||
            (keyType == EncryptedData.ETYPE_DES_CBC_CRC)) {
            // fix DES key parity
            if (!DESKeySpec.isParityAdjusted(keyValue, 0)) {
                keyValue = Des.set_parity(keyValue);
            }
            // check for weak key
            if (DESKeySpec.isWeak(keyValue, 0)) {
                keyValue[7] = (byte)(keyValue[7] ^ 0xF0);
            }
        }
        // check for 3DES key
        if (keyType == EncryptedData.ETYPE_DES3_CBC_HMAC_SHA1_KD) {
            // fix 3DES key parity
            if (!DESedeKeySpec.isParityAdjusted(keyValue, 0)) {
                keyValue = Des3.parityFix(keyValue);
            }
            // check for weak keys
            byte[] oneKey = new byte[8];
            for (int i=0; i<keyValue.length; i+=8) {
                System.arraycopy(keyValue, i, oneKey, 0, 8);
                if (DESKeySpec.isWeak(oneKey, 0)) {
                    keyValue[i+7] = (byte)(keyValue[i+7] ^ 0xF0);
                }
            }
        }
    } catch (GeneralSecurityException e) {
        KrbCryptoException ke = new KrbCryptoException(e.getMessage());
        ke.initCause(e);
        throw ke;
    }
}
 
源代码19 项目: openjdk-jdk9   文件: EncryptionKey.java
/**
 * Generates a sub-sessionkey from a given session key.
 *
 * Used in AcceptSecContextToken and KrbApReq by acceptor- and initiator-
 * side respectively.
 */
public EncryptionKey(EncryptionKey key) throws KrbCryptoException {
    // generate random sub-session key
    keyValue = Confounder.bytes(key.keyValue.length);
    for (int i = 0; i < keyValue.length; i++) {
      keyValue[i] ^= key.keyValue[i];
    }
    keyType = key.keyType;

    // check for key parity and weak keys
    try {
        // check for DES key
        if ((keyType == EncryptedData.ETYPE_DES_CBC_MD5) ||
            (keyType == EncryptedData.ETYPE_DES_CBC_CRC)) {
            // fix DES key parity
            if (!DESKeySpec.isParityAdjusted(keyValue, 0)) {
                keyValue = Des.set_parity(keyValue);
            }
            // check for weak key
            if (DESKeySpec.isWeak(keyValue, 0)) {
                keyValue[7] = (byte)(keyValue[7] ^ 0xF0);
            }
        }
        // check for 3DES key
        if (keyType == EncryptedData.ETYPE_DES3_CBC_HMAC_SHA1_KD) {
            // fix 3DES key parity
            if (!DESedeKeySpec.isParityAdjusted(keyValue, 0)) {
                keyValue = Des3.parityFix(keyValue);
            }
            // check for weak keys
            byte[] oneKey = new byte[8];
            for (int i=0; i<keyValue.length; i+=8) {
                System.arraycopy(keyValue, i, oneKey, 0, 8);
                if (DESKeySpec.isWeak(oneKey, 0)) {
                    keyValue[i+7] = (byte)(keyValue[i+7] ^ 0xF0);
                }
            }
        }
    } catch (GeneralSecurityException e) {
        KrbCryptoException ke = new KrbCryptoException(e.getMessage());
        ke.initCause(e);
        throw ke;
    }
}
 
源代码20 项目: jdk8u-dev-jdk   文件: EncryptionKey.java
/**
 * Generates a sub-sessionkey from a given session key.
 *
 * Used in AcceptSecContextToken and KrbApReq by acceptor- and initiator-
 * side respectively.
 */
public EncryptionKey(EncryptionKey key) throws KrbCryptoException {
    // generate random sub-session key
    keyValue = Confounder.bytes(key.keyValue.length);
    for (int i = 0; i < keyValue.length; i++) {
      keyValue[i] ^= key.keyValue[i];
    }
    keyType = key.keyType;

    // check for key parity and weak keys
    try {
        // check for DES key
        if ((keyType == EncryptedData.ETYPE_DES_CBC_MD5) ||
            (keyType == EncryptedData.ETYPE_DES_CBC_CRC)) {
            // fix DES key parity
            if (!DESKeySpec.isParityAdjusted(keyValue, 0)) {
                keyValue = Des.set_parity(keyValue);
            }
            // check for weak key
            if (DESKeySpec.isWeak(keyValue, 0)) {
                keyValue[7] = (byte)(keyValue[7] ^ 0xF0);
            }
        }
        // check for 3DES key
        if (keyType == EncryptedData.ETYPE_DES3_CBC_HMAC_SHA1_KD) {
            // fix 3DES key parity
            if (!DESedeKeySpec.isParityAdjusted(keyValue, 0)) {
                keyValue = Des3.parityFix(keyValue);
            }
            // check for weak keys
            byte[] oneKey = new byte[8];
            for (int i=0; i<keyValue.length; i+=8) {
                System.arraycopy(keyValue, i, oneKey, 0, 8);
                if (DESKeySpec.isWeak(oneKey, 0)) {
                    keyValue[i+7] = (byte)(keyValue[i+7] ^ 0xF0);
                }
            }
        }
    } catch (GeneralSecurityException e) {
        KrbCryptoException ke = new KrbCryptoException(e.getMessage());
        ke.initCause(e);
        throw ke;
    }
}