Search in sources :

Example 21 with DESKeySpec

use of javax.crypto.spec.DESKeySpec in project cosmic by MissionCriticalCloud.

the class VncClient 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 byte[] encodePassword(final byte[] challenge, final String password) throws Exception {
    // VNC password consist of up to eight ASCII characters.
    // Padding
    final byte[] key = { 0, 0, 0, 0, 0, 0, 0, 0 };
    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;
}
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 22 with DESKeySpec

use of javax.crypto.spec.DESKeySpec in project Lazy by l123456789jy.

the class DES method encrypt.

/**
 * 将字符串进行DES加密
 * @param source 未加密源字符串
 * @return 加密后字符串
 */
public String encrypt(String source) {
    byte[] retByte = null;
    // Create SecretKey object
    DESKeySpec dks = null;
    try {
        dks = new DESKeySpec(KEY);
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ALGORITHM);
        SecretKey securekey = keyFactory.generateSecret(dks);
        // Create IvParameterSpec object with initialization vector
        IvParameterSpec spec = new IvParameterSpec(IV);
        // Create Cipter object
        Cipher cipher = Cipher.getInstance(TRANSFORMATION);
        // Initialize Cipher object
        cipher.init(Cipher.ENCRYPT_MODE, securekey, spec);
        // Decrypting data
        retByte = cipher.doFinal(source.getBytes());
        String result = "";
        if (code == 0) {
            result = new String(retByte, "ISO-8859-1");
        } else if (code == 1) {
            result = Base64.encodeToString(retByte, false);
        } else {
            result = new String(retByte);
        }
        return result;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}
Also used : SecretKey(javax.crypto.SecretKey) DESKeySpec(javax.crypto.spec.DESKeySpec) IvParameterSpec(javax.crypto.spec.IvParameterSpec) Cipher(javax.crypto.Cipher) SecretKeyFactory(javax.crypto.SecretKeyFactory)

Example 23 with DESKeySpec

use of javax.crypto.spec.DESKeySpec in project Lazy by l123456789jy.

the class DES method decrypt.

/**
 * 将DES加密的字符串解密
 * @param encrypted 加密过的字符串
 * @return 未加密源字符串
 */
public String decrypt(String encrypted) {
    byte[] retByte = null;
    // Create SecretKey object
    DESKeySpec dks = null;
    try {
        dks = new DESKeySpec(KEY);
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ALGORITHM);
        SecretKey securekey = keyFactory.generateSecret(dks);
        // Create IvParameterSpec object with initialization vector
        IvParameterSpec spec = new IvParameterSpec(IV);
        // Create Cipter object
        Cipher cipher = Cipher.getInstance(TRANSFORMATION);
        // Initialize Cipher object
        cipher.init(Cipher.DECRYPT_MODE, securekey, spec);
        if (code == 0) {
            retByte = encrypted.getBytes("ISO-8859-1");
        } else if (code == 1) {
            retByte = Base64.decode(encrypted);
        } else {
            retByte = encrypted.getBytes();
        }
        // Decrypting data
        retByte = cipher.doFinal(retByte);
        return new String(retByte, "utf-8");
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}
Also used : SecretKey(javax.crypto.SecretKey) DESKeySpec(javax.crypto.spec.DESKeySpec) IvParameterSpec(javax.crypto.spec.IvParameterSpec) Cipher(javax.crypto.Cipher) SecretKeyFactory(javax.crypto.SecretKeyFactory)

Example 24 with DESKeySpec

use of javax.crypto.spec.DESKeySpec in project orientdb by orientechnologies.

the class ODESEncryption method configure.

public OEncryption configure(final String iOptions) {
    initialized = false;
    if (iOptions == null)
        throw new OSecurityException("DES encryption has been selected, but no key was found. Please configure it by passing the key as property at database create/open. The property key is: '" + OGlobalConfiguration.STORAGE_ENCRYPTION_KEY.getKey() + "'");
    try {
        final byte[] key = OBase64Utils.decode(iOptions);
        final DESKeySpec desKeySpec = new DESKeySpec(key);
        final SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ALGORITHM_NAME);
        theKey = keyFactory.generateSecret(desKeySpec);
        cipher = Cipher.getInstance(TRANSFORMATION);
    } catch (Exception e) {
        throw OException.wrapException(new OInvalidStorageEncryptionKeyException("Cannot initialize DES encryption with current key. Assure the key is a BASE64 - 64 bits long"), e);
    }
    this.initialized = true;
    return this;
}
Also used : OInvalidStorageEncryptionKeyException(com.orientechnologies.orient.core.exception.OInvalidStorageEncryptionKeyException) OSecurityException(com.orientechnologies.orient.core.exception.OSecurityException) DESKeySpec(javax.crypto.spec.DESKeySpec) SecretKeyFactory(javax.crypto.SecretKeyFactory) OSecurityException(com.orientechnologies.orient.core.exception.OSecurityException) OInvalidStorageEncryptionKeyException(com.orientechnologies.orient.core.exception.OInvalidStorageEncryptionKeyException) OException(com.orientechnologies.common.exception.OException)

Example 25 with DESKeySpec

use of javax.crypto.spec.DESKeySpec in project KJFrameForAndroid by kymjs.

the class CipherUtils method getDESKey.

/**
 * 返回可逆算法DES的密钥
 *
 * @param key
 *            前8字节将被用来生成密钥。
 * @return 生成的密钥
 * @throws Exception
 */
public static Key getDESKey(byte[] key) throws Exception {
    DESKeySpec des = new DESKeySpec(key);
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
    return keyFactory.generateSecret(des);
}
Also used : DESKeySpec(javax.crypto.spec.DESKeySpec) SecretKeyFactory(javax.crypto.SecretKeyFactory)

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