use of javax.crypto.spec.DESKeySpec in project drools by kiegroup.
the class KeyStoreHelperTest method storeKeyIntoKeyStoreFile.
private SecretKey storeKeyIntoKeyStoreFile(final String keyPhrase) throws CertificateException, NoSuchAlgorithmException, IOException, KeyStoreException, InvalidKeyException, InvalidKeySpecException {
final KeyStore keyStore = KeyStore.getInstance("JCEKS");
keyStore.load(null, KEYSTORE_SERVER_PASSWORD.toCharArray());
final SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance("DES");
final SecretKey mySecretKey = secretKeyFactory.generateSecret(new DESKeySpec(keyPhrase.getBytes()));
final KeyStore.SecretKeyEntry skEntry = new KeyStore.SecretKeyEntry(mySecretKey);
keyStore.setEntry(KEY_ALIAS, skEntry, new KeyStore.PasswordProtection(KEY_PASSWORD.toCharArray()));
try (FileOutputStream fos = new java.io.FileOutputStream(KEYSTORE_JCEKS_FILENAME, false)) {
keyStore.store(fos, KEYSTORE_SERVER_PASSWORD.toCharArray());
}
return mySecretKey;
}
use of javax.crypto.spec.DESKeySpec in project coprhd-controller by CoprHD.
the class NTLMUtils method encryptHashWithDES.
/**
* Encrypts the ntlm2 session hash with DES using the method described at
* http://davenport.sourceforge.net/ntlm.html#theNtlm2SessionResponse.
*
* @param ntlm2SessionHash
* the ntlm2 session hash to encrypt
* @param ntlmHash
* the 21 byte long ntlm hash
* @return the encrypted session hash
* @throws Exception
* if something goes wrong
*/
private static byte[] encryptHashWithDES(byte[] ntlm2SessionHash, byte[] ntlmHash) throws Exception {
byte[] toReturn = new byte[24];
for (int i = 0; i < 3; i++) {
byte[] key = new byte[7];
System.arraycopy(ntlmHash, i * 7, key, 0, 7);
key = adjustOddParityForDES(key);
DESKeySpec desKeySpec = new DESKeySpec(key);
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
Cipher desCipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
desCipher.init(Cipher.ENCRYPT_MODE, secretKey);
System.arraycopy(desCipher.doFinal(ntlm2SessionHash), 0, toReturn, i * 8, 8);
}
return toReturn;
}
use of javax.crypto.spec.DESKeySpec in project oxCore by GluuFederation.
the class StringEncrypter method encrypt.
/**
* Encrypt a string
*
* @param unencryptedString
* String to encrypt
* @return Encrypted string (using scheme and key specified at construction)
* @throws EncryptionException
*/
public String encrypt(final String unencryptedString, String encryptionKey) throws EncryptionException {
lock.lock();
try {
final byte[] keyAsBytes = encryptionKey.getBytes(StringEncrypter.UNICODE_FORMAT);
String encryptionScheme = StringEncrypter.DESEDE_ENCRYPTION_SCHEME;
KeySpec keySpec;
if (encryptionScheme.equalsIgnoreCase(StringEncrypter.DESEDE_ENCRYPTION_SCHEME)) {
keySpec = new DESedeKeySpec(keyAsBytes);
} else if (encryptionScheme.equalsIgnoreCase(StringEncrypter.DES_ENCRYPTION_SCHEME)) {
keySpec = new DESKeySpec(keyAsBytes);
} else {
throw new IllegalArgumentException("Encryption scheme not supported: " + encryptionScheme);
}
return encrypt(unencryptedString, keySpec);
} catch (final Exception e) {
throw new EncryptionException(e);
} finally {
lock.unlock();
}
}
use of javax.crypto.spec.DESKeySpec in project jeesuite-libs by vakinge.
the class DES method encrypt.
/**
* DES算法,加密
*
* @param data 待加密字符串
* @param key 加密私钥,长度不能够小于8位
* @return 加密后的字节数组,一般结合Base64编码使用
* @throws InvalidAlgorithmParameterException
* @throws Exception
*/
public static String encrypt(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);
AlgorithmParameterSpec paramSpec = new IvParameterSpec(IV_PARAMS_BYTES);
;
cipher.init(Cipher.ENCRYPT_MODE, secretKey, paramSpec);
byte[] bytes = cipher.doFinal(data.getBytes());
return byte2hex(bytes);
} catch (Exception e) {
e.printStackTrace();
return data;
}
}
use of javax.crypto.spec.DESKeySpec in project MVPFrames by RockyQu.
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 = encodeToString(retByte, false);
} else {
result = new String(retByte);
}
return result;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
Aggregations