This repository has been archived on 2025-12-25. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
tcommon-studio-se/org.talend.librariesmanager/resources/java/routines/system/PasswordEncryptUtil.java

78 lines
2.5 KiB
Java

// ============================================================================
//
// Copyright (C) 2006-2014 Talend Inc. - www.talend.com
//
// This source code is available under agreement available at
// %InstallDIR%\features\org.talend.rcp.branding.%PRODUCTNAME%\%PRODUCTNAME%license.txt
//
// You should have received a copy of the agreement
// along with this program; if not, write to Talend SA
// 9 rue Pages 92150 Suresnes, France
//
// ============================================================================
package routines.system;
import java.security.SecureRandom;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
/**
* DOC chuang class global comment. Detailled comment
*/
public class PasswordEncryptUtil {
public static String ENCRYPT_KEY = "Encrypt"; //$NON-NLS-1$
private static String rawKey = "Talend-Key"; //$NON-NLS-1$
private static SecretKey key = null;
private static SecureRandom secureRandom = new SecureRandom();
private static String CHARSET = "UTF-8";
private static SecretKey getSecretKey() throws Exception {
if (key == null) {
byte rawKeyData[] = rawKey.getBytes(CHARSET);//$NON-NLS-1$
DESKeySpec dks = new DESKeySpec(rawKeyData);
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); //$NON-NLS-1$
key = keyFactory.generateSecret(dks);
}
return key;
}
public static String encryptPassword(String input) throws Exception {
if (input == null || input.length() == 0) {
return input;
}
SecretKey key = getSecretKey();
Cipher c = Cipher.getInstance("DES"); //$NON-NLS-1$
c.init(Cipher.ENCRYPT_MODE, key, secureRandom);
byte[] cipherByte = c.doFinal(input.getBytes(CHARSET));//$NON-NLS-1$
String dec = Hex.encodeHexString(cipherByte);
return dec;
}
public static String decryptPassword(String input) {
if (input == null || input.length() == 0) {
return input;
}
byte[] dec = Hex.decodeHex(input.toCharArray());
try {
SecretKey key = getSecretKey();
Cipher c = Cipher.getInstance("DES"); //$NON-NLS-1$
c.init(Cipher.DECRYPT_MODE, key, secureRandom);
byte[] clearByte = c.doFinal(dec);
return new String(clearByte, CHARSET);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}