Witam,
zastanawiam się jak mogę poprawić te funkcje, aby nie dodawały kilku bitów prefixu ?
dzięki za pomoc, niżej wklejam kod:
public class Main {
public static void main(String[] args) throws UnsupportedEncodingException {
String data = "1040aa5e-641f-4b45-83b5-5927aba5bc08.oddsapi1";
String data1 = encodeTest(data);
String data2 = new String(encrypt(data.getBytes()));
String data3 = new String(decrypt(data2.getBytes()));
String data4 = decodeTest(data3);
System.out.println(data1.getBytes().equals(data4.getBytes()));
}
public static char[] chain = {
'9' ,'@','A','B','C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K',
'L', 'M','N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
'Y', 'Z','a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k',
'l', 'm','n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x',
'y', 'z','0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/',
12 ,23434, 34,67,23,7887,235,5 ,67,985,2345,
};
public static String encodeTest(String key) {
String out ="";
int l = key.length();
char ch;
int ck = 0;
for(int i = 0; i < l; i++) {
if(ck >= chain.length - 1) ck = 0;
ch = key.charAt(i);
ch += chain[ck];
out += ch;
ck++;
}
return out;
}
public static String decodeTest(String key) {
String out ="";
int l = key.length();
char ch;
int ck = 0;
for(int i = 0; i < l; i++) {
if(ck >= chain.length - 1) ck = 0;
ch = key.charAt(i);
ch -= chain[ck];
out += ch;
ck++;
}
return out;
}
public static byte[] encrypt(byte[] data) {
byte[] enc = new byte[data.length];
for (int i = 0; i < data.length; i++) {
enc[i] = (byte) ((i % 2 == 0) ? data[i] + 2 : data[i] - 2);
enc[i] = (byte) ((i % 10 == 0) ? data[i] - 1 : data[i] + 1);
}
return enc;
}
public static byte[] decrypt(byte[] data) {
byte[] enc = new byte[data.length];
for (int i = 0; i < data.length; i++) {
enc[i] = (byte) ((i % 2 == 0) ? data[i] - 2 : data[i] + 2);
enc[i] = (byte) ((i % 10 == 0) ? data[i] + 1 : data[i] - 1);
}
return enc;
}
output:
1040aa5e-641f-4b45-83b5-5927aba5bc08.oddsapi1
jpur¤?z«t~}{±y?°??~??¶??????ÂÄÄ?ÇÉ? ??????ß??
iqvsĄ@{¬u||˛z@±@@@>·@@@@@@ĂĹĂ@ČĘ@ˇ@@@@>@ŕ@@
jpur¤?z«t~}{±y?°??~??¶??????ÂÄÄ?ÇÉ? ??????ß??
1040a?5e-641f-?b??-??b??????aba?bc?8??????p??
false