You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
281 lines
9.3 KiB
281 lines
9.3 KiB
package de.memtext.util; |
|
|
|
import java.security.MessageDigest; |
|
import java.security.NoSuchAlgorithmException; |
|
import java.util.StringTokenizer; |
|
|
|
import javax.crypto.Cipher; |
|
import javax.crypto.NoSuchPaddingException; |
|
import javax.crypto.spec.SecretKeySpec; |
|
import org.apache.commons.codec.digest.Crypt; |
|
|
|
/** |
|
* |
|
* |
|
*/ |
|
public class CryptUtils { |
|
private static byte key[] = { (byte) 255, (byte) 221, (byte) 127, (byte) 109, (byte) 129 }; |
|
|
|
private static final byte[] KEY_DATA = { -108, -50, -5, -75, -98, 28, -116, 107 }; |
|
|
|
private static final SecretKeySpec KEY = new SecretKeySpec(KEY_DATA, "DES"); |
|
|
|
private static Cipher desCipher; |
|
|
|
static final String key2 = "secretKey12349739873560303"; |
|
|
|
private static MessageDigest md5; |
|
|
|
public static String geheimnis1 = "A thing of beauty is a joy for ever: Its loveliness increases; it will never Pass into nothingness; but still will keep A bower quiet for us, and a sleep Full of sweet dreams, and health, and quiet breathing."; |
|
|
|
private CryptUtils() { |
|
} |
|
|
|
public static String decryptSimple(String d) { |
|
byte[] tmp; |
|
tmp = d.getBytes(); |
|
int size = (byte) (tmp[0] ^ key[0 % key.length]); |
|
int index = (byte) (tmp[1] ^ key[1 % key.length]); |
|
byte de[] = new byte[size]; |
|
for (int i = index; i < (size + index); i++) { |
|
de[i - index] = (byte) (tmp[i] ^ key[i % key.length]); |
|
} |
|
return new String(de); |
|
} |
|
|
|
/** |
|
* Encodes a String using SHA |
|
* |
|
* @param txt - |
|
* String to be encode |
|
* @return encoded String In the unprobable event of the SHA algorithm not |
|
* being available the stack trace is printed and the program |
|
* halted. |
|
*/ |
|
public static String encodeSHA(String txt) { |
|
MessageDigest sha = null; |
|
try { |
|
sha = MessageDigest.getInstance("SHA-1"); |
|
} catch (NoSuchAlgorithmException e) { |
|
e.printStackTrace(); |
|
System.exit(-1); |
|
} |
|
|
|
byte[] buf = txt.getBytes(); |
|
byte[] digest = null; |
|
StringBuffer encrypted = new StringBuffer(); |
|
String value; |
|
int n; |
|
sha.update(buf); |
|
digest = sha.digest(); |
|
|
|
for (int i = 0; i < digest.length; i++) { |
|
n = (int) digest[i]; |
|
if (n < 0) n += 256; |
|
value = Integer.toHexString(n); |
|
if (n < 16) value = "0" + value; |
|
encrypted.append(value); |
|
} |
|
return encrypted.toString(); |
|
} |
|
public static String encodeSHA512(String txt) { |
|
if (txt ==null || txt.length() > 1000) { |
|
throw new RuntimeException("Passwort ist ungültig"); |
|
} |
|
|
|
byte txtArray[] = txt.getBytes(); |
|
|
|
return Crypt.crypt(txtArray, "$6$0q5233a66a3e9bea16f3139bfe4f6ce50ced591deafbc4b9ef56d6ae60fe9c4b22aa78dbd704bde57c"); |
|
} |
|
public static String encryptSimple(String s) { |
|
int i = 0; |
|
byte pad[] = s.getBytes(); |
|
int blockSize = Math.max((((pad.length / 8) + 1) * 8 + 2), 24); |
|
int startIndex = (int) (Math.random() * (blockSize - pad.length - 2)) + 2; |
|
byte tmp[] = new byte[blockSize]; |
|
|
|
tmp[0] = (byte) (((byte) pad.length) ^ key[(0) % key.length]); |
|
tmp[1] = (byte) (((byte) startIndex) ^ key[(1) % key.length]); |
|
|
|
for (i = startIndex; i < (pad.length + startIndex); i++) { |
|
tmp[i] = (byte) (pad[i - startIndex] ^ key[(i) % key.length]); |
|
} |
|
|
|
for (int j = i; j < tmp.length; j++) { |
|
tmp[j] = (byte) ((byte) (Math.random() * 255) ^ key[(i) % key.length]); |
|
} |
|
for (int j = 2; j < startIndex; j++) { |
|
tmp[j] = (byte) ((byte) (Math.random() * 255) ^ key[(i) % key.length]); |
|
} |
|
return new String(tmp); |
|
} |
|
|
|
public static void main(String a[]) { |
|
|
|
try { |
|
System.out.println(encodeMD5("1.0/1205147573/superx/admin/" + "mhallwtfitaowid93k39dl229UUU3o3ust")); |
|
|
|
System.out.println(decryptStringDES("#-1#108#-75#72#-110#-117#-34#-94#116#120#-97#-28#-54#-55#11#8")); |
|
// System.out.println(encodeSHA("anfang13"+"A thing of beauty is a |
|
// joy for ever: Its loveliness increases; it will never Pass into |
|
// nothingness; but still will keep A bower quiet for us, and a |
|
// sleep Full of sweet dreams, and health, and quiet breathing.")); |
|
// System.out.println(simpleDecryptString2(StringUtils.readFile(new |
|
// java.io.File("e:/mb/diss/workspace/mytomcatplugin/WEB-INF/preparedxml/dekan1.xml")))); |
|
/* |
|
* String test="sx_des#ölaksdf"; |
|
* System.out.println(test.substring(6)); |
|
* System.out.println(encryptStringDES("ISAT Duisburg")); |
|
* System.out.println(decryptStringDES("#-81#-51#-73#20#121#126#-31#-127#-88#103#-5#29#57#-3#85#42")); |
|
*/ |
|
} catch (Exception e) { |
|
e.printStackTrace(); |
|
} |
|
|
|
} |
|
|
|
public static String encodeMD5(String text) throws NoSuchAlgorithmException { |
|
if (md5 == null) md5 = MessageDigest.getInstance("MD5"); |
|
byte[] digest = null; |
|
digest = md5.digest(text.getBytes()); |
|
String trans = null; |
|
String byteStr; |
|
StringBuffer buf = new StringBuffer(); |
|
for (int i = 0; i <= digest.length - 1; i++) { |
|
byteStr = Integer.toHexString(digest[i]); |
|
switch (byteStr.length()) { |
|
case 1: |
|
trans = "0" + Integer.toHexString(digest[i]); |
|
break; |
|
case 2: |
|
trans = Integer.toHexString(digest[i]); |
|
break; |
|
case 8: |
|
trans = (Integer.toHexString(digest[i])).substring(6, 8); |
|
break; |
|
} |
|
buf.append(trans); |
|
} |
|
return buf.toString(); |
|
} |
|
|
|
public static String decryptStringDES(String aValue) throws Exception { |
|
if (aValue == null) return null; |
|
if (desCipher == null) initDesCipher(); |
|
desCipher.init(Cipher.DECRYPT_MODE, KEY); |
|
byte[] encrypted = makeArrayDES(aValue); |
|
byte[] decrypted = desCipher.doFinal(encrypted); |
|
String result = new String(decrypted); |
|
return result; |
|
|
|
} |
|
|
|
public static String encryptStringDES(String aValue) throws Exception { |
|
if (aValue == null) return null; |
|
if (desCipher == null) initDesCipher(); |
|
|
|
desCipher.init(Cipher.ENCRYPT_MODE, KEY); |
|
byte[] values = aValue.getBytes(); |
|
byte[] encrypted = desCipher.doFinal(values); |
|
return makeStringDES(encrypted); |
|
|
|
} |
|
|
|
/** |
|
* Creates a String from the given array which can be used to store the |
|
* array in a text file |
|
* |
|
* @see #makeArrayDES(String) |
|
*/ |
|
private static String makeStringDES(byte[] values) { |
|
StringBuffer buff = new StringBuffer(values.length * 3); |
|
for (int i = 0; i < values.length; i++) { |
|
buff.append('#'); |
|
buff.append(values[i]); |
|
} |
|
return buff.toString(); |
|
} |
|
|
|
/** |
|
* Internal method which converts an "Array String" into a byte array which |
|
* can be used for decoding |
|
* |
|
* @see #makeString(byte[]) |
|
*/ |
|
private static byte[] makeArrayDES(String values) { |
|
StringTokenizer tok = new StringTokenizer(values, "#"); |
|
byte[] result = new byte[tok.countTokens()]; |
|
byte b; |
|
String c; |
|
int i = 0; |
|
while (tok.hasMoreTokens()) { |
|
c = tok.nextToken(); |
|
try { |
|
b = Byte.parseByte(c); |
|
result[i] = b; |
|
i++; |
|
} catch (NumberFormatException e) { |
|
return new byte[1]; |
|
} |
|
} |
|
return result; |
|
} |
|
|
|
/** |
|
* Help method for creating the DES key. |
|
*/ |
|
private static void createKey() { |
|
/* |
|
* try { KeyGenerator keygen = KeyGenerator.getInstance("DES"); |
|
* SecretKey desKey = keygen.generateKey(); |
|
* |
|
* byte[] keyvalue = desKey.getEncoded(); System.out.print("byte[] |
|
* KEY_DATA = {"); for (int i = 0; i < keyvalue.length; i++) { |
|
* System.out.print(keyvalue[i]); if (i < keyvalue.length - 1) |
|
* System.out.print(","); } System.out.println("};"); } catch (Exception |
|
* e) { e.printStackTrace(); } |
|
*/ |
|
} |
|
|
|
private static void initDesCipher() throws NoSuchAlgorithmException, NoSuchPaddingException { |
|
desCipher = Cipher.getInstance("DES"); |
|
|
|
} |
|
|
|
public static String simpleEncryptString2(String str) { |
|
StringBuffer sb = new StringBuffer(str); |
|
|
|
int lenStr = str.length(); |
|
|
|
for (int i = 0; i < lenStr; i++) { |
|
|
|
sb.setCharAt(i, (char) (str.charAt(i) + 1)); |
|
} |
|
|
|
return sb.toString(); |
|
} |
|
|
|
public static String simpleDecryptString2(String str) { |
|
StringBuffer sb = new StringBuffer(str); |
|
|
|
int lenStr = str.length(); |
|
|
|
for (int i = 0; i < lenStr; i++) { |
|
|
|
sb.setCharAt(i, (char) (str.charAt(i) - 1)); |
|
} |
|
|
|
return sb.toString(); |
|
} |
|
|
|
public static String decryptStringDES_UTF8(String aValue) throws Exception { |
|
if (aValue == null) return null; |
|
if (desCipher == null) initDesCipher(); |
|
desCipher.init(Cipher.DECRYPT_MODE, KEY); |
|
byte[] encrypted = makeArrayDES(aValue); |
|
byte[] decrypted = desCipher.doFinal(encrypted); |
|
String result = new String(decrypted, "UTF-8"); |
|
return result; |
|
} |
|
|
|
}
|
|
|