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 = encrypt( password0, 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 = (HttpURLConnection) url.openConnection(); String post; post = "user=shestero"; post+= "&password="+passwordh; conn.setDoOutput(true); // мы будем писать POST данные conn.setDoInput(true);
OutputStreamWriter out = new OutputStreamWriter( conn.getOutputStream(), "UTF-8" ); out.write(post); // out.write("\r\n"); // перевод строки попадает в значения, передаваемые POST-ом out.close(); PushbackInputStream stream = new PushbackInputStream( conn.getInputStream(), 1 ); 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.length) key2[j++]=pwd5[i]; if (j>=keysize) break; } System.out.println("DecryptorTest: Personal key to decript reply =["+key2+"]");
// TODO: Fix key size if (j<=16) j=16; else if (j<=24) j=24; else if (j<32) j=32; System.out.println("DecryptorTest: key2.length="+j); Cipher cipher = createCipher( Cipher.DECRYPT_MODE, key2, iv ); inf = new InflaterInputStream( new CipherInputStream( stream, 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 InflaterInputStream( stream, new Inflater(true) ); //r = new BufferedReader( new InputStreamReader( stream ) ); } BufferedReader r = new BufferedReader( new InputStreamReader( inf ) ); // Чтение строка за строкой для проверки 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"); } |