Code:
public class Encryption {
private String password;
private String buffer;
private String data;
private byte[] dataBuffer;
private String algorithm;
private String result;
/** Creates a new instance of Encryption */
public Encryption(String algo) {
result = new String();
setAlgorithm(algo);
}
public void setPassword(String pass) {
if(algorithm.equals("DES")) {
pass = pass.substring(0, 8);
}
password = pass;
}
private void setAlgorithm(String algo) {
algorithm = algo;
}
public String decode(String data) {
this.data = data;
if(algorithm.equals("DES")) {
decodeDes();
}
return result;
}
public String encode(String data) {
this.data = data;
if(algorithm.equals("DES")) {
encodeDes();
}
return result;
}
private void encodeDes() {
try {
Cipher cipher = Cipher.getInstance("DES");
Key key = new SecretKeySpec(password.getBytes(), "DES");
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] encoded = cipher.doFinal(data.getBytes());
result = new String(encoded);
} catch (Exception e) {
e.printStackTrace();
}
}
private void decodeDes() {
try {
Cipher cipher = Cipher.getInstance("DES");
Key key = new SecretKeySpec(password.getBytes(), "DES");
cipher.init(Cipher.DECRYPT_MODE, key);
byte[] encoded = cipher.doFinal(data.getBytes());
result = new String(encoded);
} catch (Exception e) {
e.printStackTrace();
}
}
}
Code:
public class Main {
/** Creates a new instance of Main */
public Main() {
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
String test = "1234567810";
String pass = "foobarfo";
Encryption en = new Encryption("DES");
en.setPassword(pass);
String testCrypt = en.encode(test);
System.out.println(testCrypt);
System.out.println(en.decode(testCrypt));
}
}
Heraus kommt:
÷?‡‘KÀôØ50þXðê
³çE«NVù10
Ich verstehe das nicht, hab auch versucht mit dem Debugger den Käfer zu finden. Leider komme ich nicht drauf...