Guest User

Untitled

a guest
Feb 19th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.74 KB | None | 0 0
  1. package Server.Crypto;
  2.  
  3. import java.math.BigInteger;
  4. import java.util.Random;
  5.  
  6. /*
  7.  *****************
  8.  * @author capos *
  9.  *****************
  10. */
  11.  
  12. public class Crypto
  13. {
  14.     // Diffie Hellman
  15.     private BigInteger prime;
  16.     private BigInteger publicKey;
  17.     private BigInteger privateKey;
  18.    
  19.     public HabboRC4 RC4Decode;
  20.    
  21.     public void InitRc4(byte[] tKey)
  22.     {
  23.         RC4Decode = new HabboRC4();
  24.         RC4Decode.init(tKey);
  25.     }
  26.    
  27.     public void InitDH(BigInteger p, BigInteger generator, String bigrand)
  28.     {
  29.         prime = p;
  30.         privateKey = new BigInteger(bigrand, 16);
  31.         publicKey = generator.modPow(privateKey, prime);
  32.     }
  33.    
  34.     public String generateSharedKey(String ClientKey)
  35.     {
  36.         return (new BigInteger(ClientKey, 10).modPow(privateKey, prime).toString(16)).toUpperCase();
  37.     }
  38.    
  39.     public String getPublicKey()
  40.     {
  41.         return publicKey.toString(10);
  42.     }
  43.        
  44.     public String generateRandomHexString(int len)
  45.     {
  46.         int rand = 0;
  47.         String result = "";
  48.  
  49.         Random rnd = new Random();
  50.  
  51.         for (int i = 0; i < len; i++)
  52.         {
  53.             rand =  1 + (int)(rnd.nextDouble() * 254); // 1 - 255
  54.             result += Integer.toString(rand, 16);
  55.         }
  56.         return result;
  57.     }
  58.  
  59.     public byte[] HextoBytes(String hexString)
  60.     {
  61.         if ((hexString.length() % 2) != 0)
  62.         {
  63.             hexString = ("0" + hexString);
  64.         }
  65.         byte[] bytes = new byte[hexString.length() / 2];
  66.         int j = 0;
  67.         for (int i = 0; i < bytes.length; i++)
  68.         {
  69.             bytes[i] = (byte)Integer.parseInt(hexString.substring(j, j+2), 16);
  70.             j+=2;
  71.         }
  72.         return bytes;
  73.     }
  74. }
Add Comment
Please, Sign In to add comment