Дневник Шестеро Михаила - Сжатие потока информации PHP-HTTP-Java

Oct. 6th, 2014

03:42 pm - Сжатие потока информации PHP-HTTP-Java

Previous Entry Add to Memories Tell A Friend Next Entry

Это логическое продолжение моих статей «Сжатие потока информации PHP-HTTP-Qt» и «Шифровка потока информации PHP-HTTP-Java».

Перед вами код на Java, способный конвеерно принимать запакованный (как в первой упомянутой статье) и возможно зашифрованный (как во второй) XML:


import java.util.zip.Inflater;
import java.util.zip.InflaterInputStream;
// ................


public static void main(String[] args
{
  System.out.println("DecryptorTest HELLO");
  
  //Security.addProvider( new IAIK() ); // using IAIK Security Provider
  java.security.Security.addProvider(new gnu.crypto.jce.GnuCrypto());
  
  // Using Hex in Apache Commons:
  // byte[] bytes = Hex.decodeHex(key0.toCharArray());
  StringBuilder keyc = new StringBuilder();
    for (int i = 0; i < key0.length(); i+=2) {
        String str = key0.substring(i, i+2);
        keyc.append((char)Byte.parseByte(str, 16));
    }
  System.out.println("DecryptorTest: Common secret key (plain) =" + keyc);

  MD5 md5 = new MD5();
  //IMessageDigest md5 = HashFactory.getInstance("MD5");
  byte[] iv;
  try {
    iv = iv0.getBytes"UTF-8" );
  catch (UnsupportedEncodingException e1) {
    System.out.println("DecryptorTest: Error: UnsupportedEncodingException (UTF-8)");
    return;
  }
  System.out.println("DecryptorTest: Common initialization vector length (bytes) =" + iv.length);
  md5.update(iv, 0, iv.length);
  String ivh = "";
  iv = md5.digest();
  for (int i = 0; i < iv.length; i++) {
    ivh += String.format("%02x", iv[i] );
  }
  System.out.println("DecryptorTest: Common initialization vector (MD5) =" + ivh);

  // encrypt the user password and convert it to Hex
  String passwordh;
  try {
    passwordh = encryptpassword0, keyc.toString(), iv );
  catch InvalidKeyException 
      | UnsupportedEncodingException 
      | IllegalBlockSizeException 
      | BadPaddingException 
      | NoSuchAlgorithmException 
      | NoSuchProviderException 
      | NoSuchPaddingException 
      | InvalidAlgorithmParameterException e
  {
    System.out.println("DecryptorTest: Error: cannot encrypt user password! Exception="+e.toString());
    return;
  }
  System.out.println("DecryptorTest: Personal password (encripted, hex) =" + passwordh);
  
  md5.reset();
  try {
    md5.update(password0.getBytes("UTF-8")0, password0.getBytes("UTF-8").length);
  catch (UnsupportedEncodingException e) {
    System.out.println("DecryptorTest: Error: cannot prepare password (no UTF-8 encoding)");
    return;
  }
  byte[] pwd5 = md5.digest();
  //System.out.println("DecryptorTest: Personal password (MD5, hex) =" + password5);
  
  int keysize;
  keysize = 32// Cipher.getMaxAllowedKeyLength("Twofish/CBC/NoPadding");
  System.out.println("DecryptorTest: maximum key size ="+keysize );
  
  URL url;
  try {
    url = new URL(surl);
  catch (MalformedURLException e) {
    System.out.println("DecryptorTest: Error: Malformed URL");
    return;
  }
    HttpURLConnection conn;
  try {
    conn = (HttpURLConnectionurl.openConnection();
    String post;
    
    post = "user=shestero";
    post+= "&password="+passwordh;
    
    conn.setDoOutput(true)// мы будем писать POST данные
    conn.setDoInput(true);

    OutputStreamWriter out =
        new OutputStreamWriterconn.getOutputStream()"UTF-8" );
    out.write(post);
    // out.write("\r\n"); // перевод строки попадает в значения, передаваемые POST-ом
    out.close();
    
    PushbackInputStream stream = new PushbackInputStreamconn.getInputStream());
    int i0 = stream.read();
    if (i0<0)
    {
      System.out.println("DecryptorTest: Warning: empty reply!");
    }
    else
    {
      stream.unread(i0);
      
      InflaterInputStream inf = null;
      if (i0==0)
      {
        // encrypted
        stream.skip(32)// skip header
        
        System.out.println("DecryptorTest: Note: data comes encrypted!");
        
        byte[] key2 = new byte[32];
        Arrays.fill(key2,(byte)0);
        int j=0;
        for (int i=0; i<keyc.length() || i<pwd5.length; i++)
        {
          if (i<keyc.length()) key2[j++]=keyc.toString().substring(i,i+1).getBytes()[0];
          if (i<pwd5.lengthkey2[j++]=pwd5[i];
          if (j>=keysize)
            break;
        }
        System.out.println("DecryptorTest: Personal key to decript reply =["+key2+"]");

        // TODO: Fix key size
        if (j<=16j=16else if (j<=24j=24else if (j<32j=32;
        System.out.println("DecryptorTest: key2.length="+j);
        Cipher cipher = createCipherCipher.DECRYPT_MODE, key2, iv );
        
        inf = new InflaterInputStream
            new CipherInputStreamstream, cipher )
            new Inflater(true// set 'nowrap' parameter to 'true' here 
            );
        // r = new BufferedReader( new InputStreamReader( new CipherInputStream( stream, cipher ) ) );
      }
      else
      {
        // plain
        System.out.println("DecryptorTest: Warning: data comes unencrypted!");

        inf = new InflaterInputStreamstream, new Inflater(true) );
        //r = new BufferedReader( new InputStreamReader( stream ) );
      }
      BufferedReader r = new BufferedReadernew InputStreamReaderinf ) );    
      
      // Чтение строка за строкой для проверки
      System.out.println("====[REPLY FROM SERVER:]===========================");
          String inputLine;
          while ((inputLine = r.readLine()) != null
          {
              System.out.println(inputLine);
          }
          r.close();
          System.out.println("====[SUCESS]=======================================");
    }          
    catch (IOException e) {
    System.out.println("DecryptorTest: Error: IOException; URL="+surl);
  catch InvalidKeyException
      |  NoSuchAlgorithmException
      |  NoSuchProviderException
      |  NoSuchPaddingException
      |  InvalidAlgorithmParameterException e
  {
    System.out.println("DecryptorTest: Error: cannot decode: Exception="+e.toString());
  
  
  System.out.println("DecryptorTest BYE")
}
Исходник обработан: Java2html

Tags: , , ,
Current Mood: busy