Search in sources :

Example 1 with KrbCryptoException

use of sun.security.krb5.KrbCryptoException in project jdk8u_jdk by JetBrains.

the class Des method des_cksum.

/**
     * Encrypts the message blocks using DES CBC and output the
     * final block of 8-byte ciphertext.
     * @param ivec Initialization vector.
     * @param msg Input message as an byte array.
     * @param key DES key to encrypt the message.
     * @return the last block of ciphertext.
     *
     * @created by Yanni Zhang, Dec 6, 99.
     */
public static byte[] des_cksum(byte[] ivec, byte[] msg, byte[] key) throws KrbCryptoException {
    Cipher cipher = null;
    byte[] result = new byte[8];
    try {
        cipher = Cipher.getInstance("DES/CBC/NoPadding");
    } catch (Exception e) {
        KrbCryptoException ke = new KrbCryptoException("JCE provider may not be installed. " + e.getMessage());
        ke.initCause(e);
        throw ke;
    }
    IvParameterSpec params = new IvParameterSpec(ivec);
    SecretKeySpec skSpec = new SecretKeySpec(key, "DES");
    try {
        SecretKeyFactory skf = SecretKeyFactory.getInstance("DES");
        // SecretKey sk = skf.generateSecret(skSpec);
        SecretKey sk = (SecretKey) skSpec;
        cipher.init(Cipher.ENCRYPT_MODE, sk, params);
        for (int i = 0; i < msg.length / 8; i++) {
            result = cipher.doFinal(msg, i * 8, 8);
            cipher.init(Cipher.ENCRYPT_MODE, sk, (new IvParameterSpec(result)));
        }
    } catch (GeneralSecurityException e) {
        KrbCryptoException ke = new KrbCryptoException(e.getMessage());
        ke.initCause(e);
        throw ke;
    }
    return result;
}
Also used : SecretKey(javax.crypto.SecretKey) SecretKeySpec(javax.crypto.spec.SecretKeySpec) GeneralSecurityException(java.security.GeneralSecurityException) KrbCryptoException(sun.security.krb5.KrbCryptoException) IvParameterSpec(javax.crypto.spec.IvParameterSpec) Cipher(javax.crypto.Cipher) SecretKeyFactory(javax.crypto.SecretKeyFactory) GeneralSecurityException(java.security.GeneralSecurityException) KrbCryptoException(sun.security.krb5.KrbCryptoException)

Example 2 with KrbCryptoException

use of sun.security.krb5.KrbCryptoException in project jdk8u_jdk by JetBrains.

the class Des method char_to_key.

/**
     * Generates DES key from the password.
     * @param password a char[] used to create the key.
     * @return DES key.
     *
     * @modified by Yanni Zhang, Dec 6, 99
     */
public static long char_to_key(char[] passwdChars) throws KrbCryptoException {
    long key = 0;
    long octet, octet1, octet2 = 0;
    byte[] cbytes = null;
    // Convert password to byte array
    try {
        if (CHARSET == null) {
            cbytes = (new String(passwdChars)).getBytes();
        } else {
            cbytes = (new String(passwdChars)).getBytes(CHARSET);
        }
    } catch (Exception e) {
        // clear-up sensitive information
        if (cbytes != null) {
            Arrays.fill(cbytes, 0, cbytes.length, (byte) 0);
        }
        KrbCryptoException ce = new KrbCryptoException("Unable to convert passwd, " + e);
        ce.initCause(e);
        throw ce;
    }
    // pad data
    byte[] passwdBytes = pad(cbytes);
    byte[] newkey = new byte[8];
    int length = (passwdBytes.length / 8) + (passwdBytes.length % 8 == 0 ? 0 : 1);
    for (int i = 0; i < length; i++) {
        octet = octet2long(passwdBytes, i * 8) & 0x7f7f7f7f7f7f7f7fL;
        if (i % 2 == 1) {
            octet1 = 0;
            for (int j = 0; j < 64; j++) {
                octet1 |= ((octet & (1L << j)) >>> j) << (63 - j);
            }
            octet = octet1 >>> 1;
        }
        key ^= (octet << 1);
    }
    key = set_parity(key);
    if (bad_key(key)) {
        byte[] temp = long2octet(key);
        temp[7] ^= 0xf0;
        key = octet2long(temp);
    }
    newkey = des_cksum(long2octet(key), passwdBytes, long2octet(key));
    key = octet2long(set_parity(newkey));
    if (bad_key(key)) {
        byte[] temp = long2octet(key);
        temp[7] ^= 0xf0;
        key = octet2long(temp);
    }
    // clear-up sensitive information
    if (cbytes != null) {
        Arrays.fill(cbytes, 0, cbytes.length, (byte) 0);
    }
    if (passwdBytes != null) {
        Arrays.fill(passwdBytes, 0, passwdBytes.length, (byte) 0);
    }
    return key;
}
Also used : KrbCryptoException(sun.security.krb5.KrbCryptoException) GeneralSecurityException(java.security.GeneralSecurityException) KrbCryptoException(sun.security.krb5.KrbCryptoException)

Example 3 with KrbCryptoException

use of sun.security.krb5.KrbCryptoException in project jdk8u_jdk by JetBrains.

the class DesCbcEType method encrypt.

/**
     * Encrypts the data using DES in CBC mode.
     * @param data the buffer for plain text.
     * @param key the key to encrypt the data.
     * @param ivec initialization vector.
     * @return buffer for encrypted data.
     *
     * @modified by Yanni Zhang, Feb 24 00.
     */
public byte[] encrypt(byte[] data, byte[] key, byte[] ivec, int usage) throws KrbCryptoException {
    /*
         * To meet export control requirements, double check that the
         * key being used is no longer than 64 bits.
         *
         * Note that from a protocol point of view, an
         * algorithm that is not DES will be rejected before this
         * point. Also, a  DES key that is not 64 bits will be
         * rejected by a good implementations of JCE.
         */
    if (key.length > 8)
        throw new KrbCryptoException("Invalid DES Key!");
    int new_size = data.length + confounderSize() + checksumSize();
    byte[] new_data;
    byte pad;
    /*Data padding: using Kerberos 5 GSS-API mechanism (1.2.2.3), Jun 1996.
         *Before encryption, plain text data is padded to the next highest multiple of blocksize.
         *by appending between 1 and 8 bytes, the value of each such byte being the total number
         *of pad bytes. For example, if new_size = 10, blockSize is 8, we should pad 2 bytes,
         *and the value of each byte is 2.
         *If plaintext data is a multiple of blocksize, we pad a 8 bytes of 8.
         */
    if (new_size % blockSize() == 0) {
        new_data = new byte[new_size + blockSize()];
        pad = (byte) 8;
    } else {
        new_data = new byte[new_size + blockSize() - new_size % blockSize()];
        pad = (byte) (blockSize() - new_size % blockSize());
    }
    for (int i = new_size; i < new_data.length; i++) {
        new_data[i] = pad;
    }
    byte[] conf = Confounder.bytes(confounderSize());
    System.arraycopy(conf, 0, new_data, 0, confounderSize());
    System.arraycopy(data, 0, new_data, startOfData(), data.length);
    byte[] cksum = calculateChecksum(new_data, new_data.length);
    System.arraycopy(cksum, 0, new_data, startOfChecksum(), checksumSize());
    byte[] cipher = new byte[new_data.length];
    Des.cbc_encrypt(new_data, cipher, key, ivec, true);
    return cipher;
}
Also used : KrbCryptoException(sun.security.krb5.KrbCryptoException)

Example 4 with KrbCryptoException

use of sun.security.krb5.KrbCryptoException in project jdk8u_jdk by JetBrains.

the class RsaMd5CksumType method calculateChecksum.

/**
     * Calculates checksum using MD5.
     * @param data the data used to generate the checksum.
     * @param size length of the data.
     * @return the checksum.
     *
     * @modified by Yanni Zhang, 12/08/99.
     */
public byte[] calculateChecksum(byte[] data, int size) throws KrbCryptoException {
    MessageDigest md5;
    byte[] result = null;
    try {
        md5 = MessageDigest.getInstance("MD5");
    } catch (Exception e) {
        throw new KrbCryptoException("JCE provider may not be installed. " + e.getMessage());
    }
    try {
        md5.update(data);
        result = md5.digest();
    } catch (Exception e) {
        throw new KrbCryptoException(e.getMessage());
    }
    return result;
}
Also used : KrbCryptoException(sun.security.krb5.KrbCryptoException) MessageDigest(java.security.MessageDigest) KrbCryptoException(sun.security.krb5.KrbCryptoException)

Example 5 with KrbCryptoException

use of sun.security.krb5.KrbCryptoException in project jdk8u_jdk by JetBrains.

the class Des method cbc_encrypt.

/**
     * Creates a DES cipher in Electronic Codebook mode, with no padding.
     * @param input plain text.
     * @param output the buffer for the result.
     * @param key DES the key to encrypt the text.
     * @param ivec initialization vector.
     *
     * @created by Yanni Zhang, Dec 6 99.
     */
public static void cbc_encrypt(byte[] input, byte[] output, byte[] key, byte[] ivec, boolean encrypt) throws KrbCryptoException {
    Cipher cipher = null;
    try {
        cipher = Cipher.getInstance("DES/CBC/NoPadding");
    } catch (GeneralSecurityException e) {
        KrbCryptoException ke = new KrbCryptoException("JCE provider may not be installed. " + e.getMessage());
        ke.initCause(e);
        throw ke;
    }
    IvParameterSpec params = new IvParameterSpec(ivec);
    SecretKeySpec skSpec = new SecretKeySpec(key, "DES");
    try {
        SecretKeyFactory skf = SecretKeyFactory.getInstance("DES");
        //                  SecretKey sk = skf.generateSecret(skSpec);
        SecretKey sk = (SecretKey) skSpec;
        if (encrypt)
            cipher.init(Cipher.ENCRYPT_MODE, sk, params);
        else
            cipher.init(Cipher.DECRYPT_MODE, sk, params);
        byte[] result;
        result = cipher.doFinal(input);
        System.arraycopy(result, 0, output, 0, result.length);
    } catch (GeneralSecurityException e) {
        KrbCryptoException ke = new KrbCryptoException(e.getMessage());
        ke.initCause(e);
        throw ke;
    }
}
Also used : SecretKey(javax.crypto.SecretKey) SecretKeySpec(javax.crypto.spec.SecretKeySpec) GeneralSecurityException(java.security.GeneralSecurityException) KrbCryptoException(sun.security.krb5.KrbCryptoException) IvParameterSpec(javax.crypto.spec.IvParameterSpec) Cipher(javax.crypto.Cipher) SecretKeyFactory(javax.crypto.SecretKeyFactory)

Aggregations

KrbCryptoException (sun.security.krb5.KrbCryptoException)7 GeneralSecurityException (java.security.GeneralSecurityException)3 MessageDigest (java.security.MessageDigest)2 Cipher (javax.crypto.Cipher)2 SecretKey (javax.crypto.SecretKey)2 SecretKeyFactory (javax.crypto.SecretKeyFactory)2 IvParameterSpec (javax.crypto.spec.IvParameterSpec)2 SecretKeySpec (javax.crypto.spec.SecretKeySpec)2 InvalidKeyException (java.security.InvalidKeyException)1