73 lines
1.9 KiB
Java
73 lines
1.9 KiB
Java
package de.Thiesyy.MelcryptJavaAPI.Core;
|
|
|
|
import java.util.Random;
|
|
|
|
import de.Thiesyy.MelcryptJavaAPI.Util.KeyGenerator;
|
|
import de.Thiesyy.MelcryptJavaAPI.Util.MelcryptDataSet;
|
|
|
|
public class Melcrypt {
|
|
public static MelcryptDataSet encode(int security, String Text) {
|
|
System.out.println("Encrypting can take large Calculation power!");
|
|
String[] Keys = new String[security];
|
|
for(int x = 0; x != security; x++) {
|
|
Keys[x] = KeyGenerator.generateKey();
|
|
}
|
|
for(String x : Keys) {
|
|
Text = encrypt(x, Text);
|
|
}
|
|
return new MelcryptDataSet(Keys, Text);
|
|
}
|
|
private static String encrypt(String key, String Text) {
|
|
int Length = Text.length() * 9;
|
|
String Rdm = genRdmStr(Length);
|
|
String toRet = Rdm;
|
|
long KeyLoc = -1;
|
|
long Loc = 0;
|
|
StringBuilder Return = new StringBuilder(Rdm);
|
|
for(int x = 0; x != Text.length(); x++) {
|
|
KeyLoc++;
|
|
Loc += Integer.parseInt(key.charAt((int) (KeyLoc % 8))+"");
|
|
Return.setCharAt((int) Loc, Text.charAt(x));
|
|
}
|
|
toRet = Return.toString();
|
|
|
|
return toRet;
|
|
}
|
|
|
|
private static String genRdmStr(int length) {
|
|
String toUse = " ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz[]}{?.,=!§$%&()1234567890";
|
|
String built = "";
|
|
for(int x = 0; x != length; x++)
|
|
built += toUse.charAt(new Random().nextInt(toUse.length()));
|
|
|
|
return built;
|
|
}
|
|
public static String decode(MelcryptDataSet MDS) {
|
|
String Crypted = MDS.getCrypted();
|
|
String[] keays = new String[MDS.getCodes().length];
|
|
int a = keays.length-1;
|
|
for(String x : MDS.getCodes()) {
|
|
keays[a] = x;
|
|
a--;
|
|
}
|
|
|
|
for(String key : keays) {
|
|
Crypted = decrypt(key, Crypted);
|
|
}
|
|
return Crypted;
|
|
|
|
}
|
|
private static String decrypt(String key, String crypted) {
|
|
String toRet = "";
|
|
int RealtextLength = crypted.length() / 9;
|
|
int KeyLoc = -1;
|
|
int Loc = 0;
|
|
for(int x = 0; x != RealtextLength; x++) {
|
|
KeyLoc++;
|
|
Loc += Integer.parseInt(key.charAt(KeyLoc % 8)+"");
|
|
toRet += crypted.charAt(Loc);
|
|
}
|
|
return toRet;
|
|
}
|
|
}
|