Search in sources :

Example 31 with DESKeySpec

use of javax.crypto.spec.DESKeySpec in project DataX by alibaba.

the class DESCipher method decrypt.

/**
 *   * 解密
 *
 *   *
 *
 *   * @param src
 *
 *   * 密文(字节)
 *
 *   * @param key
 *
 *   * 密钥,长度必须是8的倍数
 *
 *   * @return 明文(字节)
 *
 *   * @throws Exception
 *
 *   
 */
public static byte[] decrypt(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.DECRYPT_MODE, securekey, sr);
    return cipher.doFinal(src);
}
Also used : SecretKey(javax.crypto.SecretKey) SecureRandom(java.security.SecureRandom) DESKeySpec(javax.crypto.spec.DESKeySpec) Cipher(javax.crypto.Cipher) SecretKeyFactory(javax.crypto.SecretKeyFactory)

Example 32 with DESKeySpec

use of javax.crypto.spec.DESKeySpec in project DataX by alibaba.

the class DESCipher method decrypt.

/**
 *   * 解密
 *
 *   *
 *
 *   * @param src
 *
 *   * 密文(字节)
 *
 *   * @param key
 *
 *   * 密钥,长度必须是8的倍数
 *
 *   * @return 明文(字节)
 *
 *   * @throws Exception
 *
 *   
 */
public static byte[] decrypt(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.DECRYPT_MODE, securekey, sr);
    return cipher.doFinal(src);
}
Also used : SecretKey(javax.crypto.SecretKey) SecureRandom(java.security.SecureRandom) DESKeySpec(javax.crypto.spec.DESKeySpec) Cipher(javax.crypto.Cipher) SecretKeyFactory(javax.crypto.SecretKeyFactory)

Example 33 with DESKeySpec

use of javax.crypto.spec.DESKeySpec in project j2objc by google.

the class DESKeySpecTest method testGetKey.

/**
 * getKey() method testing. Checks that modification of returned key
 * does not affect the internal key. Also test check an equality of
 * the key with the key specified in the constructor. The object under
 * the test is created by different constructors.
 */
public void testGetKey() {
    byte[] key = { 1, 2, 3, 4, 5, 6, 7, 8 };
    DESKeySpec ks;
    try {
        ks = new DESKeySpec(key);
    } catch (InvalidKeyException e) {
        fail("InvalidKeyException should not be thrown.");
        return;
    }
    byte[] res = ks.getKey();
    assertTrue("The returned array should be equal to the specified " + "in constructor.", Arrays.equals(key, res));
    res[0] += 1;
    assertFalse("The modification of returned key should not affect" + "the underlying key.", key[0] == res[0]);
    byte[] key1 = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
    try {
        ks = new DESKeySpec(key1, 2);
    } catch (InvalidKeyException e) {
        fail("InvalidKeyException should not be thrown.");
        return;
    }
    res = ks.getKey();
    assertNotSame("The returned array should not be the same object " + "as specified in a constructor.", key1, res);
    byte[] exp = new byte[8];
    System.arraycopy(key1, 2, exp, 0, 8);
    assertTrue("The returned array should be equal to the specified " + "in constructor.", Arrays.equals(exp, res));
}
Also used : DESKeySpec(javax.crypto.spec.DESKeySpec) InvalidKeyException(java.security.InvalidKeyException)

Example 34 with DESKeySpec

use of javax.crypto.spec.DESKeySpec in project cloudstack by apache.

the class NoVncClient method encodePassword.

public byte[] encodePassword(byte[] challenge, String password) throws Exception {
    // VNC password consist of up to eight ASCII characters.
    // Padding
    byte[] key = { 0, 0, 0, 0, 0, 0, 0, 0 };
    byte[] passwordAsciiBytes = password.getBytes(Charset.availableCharsets().get("US-ASCII"));
    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]);
    }
    KeySpec desKeySpec = new DESKeySpec(key);
    SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance("DES");
    SecretKey secretKey = secretKeyFactory.generateSecret(desKeySpec);
    Cipher cipher = Cipher.getInstance("DES/ECB/NoPadding");
    cipher.init(Cipher.ENCRYPT_MODE, secretKey);
    byte[] response = cipher.doFinal(challenge);
    return response;
}
Also used : SecretKey(javax.crypto.SecretKey) DESKeySpec(javax.crypto.spec.DESKeySpec) KeySpec(java.security.spec.KeySpec) DESKeySpec(javax.crypto.spec.DESKeySpec) Cipher(javax.crypto.Cipher) SecretKeyFactory(javax.crypto.SecretKeyFactory)

Example 35 with DESKeySpec

use of javax.crypto.spec.DESKeySpec in project cloudstack by apache.

the class Vnc33Authentication method encodePassword.

/**
 * 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 ByteBuffer encodePassword(ByteBuffer challenge, String password) {
    if (challenge.length != 16)
        throw new RuntimeException("Challenge must be exactly 16 bytes long.");
    // VNC password consist of up to eight ASCII characters.
    // Padding
    byte[] key = { 0, 0, 0, 0, 0, 0, 0, 0 };
    byte[] passwordAsciiBytes = password.getBytes(RfbConstants.US_ASCII_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]);
    }
    try {
        KeySpec desKeySpec = new DESKeySpec(key);
        SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance("DES");
        SecretKey secretKey = secretKeyFactory.generateSecret(desKeySpec);
        Cipher cipher = Cipher.getInstance("DES/ECB/NoPadding");
        cipher.init(Cipher.ENCRYPT_MODE, secretKey);
        ByteBuffer buf = new ByteBuffer(cipher.doFinal(challenge.data, challenge.offset, challenge.length));
        return buf;
    } catch (Exception e) {
        throw new RuntimeException("Cannot encode password.", e);
    }
}
Also used : SecretKey(javax.crypto.SecretKey) DESKeySpec(javax.crypto.spec.DESKeySpec) KeySpec(java.security.spec.KeySpec) DESKeySpec(javax.crypto.spec.DESKeySpec) Cipher(javax.crypto.Cipher) SecretKeyFactory(javax.crypto.SecretKeyFactory) ByteBuffer(streamer.ByteBuffer)

Aggregations

DESKeySpec (javax.crypto.spec.DESKeySpec)69 SecretKeyFactory (javax.crypto.SecretKeyFactory)52 SecretKey (javax.crypto.SecretKey)48 Cipher (javax.crypto.Cipher)45 SecureRandom (java.security.SecureRandom)20 InvalidKeyException (java.security.InvalidKeyException)13 KeySpec (java.security.spec.KeySpec)12 IvParameterSpec (javax.crypto.spec.IvParameterSpec)11 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)9 BadPaddingException (javax.crypto.BadPaddingException)8 IllegalBlockSizeException (javax.crypto.IllegalBlockSizeException)8 NoSuchPaddingException (javax.crypto.NoSuchPaddingException)8 DESedeKeySpec (javax.crypto.spec.DESedeKeySpec)8 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)7 SecretKeySpec (javax.crypto.spec.SecretKeySpec)7 UnsupportedEncodingException (java.io.UnsupportedEncodingException)5 Key (java.security.Key)4 AlgorithmParameterSpec (java.security.spec.AlgorithmParameterSpec)3 NameNotFoundException (android.content.pm.PackageManager.NameNotFoundException)2 SQLException (android.database.SQLException)2