package qora.crypto;

import java.io.UnsupportedEncodingException;

import org.bouncycastle.crypto.CipherParameters;
import org.bouncycastle.crypto.engines.AESEngine;
import org.bouncycastle.crypto.modes.CBCBlockCipher;
import org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher;
import org.bouncycastle.crypto.params.KeyParameter;
import org.bouncycastle.crypto.params.ParametersWithIV;

public class AEScrypto {
	
	private static byte[] ivconst = new byte[]{6,4,3,8,1,2,1,2,7,2,3,8,5,7,1,1};

	public static byte[] buf;
	public static byte[] buf2;
	
	public static byte[] dataEncrypt(byte[] data, byte[] myPrivateKey, byte[] theirPublicKey)
	{
		byte[] encryptdata = null;
		
		byte[] SharedSecret = Ed25519.getSharedSecret(theirPublicKey, myPrivateKey);
	    
		try {
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		byte[] encryptdataandver = new byte[encryptdata.length + 1];
		System.arraycopy(encryptdata, 0, encryptdataandver, 1, encryptdata.length);
		encryptdataandver[0] = 1; //version crypto algo
				
		return encryptdataandver;
	}
	
	public static byte[] dataDecrypt(byte[] encryptdata, byte[] myPrivateKey, byte[] theirPublicKey)
	{
		byte[] decryptdata = null;
		
		byte[] SharedSecret = Ed25519.getSharedSecret(theirPublicKey, myPrivateKey);
	    
		//byte version = encryptdata[0];
		byte[] encryptdata2 = new byte[encryptdata.length - 1];
		System.arraycopy(encryptdata, 1, encryptdata2, 0, encryptdata.length-1);
		
		try {
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return decryptdata;
	}

	public static byte[] cipherData(PaddedBufferedBlockCipher cipher, byte[] data)
		throws Exception 
	{
		int minSize = cipher.getOutputSize(data.length);
		
		byte[] outBuf = new byte[minSize];
		
		int length1 = cipher.processBytes(data, 0, data.length, outBuf, 0);
		
		int length2 = cipher.doFinal(outBuf, length1);
		
		int actualLength = length1 + length2;
		
		byte[] result = new byte[actualLength];
		
		System.arraycopy(outBuf, 0, result, 0, result.length);
		
		return result;
	}

	public static byte[] aesDecrypt(byte[] cipher, byte[] key, byte[] iv)
			throws Exception 
	{

		PaddedBufferedBlockCipher aes = new PaddedBufferedBlockCipher(
        new CBCBlockCipher(new AESEngine()));

		CipherParameters ivAndKey = new ParametersWithIV(new KeyParameter(key), iv);

		aes.init(false, ivAndKey);

		return cipherData(aes, cipher);
	}

	public static byte[] aesEncrypt(byte[] plain, byte[] key, byte[] iv) throws Exception 
	{
		PaddedBufferedBlockCipher aes = new PaddedBufferedBlockCipher(
				new CBCBlockCipher(
				new AESEngine()));
		CipherParameters ivAndKey = new ParametersWithIV(new KeyParameter(key), iv);

		aes.init(true, ivAndKey);

		return cipherData(aes, plain);
	}
}
@@ -19,54 +19,8 @@
 o;%0A%0A
-import java.io.UnsupportedEncodingException;%0A%0A
 impo
@@ -453,65 +453,8 @@
 %7D;%0A%0A
-%09public static byte%5B%5D buf;%0A%09public static byte%5B%5D buf2;%0A%09%0A
 %09pub
@@ -659,20 +659,146 @@
 );%0A%09
-    %0A%09%09try %7B
+%09byte%5B%5D password = Crypto.getInstance().digest(SharedSecret);  // SHA-256%0A%09%09%0A%09%09try %7B%0A%09%09%09encryptdata = aesEncrypt(data, password, ivconst);
 %0A%09%09%7D
@@ -1082,18 +1082,16 @@
  algo%0A%09%09
-%09%09
 %0A%09%09retur
@@ -1329,20 +1329,92 @@
 eKey);%0A%09
-    
+%09byte%5B%5D password = Crypto.getInstance().digest(SharedSecret);  // SHA-256%0A%09%09
 %0A%09%09//byt
@@ -1585,16 +1585,78 @@
 %09%09try %7B%0A
+%09%09%09decryptdata = aesDecrypt(encryptdata2, password, ivconst);%0A
 %09%09%7D catc

fix bug in last commit