javax.crypto.spec.DESKeySpec Java Examples

The following examples show how to use javax.crypto.spec.DESKeySpec. 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: RsaMd5DesCksumType.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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;
}
 
Example #2
Source File: DESUtil.java    From ZTuoExchange_framework with MIT License 6 votes vote down vote up
/**
 * DES解密方法
 * @param message 需要解密字符串
 * @param key 解密需要的KEY
 * @param keyENCODED  解密KEY转换编码
 * @param HexStringENCODED  解密字符串转换编码
 * @param CipherInstanceType 解密类型
 * @return
 * @throws Exception
 */
public static String decrypt(String message, String key,String keyENCODED,String HexStringENCODED,String CipherInstanceType) throws Exception {

	byte[] bytesrc = convertHexString(message);
	byte[] theKey = null;
	String jqstr = getstrByte(key).substring(0,
			8).toUpperCase();
	theKey = jqstr.getBytes(keyENCODED);
	Cipher cipher = Cipher.getInstance(CipherInstanceType);
	DESKeySpec desKeySpec = new DESKeySpec(theKey);
	SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
	SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
	IvParameterSpec iv = new IvParameterSpec(theKey);

	cipher.init(Cipher.DECRYPT_MODE, secretKey, iv);

	byte[] retByte = cipher.doFinal(bytesrc);
	return new String(retByte,HexStringENCODED);
}
 
Example #3
Source File: DESKeyFactory.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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 DESKeySpec) {
            return new DESKey(((DESKeySpec)keySpec).getKey());
        }

        if (keySpec instanceof SecretKeySpec) {
            return new DESKey(((SecretKeySpec)keySpec).getEncoded());
        }

        throw new InvalidKeySpecException(
                "Inappropriate key specification");

    } catch (InvalidKeyException e) {
        throw new InvalidKeySpecException(e.getMessage());
    }
}
 
Example #4
Source File: VncClient.java    From cosmic with Apache License 2.0 6 votes vote down vote up
/**
 * Encode password using DES encryption with given challenge.
 *
 * @param challenge a random set of bytes.
 * @param password  a password
 * @return DES hash of password and challenge
 */
public byte[] encodePassword(final byte[] challenge, final String password) throws Exception {
    // VNC password consist of up to eight ASCII characters.
    final byte[] key = {0, 0, 0, 0, 0, 0, 0, 0}; // Padding
    final byte[] passwordAsciiBytes = password.getBytes(RfbConstants.CHARSET);
    System.arraycopy(passwordAsciiBytes, 0, key, 0, Math.min(password.length(), 8));

    // Flip bytes (reverse bits) in key
    for (int i = 0; i < key.length; i++) {
        key[i] = flipByte(key[i]);
    }

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

    final byte[] response = cipher.doFinal(challenge);
    return response;
}
 
Example #5
Source File: SecurityUtility.java    From jstarcraft-core with Apache License 2.0 6 votes vote down vote up
/**
 * DES加密
 * 
 * @param data
 * @param key
 * @return
 */
public static byte[] encryptDes(byte[] data, byte[] key) {
    try {
        // DES算法要求有一个可信任的随机数源
        SecureRandom random = new SecureRandom();
        DESKeySpec desKey = new DESKeySpec(key);
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
        SecretKey secretKey = keyFactory.generateSecret(desKey);
        Cipher cipher = Cipher.getInstance(DES);
        cipher.init(Cipher.ENCRYPT_MODE, secretKey, random);
        return cipher.doFinal(data);
    } catch (Exception exception) {
        String message = StringUtility.format("DES加密数据异常:[{}]", Arrays.toString(data));
        throw new RuntimeException(message, exception);
    }
}
 
Example #6
Source File: DesMacKCksumType.java    From jdk8u-dev-jdk with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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;
}
 
Example #7
Source File: DESKeyFactory.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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 DESKeySpec) {
            return new DESKey(((DESKeySpec)keySpec).getKey());
        }

        if (keySpec instanceof SecretKeySpec) {
            return new DESKey(((SecretKeySpec)keySpec).getEncoded());
        }

        throw new InvalidKeySpecException(
                "Inappropriate key specification");

    } catch (InvalidKeyException e) {
        throw new InvalidKeySpecException(e.getMessage());
    }
}
 
Example #8
Source File: CheckWeakKeys.java    From jdk8u_jdk with GNU General Public License v2.0 6 votes vote down vote up
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.");
    }
 
Example #9
Source File: DESKeyFactory.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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 DESKeySpec) {
            return new DESKey(((DESKeySpec)keySpec).getKey());
        }

        if (keySpec instanceof SecretKeySpec) {
            return new DESKey(((SecretKeySpec)keySpec).getEncoded());
        }

        throw new InvalidKeySpecException(
                "Inappropriate key specification");

    } catch (InvalidKeyException e) {
        throw new InvalidKeySpecException(e.getMessage());
    }
}
 
Example #10
Source File: CheckWeakKeys.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
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.");
    }
 
Example #11
Source File: DESUtil.java    From enjoyshop with Apache License 2.0 6 votes vote down vote up
/**
 * DES算法,加密
 *
 * @param data
 *         待加密字符串
 * @param key
 *            加密私钥,长度不能够小于8位
 * @return 加密后的字节数组,一般结合Base64编码使用

 * @throws Exception
 */
public static String encode(String key, String data) {
    if (data == null)
        return null;
    try {
        DESKeySpec dks = new DESKeySpec(key.getBytes());
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
        // key的长度不能够小于8位字节
        Key secretKey = keyFactory.generateSecret(dks);
        Cipher cipher = Cipher.getInstance(ALGORITHM_DES);
        IvParameterSpec iv = new IvParameterSpec("12345678".getBytes());
        AlgorithmParameterSpec paramSpec = iv;
        cipher.init(Cipher.ENCRYPT_MODE, secretKey, paramSpec);
        byte[] bytes = cipher.doFinal(data.getBytes());
        return byte2String(bytes);
    } catch (Exception e) {
        e.printStackTrace();
        return data;
    }
}
 
Example #12
Source File: DESUtil.java    From ZTuoExchange_framework with MIT License 6 votes vote down vote up
/**
 * DES加密
 * @param HexString 字符串(16位16进制字符串)
 * @param keyStr 密钥16个1
 * @param keyENCODED  Keybyte转换编码
 * @param HexStringENCODED 要加密值的转换byte编码
 * @param CipherInstanceType 需要加密类型
 * @return
 * @throws Exception
 */
public static String ENCRYPTMethod(String HexString, String keyStr,String keyENCODED,String HexStringENCODED,String CipherInstanceType)
		throws Exception {
	String jmstr = "";
	try {
		byte[] theKey = null;
		String jqstr = getstrByte(keyStr).substring(0,8).toUpperCase();
		theKey = jqstr.getBytes(keyENCODED);
		Cipher cipher = Cipher.getInstance(CipherInstanceType);
		DESKeySpec desKeySpec = new DESKeySpec(theKey);
		SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
		SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
		IvParameterSpec iv = new IvParameterSpec(theKey);
		cipher.init(Cipher.ENCRYPT_MODE, secretKey, iv);
		byte[] theCph = cipher.doFinal(HexString.getBytes(HexStringENCODED));
		jmstr = toHexString(theCph).toUpperCase();
		jmstr = toHexString(theCph);
	} catch (Exception e) {
		e.printStackTrace();
		return null;
	}
	return jmstr;
}
 
Example #13
Source File: RsaMd5DesCksumType.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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;
}
 
Example #14
Source File: DesMacKCksumType.java    From jdk8u_jdk with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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[] calculateChecksum(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;
}
 
Example #15
Source File: DESUtil.java    From ImitateTaobaoApp with Apache License 2.0 6 votes vote down vote up
/**
 * DES算法,加密
 *
 * @param data
 *         待加密字符串
 * @param key
 *            加密私钥,长度不能够小于8位
 * @return 加密后的字节数组,一般结合Base64编码使用

 * @throws Exception
 */
public static String encode(String key, String data) {
    if (data == null)
        return null;
    try {
        DESKeySpec dks = new DESKeySpec(key.getBytes());
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
        // key的长度不能够小于8位字节
        Key secretKey = keyFactory.generateSecret(dks);
        Cipher cipher = Cipher.getInstance(ALGORITHM_DES);
        IvParameterSpec iv = new IvParameterSpec("12345678".getBytes());
        AlgorithmParameterSpec paramSpec = iv;
        cipher.init(Cipher.ENCRYPT_MODE, secretKey, paramSpec);
        byte[] bytes = cipher.doFinal(data.getBytes());
        return byte2String(bytes);
    } catch (Exception e) {
        e.printStackTrace();
        return data;
    }
}
 
Example #16
Source File: CheckWeakKeys.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
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.");
    }
 
Example #17
Source File: DESUtil.java    From Encrypt with Apache License 2.0 6 votes vote down vote up
/**
 * DES���ܷ���
 * 
 * @param message
 * @param key
 * @return
 * @throws Exception
 */
public static String decrypt(String message, String key) throws Exception {

	byte[] bytesrc = convertHexString(message);
	byte[] theKey = null;
	String jqstr = getstrByte(key).substring(0, 8).toUpperCase();
	theKey = jqstr.getBytes(ENCODED_ASCII);
	Cipher cipher = Cipher.getInstance(CIPHER_INSTANCE_CBC);
	DESKeySpec desKeySpec = new DESKeySpec(theKey);
	SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
	SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
	IvParameterSpec iv = new IvParameterSpec(theKey);

	cipher.init(Cipher.DECRYPT_MODE, secretKey, iv);

	byte[] retByte = cipher.doFinal(bytesrc);
	return new String(retByte, ENCODED_GB2312);
}
 
Example #18
Source File: DesMacCksumType.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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 {
    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;
}
 
Example #19
Source File: DesMacCksumType.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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);
}
 
Example #20
Source File: DesUtil.java    From radar with Apache License 2.0 6 votes vote down vote up
/** 
 * 用指定的key对数据进行DES解密. 
 * @param data 待解密的数据 
 * @param key DES解密的key 
 * @return 返回DES解密后的数据 
 * @throws Exception 
 * @author <a href="mailto:[email protected]" mce_href="mailto:[email protected]">AmigoXie</a> 
 * Creation date: 2007-7-31 - 下午12:10:34 
 */  
private static byte[] decrypt(byte[] data, byte[] key) throws Exception {  
    // DES算法要求有一个可信任的随机数源  
    SecureRandom sr = new SecureRandom();  
    // 从原始密匙数据创建一个DESKeySpec对象  
    DESKeySpec dks = new DESKeySpec(key);  
    // 创建一个密匙工厂,然后用它把DESKeySpec对象转换成  
    // 一个SecretKey对象  
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ALGORITHM);  
    SecretKey securekey = keyFactory.generateSecret(dks);  
    // Cipher对象实际完成解密操作  
    Cipher cipher = Cipher.getInstance(ALGORITHM);  
    // 用密匙初始化Cipher对象  
    cipher.init(Cipher.DECRYPT_MODE, securekey, sr);  
    // 现在,获取数据并解密  
    // 正式执行解密操作  
    return cipher.doFinal(data);  
}
 
Example #21
Source File: CheckWeakKeys.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
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.");
    }
 
Example #22
Source File: CheckWeakKeys.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
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.");
    }
 
Example #23
Source File: RsaMd5DesCksumType.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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;
}
 
Example #24
Source File: DefaultAppLock.java    From UltimateAndroid with Apache License 2.0 6 votes vote down vote up
private String encryptPassword(String clearText) {
    try {
        DESKeySpec keySpec = new DESKeySpec(
                PASSWORD_ENC_SECRET.getBytes("UTF-8"));
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
        SecretKey key = keyFactory.generateSecret(keySpec);

        Cipher cipher = Cipher.getInstance("DES");
        cipher.init(Cipher.ENCRYPT_MODE, key);
        String encrypedPwd = Base64.encodeToString(cipher.doFinal(clearText
                .getBytes("UTF-8")), Base64.DEFAULT);
        return encrypedPwd;
    } catch (Exception e) {
    }
    return clearText;
}
 
Example #25
Source File: DesMacCksumType.java    From jdk8u_jdk with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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 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);
}
 
Example #26
Source File: DesMacCksumType.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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 {
    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;
}
 
Example #27
Source File: Terminal.java    From JCMathLib with MIT License 6 votes vote down vote up
/**
 * Creates a terminal.
 *
 * @param hostName
 *            The hostname where the cref or jcwde tools are running.
 * @param hostPort
 *            The port used by the cref or jcwde tools
 * @param staticKeyData
 *            The static DES key - secret shared by the on-card and off-card
 *            applications
 * @throws Exception
 */
public Terminal(String hostName, int hostPort, byte[] staticKeyData)
        throws Exception {
    Socket socket = new Socket(hostName, hostPort);
    socket.setTcpNoDelay(true);
    BufferedInputStream input = new BufferedInputStream(socket
            .getInputStream());
    BufferedOutputStream output = new BufferedOutputStream(socket
            .getOutputStream());
    cad = new CadT1Client(input, output);
    KeySpec keySpec = new DESKeySpec(fixParity(staticKeyData));
    keyFactory = SecretKeyFactory.getInstance("DES");
    SecretKey staticKey = keyFactory.generateSecret(keySpec);
    cipher = Cipher.getInstance("DES/CBC/NoPadding");
    cipher.init(Cipher.ENCRYPT_MODE, staticKey, new IvParameterSpec(new byte[] { 0, 0, 0, 0, 0, 0, 0, 0}));
}
 
Example #28
Source File: DESKeyFactory.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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 DESKeySpec) {
            return new DESKey(((DESKeySpec)keySpec).getKey());
        }

        if (keySpec instanceof SecretKeySpec) {
            return new DESKey(((SecretKeySpec)keySpec).getEncoded());
        }

        throw new InvalidKeySpecException(
                "Inappropriate key specification");

    } catch (InvalidKeyException e) {
        throw new InvalidKeySpecException(e.getMessage());
    }
}
 
Example #29
Source File: DESKeyFactory.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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 DESKeySpec) {
            return new DESKey(((DESKeySpec)keySpec).getKey());
        }

        if (keySpec instanceof SecretKeySpec) {
            return new DESKey(((SecretKeySpec)keySpec).getEncoded());
        }

        throw new InvalidKeySpecException(
                "Inappropriate key specification");

    } catch (InvalidKeyException e) {
        throw new InvalidKeySpecException(e.getMessage());
    }
}
 
Example #30
Source File: DESBase64Util.java    From youqu_master with Apache License 2.0 6 votes vote down vote up
/**
 * DES加密
 * 
 * @param src
 * @param key
 * @return
 * @throws Exception
 */
public static byte[] encrypt(byte[] src, byte[] key) throws Exception {
    // DES算法要求有一个可信任的随机数源
    SecureRandom sr = new SecureRandom();
    // 从原始密匙数据创建DESKeySpec对象
    DESKeySpec dks = new DESKeySpec(key);
    // 创建一个密匙工厂,然后用它把DESKeySpec转换成一个SecretKey对象
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
    SecretKey securekey = keyFactory.generateSecret(dks);
    // Cipher对象实际完成加密操作
    Cipher cipher = Cipher.getInstance(DES);
    // 用密匙初始化Cipher对象
    cipher.init(Cipher.ENCRYPT_MODE, securekey, sr);
    // 现在,获取数据并加密
    // 正式执行加密操作
    return cipher.doFinal(src);
}