Search in sources :

Example 16 with DESKeySpec

use of javax.crypto.spec.DESKeySpec in project mailim by zengsn.

the class DESUtil method decrypt.

/**
 * 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);
}
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 17 with DESKeySpec

use of javax.crypto.spec.DESKeySpec in project mailim by zengsn.

the class DESUtil method ENCRYPTMethod.

/**
 * DES加密
 *
 * @param HexString
 *            字符串(16位16进制字符串)
 * @param keyStr
 *            密钥16个1
 * @throws Exception
 */
public static String ENCRYPTMethod(String HexString, String keyStr) throws Exception {
    String jmstr = "";
    try {
        byte[] theKey = null;
        String jqstr = getstrByte(keyStr).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.ENCRYPT_MODE, secretKey, iv);
        byte[] theCph = cipher.doFinal(HexString.getBytes(ENCODED_UTF8));
        jmstr = toHexString(theCph).toUpperCase();
        jmstr = toHexString(theCph);
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
    return jmstr;
}
Also used : SecretKey(javax.crypto.SecretKey) DESKeySpec(javax.crypto.spec.DESKeySpec) IvParameterSpec(javax.crypto.spec.IvParameterSpec) Cipher(javax.crypto.Cipher) SecretKeyFactory(javax.crypto.SecretKeyFactory) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 18 with DESKeySpec

use of javax.crypto.spec.DESKeySpec in project happy-dns-android by qiniu.

the class DES method decrypt.

/**
 * Convert encrypted hex string to UTF-8 string
 * @param data data to decrypt
 * @param key decrypt key
 * @return UTF-8 string
 */
public static String decrypt(String data, String key) {
    try {
        Cipher c = Cipher.getInstance("DES/ECB/PKCS5Padding");
        c.init(Cipher.DECRYPT_MODE, SecretKeyFactory.getInstance("DES").generateSecret(new DESKeySpec(key.getBytes())));
        return new String(c.doFinal(Hex.decodeHex(data.toCharArray())), Charset.defaultCharset());
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}
Also used : DESKeySpec(javax.crypto.spec.DESKeySpec) Cipher(javax.crypto.Cipher)

Example 19 with DESKeySpec

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

the class EncryptionManager method decryptData.

// This method decrypts the usreid/password with the middle 8 bytes of
// the generated secret key and an encryption token. Then it returns the
// decrypted data in a byte array.
// plainText   The byte array form userid/password to encrypt.
// initVector  The byte array which is used to calculate the
// encryption token.
// targetPublicKey   DERBY' public key.
// Returns the decrypted data in a byte array.
public byte[] decryptData(byte[] cipherText, int securityMechanism, byte[] initVector, byte[] targetPublicKey) throws SqlException {
    byte[] plainText = null;
    Key key = null;
    if (token_ == null) {
        token_ = calculateEncryptionToken(securityMechanism, initVector);
    }
    try {
        if (secKey_ == null) {
            // use this encryption key to initiate a SecretKeySpec object
            secKey_ = generatePrivateKey(targetPublicKey);
            SecretKeySpec desKey = new SecretKeySpec(secKey_, "DES");
            key = desKey;
        } else {
            // use this encryption key to initiate a SecretKeySpec object
            DESKeySpec desKey = new DESKeySpec(secKey_);
            if (secretKeyFactory_ == null) {
                secretKeyFactory_ = SecretKeyFactory.getInstance("DES", providerName);
            }
            key = secretKeyFactory_.generateSecret(desKey);
        }
        // We use DES in CBC mode because this is the mode used in PROTOCOL. The
        // encryption mode has to be consistent for encryption and decryption.
        // CBC mode requires an initialization vector(IV) parameter. In CBC mode
        // we need to initialize the Cipher object with an IV, which can be supplied
        // using the javax.crypto.spec.IvParameterSpec class.
        Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding", providerName);
        // generate a IVParameterSpec object and use it to initiate the
        // Cipher object.
        IvParameterSpec ivParam = new IvParameterSpec(token_);
        // initiate the Cipher using encryption mode, encryption key and the
        // IV parameter.
        cipher.init(Cipher.DECRYPT_MODE, key, ivParam);
        // Execute the final phase of encryption
        plainText = cipher.doFinal(cipherText);
    } catch (NoSuchPaddingException e) {
        throw new SqlException(agent_.logWriter_, new ClientMessageId(SQLState.CRYPTO_NO_SUCH_PADDING));
    } catch (BadPaddingException e) {
        throw new SqlException(agent_.logWriter_, new ClientMessageId(SQLState.CRYPTO_BAD_PADDING));
    } catch (IllegalBlockSizeException e) {
        throw new SqlException(agent_.logWriter_, new ClientMessageId(SQLState.CRYPTO_ILLEGAL_BLOCK_SIZE));
    } catch (GeneralSecurityException e) {
        throw new SqlException(agent_.logWriter_, new ClientMessageId(SQLState.SECURITY_EXCEPTION_ENCOUNTERED), e);
    }
    return plainText;
}
Also used : SecretKeySpec(javax.crypto.spec.SecretKeySpec) GeneralSecurityException(java.security.GeneralSecurityException) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) DESKeySpec(javax.crypto.spec.DESKeySpec) IvParameterSpec(javax.crypto.spec.IvParameterSpec) Cipher(javax.crypto.Cipher) BadPaddingException(javax.crypto.BadPaddingException) PublicKey(java.security.PublicKey) Key(java.security.Key) DHPublicKey(javax.crypto.interfaces.DHPublicKey)

Example 20 with DESKeySpec

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

the class EncryptionManager method encryptData.

// This method encrypts the usreid/password with the middle 8 bytes of
// the generated secret key and an encryption token. Then it returns the
// encrypted data in a byte array.
// plainText   The byte array form userid/password to encrypt.
// initVector  The byte array which is used to calculate the
// encryption token.
// targetPublicKey   DERBY' public key.
// Returns the encrypted data in a byte array.
public byte[] encryptData(byte[] plainText, int securityMechanism, byte[] initVector, byte[] targetPublicKey) throws SqlException {
    byte[] cipherText = null;
    Key key = null;
    if (token_ == null) {
        token_ = calculateEncryptionToken(securityMechanism, initVector);
    }
    try {
        if (secKey_ == null) {
            // use this encryption key to initiate a SecretKeySpec object
            secKey_ = generatePrivateKey(targetPublicKey);
            SecretKeySpec desKey = new SecretKeySpec(secKey_, "DES");
            key = desKey;
        } else {
            // use this encryption key to initiate a SecretKeySpec object
            DESKeySpec desKey = new DESKeySpec(secKey_);
            if (secretKeyFactory_ == null) {
                secretKeyFactory_ = SecretKeyFactory.getInstance("DES", providerName);
            }
            key = secretKeyFactory_.generateSecret(desKey);
        }
        // We use DES in CBC mode because this is the mode used in PROTOCOL. The
        // encryption mode has to be consistent for encryption and decryption.
        // CBC mode requires an initialization vector(IV) parameter. In CBC mode
        // we need to initialize the Cipher object with an IV, which can be supplied
        // using the javax.crypto.spec.IvParameterSpec class.
        Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding", providerName);
        // generate a IVParameterSpec object and use it to initiate the
        // Cipher object.
        IvParameterSpec ivParam = new IvParameterSpec(token_);
        // initiate the Cipher using encryption mode, encryption key and the
        // IV parameter.
        cipher.init(Cipher.ENCRYPT_MODE, key, ivParam);
        // Execute the final phase of encryption
        cipherText = cipher.doFinal(plainText);
    } catch (NoSuchPaddingException e) {
        throw new SqlException(agent_.logWriter_, new ClientMessageId(SQLState.CRYPTO_NO_SUCH_PADDING));
    } catch (BadPaddingException e) {
        throw new SqlException(agent_.logWriter_, new ClientMessageId(SQLState.CRYPTO_BAD_PADDING));
    } catch (IllegalBlockSizeException e) {
        throw new SqlException(agent_.logWriter_, new ClientMessageId(SQLState.CRYPTO_ILLEGAL_BLOCK_SIZE));
    } catch (GeneralSecurityException e) {
        throw new SqlException(agent_.logWriter_, new ClientMessageId(SQLState.SECURITY_EXCEPTION_ENCOUNTERED), e);
    }
    return cipherText;
}
Also used : SecretKeySpec(javax.crypto.spec.SecretKeySpec) GeneralSecurityException(java.security.GeneralSecurityException) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) DESKeySpec(javax.crypto.spec.DESKeySpec) IvParameterSpec(javax.crypto.spec.IvParameterSpec) Cipher(javax.crypto.Cipher) BadPaddingException(javax.crypto.BadPaddingException) PublicKey(java.security.PublicKey) Key(java.security.Key) DHPublicKey(javax.crypto.interfaces.DHPublicKey)

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