📄 helper.java
字号:
private static int fourBytesToInt(byte b[], int offset)
{
int value;
value = byteToUnsigned(b[offset++]);
value |= (byteToUnsigned(b[offset++]) << 8);
value |= (byteToUnsigned(b[offset++]) << 16);
value |= (byteToUnsigned(b[offset++]) << 24);
return(value);
}
private static final void intToFourBytes(int iValue, byte b[], int offset)
{
b[offset++] = (byte)((iValue) & 0xff);
b[offset++] = (byte)((iValue >>> 8 ) & 0xff);
b[offset++] = (byte)((iValue >>> 16) & 0xff);
b[offset++] = (byte)((iValue >>> 24) & 0xff);
}
private static final void PERM_OP(int a, int b, int n, int m, int results[])
{
int t;
t = ((a >>> n) ^ b) & m;
a ^= t << n;
b ^= t;
results[0] = a;
results[1] = b;
}
private static final int HPERM_OP(int a, int n, int m)
{
int t;
t = ((a << (16 - n)) ^ a) & m;
a = a ^ t ^ (t >>> (16 - n));
return(a);
}
private static int [] des_set_key(byte key[])
{
int schedule[] = new int[ITERATIONS * 2];
int c = fourBytesToInt(key, 0);
int d = fourBytesToInt(key, 4);
int results[] = new int[2];
PERM_OP(d, c, 4, 0x0f0f0f0f, results);
d = results[0]; c = results[1];
c = HPERM_OP(c, -2, 0xcccc0000);
d = HPERM_OP(d, -2, 0xcccc0000);
PERM_OP(d, c, 1, 0x55555555, results);
d = results[0]; c = results[1];
PERM_OP(c, d, 8, 0x00ff00ff, results);
c = results[0]; d = results[1];
PERM_OP(d, c, 1, 0x55555555, results);
d = results[0]; c = results[1];
d = (((d & 0x000000ff) << 16) | (d & 0x0000ff00) |
((d & 0x00ff0000) >>> 16) | ((c & 0xf0000000) >>> 4));
c &= 0x0fffffff;
int s, t;
int j = 0;
for(int i = 0; i < ITERATIONS; i ++)
{
if(shifts2[i])
{
c = (c >>> 2) | (c << 26);
d = (d >>> 2) | (d << 26);
}
else
{
c = (c >>> 1) | (c << 27);
d = (d >>> 1) | (d << 27);
}
c &= 0x0fffffff;
d &= 0x0fffffff;
s = skb[0][ (c ) & 0x3f ]|
skb[1][((c >>> 6) & 0x03) | ((c >>> 7) & 0x3c)]|
skb[2][((c >>> 13) & 0x0f) | ((c >>> 14) & 0x30)]|
skb[3][((c >>> 20) & 0x01) | ((c >>> 21) & 0x06) |
((c >>> 22) & 0x38)];
t = skb[4][ (d ) & 0x3f ]|
skb[5][((d >>> 7) & 0x03) | ((d >>> 8) & 0x3c)]|
skb[6][ (d >>>15) & 0x3f ]|
skb[7][((d >>>21) & 0x0f) | ((d >>> 22) & 0x30)];
schedule[j++] = ((t << 16) | (s & 0x0000ffff)) & 0xffffffff;
s = ((s >>> 16) | (t & 0xffff0000));
s = (s << 4) | (s >>> 28);
schedule[j++] = s & 0xffffffff;
}
return(schedule);
}
private static final int D_ENCRYPT
(
int L, int R, int S, int E0, int E1, int s[]
)
{
int t, u, v;
v = R ^ (R >>> 16);
u = v & E0;
v = v & E1;
u = (u ^ (u << 16)) ^ R ^ s[S];
t = (v ^ (v << 16)) ^ R ^ s[S + 1];
t = (t >>> 4) | (t << 28);
L ^= SPtrans[1][(t ) & 0x3f] |
SPtrans[3][(t >>> 8) & 0x3f] |
SPtrans[5][(t >>> 16) & 0x3f] |
SPtrans[7][(t >>> 24) & 0x3f] |
SPtrans[0][(u ) & 0x3f] |
SPtrans[2][(u >>> 8) & 0x3f] |
SPtrans[4][(u >>> 16) & 0x3f] |
SPtrans[6][(u >>> 24) & 0x3f];
return(L);
}
private static final int [] body(int schedule[], int Eswap0, int Eswap1)
{
int left = 0;
int right = 0;
int t = 0;
for(int j = 0; j < 25; j ++)
{
for(int i = 0; i < ITERATIONS * 2; i += 4)
{
left = D_ENCRYPT(left, right, i, Eswap0, Eswap1, schedule);
right = D_ENCRYPT(right, left, i + 2, Eswap0, Eswap1, schedule);
}
t = left;
left = right;
right = t;
}
t = right;
right = (left >>> 1) | (left << 31);
left = (t >>> 1) | (t << 31);
left &= 0xffffffff;
right &= 0xffffffff;
int results[] = new int[2];
PERM_OP(right, left, 1, 0x55555555, results);
right = results[0]; left = results[1];
PERM_OP(left, right, 8, 0x00ff00ff, results);
left = results[0]; right = results[1];
PERM_OP(right, left, 2, 0x33333333, results);
right = results[0]; left = results[1];
PERM_OP(left, right, 16, 0x0000ffff, results);
left = results[0]; right = results[1];
PERM_OP(right, left, 4, 0x0f0f0f0f, results);
right = results[0]; left = results[1];
int out[] = new int[2];
out[0] = left; out[1] = right;
return(out);
}
/**
* Perform a Unix crypt() operation
*/
public static final String crypt(String salt, String original)
{
while(salt.length() < 2)
salt += "A";
StringBuffer buffer = new StringBuffer(" ");
char charZero = salt.charAt(0);
char charOne = salt.charAt(1);
buffer.setCharAt(0, charZero);
buffer.setCharAt(1, charOne);
int Eswap0 = con_salt[(int)charZero];
int Eswap1 = con_salt[(int)charOne] << 4;
byte key[] = new byte[8];
for(int i = 0; i < key.length; i ++)
key[i] = (byte)0;
for(int i = 0; i < key.length && i < original.length(); i ++)
{
int iChar = (int)original.charAt(i);
key[i] = (byte)(iChar << 1);
}
int schedule[] = des_set_key(key);
int out[] = body(schedule, Eswap0, Eswap1);
byte b[] = new byte[9];
intToFourBytes(out[0], b, 0);
intToFourBytes(out[1], b, 4);
b[8] = 0;
for(int i = 2, y = 0, u = 0x80; i < 13; i ++)
{
for(int j = 0, c = 0; j < 6; j ++)
{
c <<= 1;
if(((int)b[y] & u) != 0)
c |= 1;
u >>>= 1;
if(u == 0)
{
y++;
u = 0x80;
}
buffer.setCharAt(i, (char)cov_2char[c]);
}
}
return(buffer.toString());
}
public static String joinAddress(Address[] a) {
String s="";
if(a != null) {
for(int i=0;i<a.length;i++) {
s+=a[i].toString()+", ";
}
if(s.length()>=2) {
s=s.substring(0,s.length()-2);
}
} else {
s="";
}
return s;
}
/**
* Calculate session-ID for a session.
*
* @param a Adress of the remote host
* @param h Requestheader of the remote user agent
* @returns Session-ID
*/
public static String calcSessionCode(InetAddress a, HTTPRequestHeader h) {
String temp=a.toString().replace('\n',' ')
+h.getHeader("User-Agent").replace('\n',' ');
temp=""+Long.toHexString(Math.abs(temp.hashCode()));
if(h.getContent("login") != null && !h.getContent("login").equals("") && h.getPath().startsWith("/login")) {
temp += Long.toHexString(Math.abs(h.getContent("login").hashCode()));
temp += Long.toHexString(System.currentTimeMillis());
} else if(h.getPath().startsWith("/admin/login")) {
temp+="admin";
}
return temp;
}
public static void main(String[] args) {
System.err.println(Helper.crypt("AA",args[1]));
String crypted=encryptTEA(args[1]);
System.err.println("DES-Crypted: "+crypted);
System.err.println("DES-Decrypted: "+decryptTEA(crypted));
}
private static String str_key="39e858f86df9b909a8c87cb8d9ad599";
public static String encryptTEA(String src) {
byte[] key=new BigInteger(str_key, 16).toByteArray();
TEA tea=new TEA(key);
byte plainSource[] = src.getBytes();
int enc[] = tea.encode(plainSource, plainSource.length);
String s="";
for(int i=0;i<enc.length;i++) {
s+=Integer.toHexString((byte)(enc[i] >>> 24)+128).toUpperCase()+":";
s+=Integer.toHexString((byte)(enc[i] >>> 16)+128).toUpperCase()+":";
s+=Integer.toHexString((byte)(enc[i] >>> 8)+128).toUpperCase()+":";
s+=Integer.toHexString((byte)(enc[i])+128).toUpperCase()+":";
}
// System.err.println("encryptTEA: Returning "+s);
return s;
}
public static String decryptTEA(String src) {
StringTokenizer tok=new StringTokenizer(src,": ");
byte[] key=new BigInteger(str_key, 16).toByteArray();
TEA tea=new TEA(key);
byte inb[] = new byte[tok.countTokens()];
for(int i=0;i<inb.length && tok.hasMoreTokens();i++) {
String s=tok.nextToken();
try {
inb[i]=(byte)(Integer.parseInt(s,16)-128);
} catch(NumberFormatException ex) {
System.err.println("Could not convert string '"+s+"' to byte.");
}
}
byte dec[] = tea.decode(inb,inb.length);
String s=new String(dec);
// System.err.println("encryptTEA: Returning "+s);
return s;
}
/**
* Here we figure out which quote level this line has, simply by counting how many
* ">" are in front of the line, ignoring all whitespaces.
*/
public static int getQuoteLevel(String token) {
int current_quotelevel=0;
for(int i=0;i<token.length() &&
(token.substring(i).startsWith(">") || token.substring(i).startsWith(" "));
i++) {
if(token.substring(i).startsWith(">")) current_quotelevel++;
}
return current_quotelevel;
}
protected static int findFittingBreakPos(String line, int linesize) {
int pos;
for(pos=linesize; pos>0; pos--) {
switch(line.charAt(pos)) {
case ' ':
case '\n':
case '\t':
case '.':
case ',':
case ';':
case ':':
case '!':
case '?':
return pos+1;
}
}
return linesize+1;
}
public static Enumeration breakLine(String line, int linesize, int quotelevel) {
if(line.length() <= linesize) {
final String s=line;
return new Enumeration() {
int count=0;
public boolean hasMoreElements() {
return count == 0;
}
public Object nextElement() {
count++;
if(count==1) return s;
else return null;
}
};
} else {
int breakpos=findFittingBreakPos(line,linesize);
final String first=line.substring(0,breakpos);
String rest="";
for(int i=0;i<quotelevel;i++) rest+=">";
rest+=line.substring(breakpos);
final Enumeration enum=breakLine(rest,linesize,quotelevel);
return new Enumeration() {
int count=0;
public boolean hasMoreElements() {
return count < 1 || enum.hasMoreElements();
}
public Object nextElement() {
count++;
if(count == 1) {
return first;
} else {
return enum.nextElement();
}
}
};
}
}
} // Helper
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -