Few month ago i have working AuthProtocolCryptographer but reinstalled win 7 and forgot to save it. And now i cant make new one.
Here is my current one:
but it cant de/en correctly and dont know why? de/en changed in last months ? or i have made mistakes in my class ?
Here how i use it:
Here is my current one:
Code:
public class AuthCrypto
{
class CryptCounter
{
int m_Counter = 0;
public byte Key2()
{
return (byte)(m_Counter >> 8);
}
public byte Key1()
{
return (byte)(m_Counter & 0xFF);
}
public void Increment()
{
m_Counter++;
}
}
private CryptCounter _decryptCounter;
private CryptCounter _encryptCounter;
private byte[] _cryptKey1;
private byte[] _cryptKey2;
public AuthCrypto()
{
_decryptCounter = new CryptCounter();
_encryptCounter = new CryptCounter();
_cryptKey1 = new byte[0x100];
_cryptKey2 = new byte[0x100];
int i_key1 = 0x9D;
int i_key2 = 0x62;
for (int i = 0; i < 0x100; i++)
{
_cryptKey1[i] = (byte) i_key1;
_cryptKey2[i] = (byte) i_key2;
i_key1 = (byte)((0x0F + (byte)(i_key1 * 0xFA)) * i_key1 + 0x13);
i_key2 = (byte)((0x79 - (byte)(i_key2 * 0x5C)) * i_key2 + 0x6D);
}
}
public void Decrypt(byte[] buffer)
{
for (int i = 0; i < buffer.length; i++)
{
buffer[i] ^= (byte)0xAB;
buffer[i] = (byte)(buffer[i] >> 4 | buffer[i] << 4);
buffer[i] ^= (byte)(_cryptKey1[_decryptCounter.Key1()] ^ _cryptKey2[_decryptCounter.Key2()]);
_decryptCounter.Increment();
}
}
public void Encrypt(byte[] buffer)
{
for (int i = 0; i < buffer.length; i++)
{
buffer[i] ^= (byte)0xAB;
buffer[i] = (byte)(buffer[i] >> 4 | buffer[i] << 4);
buffer[i] ^= (byte)(_cryptKey1[_encryptCounter.Key1()] ^ _cryptKey2[_encryptCounter.Key2()]);
_encryptCounter.Increment();
}
}
public void EncryptBackwards(byte[] buffer)
{
for (int i = 0; i < buffer.length; i++)
{
buffer[i] ^= (byte)(_cryptKey2[_encryptCounter.Key2()] ^ _cryptKey1[_encryptCounter.Key1()]);
buffer[i] = (byte)(buffer[i] >> 4 | buffer[i] << 4);
buffer[i] ^= (byte)0xAB;
_encryptCounter.Increment();
}
}
public void DecryptBackwards(byte[] buffer)
{
for (int i = 0; i < buffer.length; i++)
{
buffer[i] ^= (byte)(_cryptKey2[_decryptCounter.Key2()] ^ _cryptKey1[_decryptCounter.Key1()]);
buffer[i] = (byte)(buffer[i] >> 4 | buffer[i] << 4);
buffer[i] ^= (byte)0xAB;
_decryptCounter.Increment();
}
}
public void GenerateKeys(int CryptoKey, int AccountID)
{
int tmpkey1 = 0, tmpkey2 = 0;
tmpkey1 = ((CryptoKey + AccountID) ^ (0x4321)) ^ CryptoKey;
tmpkey2 = tmpkey1 * tmpkey1;
for (int i = 0; i < 256; i++)
{
int right = ((3 - (i % 4)) * 8);
int left = ((i % 4)) * 8 + right;
_cryptKey1[i] ^= (byte)(tmpkey1 << right >> left);
_cryptKey2[i] ^= (byte)(tmpkey2 << right >> left);
}
}
}
Here how i use it:
Code:
import java.io.*;
import java.net.*;
public class proxy {
public static void main(String[] args) throws Exception {
String AuthIP = "208.96.34.46";
int port = 9960;
/*
// TEST PURPOSE
AuthCrypto ac = new AuthCrypto();
byte[] buff = {0xC5,(byte)0x48,(byte)0x69,(byte)0x12,(byte)0x04,(byte)0x85,(byte)0xF5,(byte)0xEA};
ac.DecryptBackwards(buff);
Print("Test : ",buff);
*/
ServerSocket SS = new ServerSocket(9960);
Socket Client = SS.accept();
if(Client.isConnected()==true){
System.out.println("Client Connected to proxy !");
Socket Auth = new Socket(AuthIP,9960);
DataInputStream fromAuth = new DataInputStream(Auth.getInputStream());
DataInputStream fromClient = new DataInputStream(Client.getInputStream());
DataOutputStream toClient = new DataOutputStream(Client.getOutputStream());
DataOutputStream toAuth = new DataOutputStream(Auth.getOutputStream());
AuthCrypto crypt = new AuthCrypto();
if(Auth.isConnected()== true){
System.out.println("Connected to "+AuthIP+" at port "+port);
byte buffer[] = new byte[8];
fromAuth.read(buffer, 0, 8);
Print("From Auth : ",buffer);
toClient.write(buffer);
buffer = new byte[276];
fromClient.readFully(buffer);
Print("from Client : ",buffer);
toAuth.write(buffer, 0, buffer.length);
buffer = new byte[52];
fromAuth.readFully(buffer, 0, buffer.length);
Print("Original : ",buffer);
crypt.DecryptBackwards(buffer);
Print("Decrypted : ",buffer);
crypt.Encrypt(buffer);
Print("Encrypted : ",buffer);
}
Auth.close();
}
}
public static String toHex(byte b) {
String result = "";
result =Integer.toString( ( b & 0xff ) + 0x100, 16).substring(1);
return result;
}
public static void Print(String text,byte[] buffer) throws Exception
{
System.out.print(text);
for (int i = 0; i < buffer.length; i++)
{
System.out.print(toHex(buffer[i]).toUpperCase()+" ");
}
System.out.println("");
}
}