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;
}
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;
}
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;
}
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;
}
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);
}
Aggregations