import bin file

This commit is contained in:
Henry Varona 2016-12-01 23:19:52 -04:00
parent f7f2952769
commit 7e929b71ad
4 changed files with 57 additions and 79 deletions

View file

@ -5,16 +5,12 @@ import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.luminiasoft.bitshares.crypto.AndroidRandomSource;
import com.luminiasoft.bitshares.crypto.SecureRandomStrengthener;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.Arrays;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.bitcoinj.core.ECKey;
import org.spongycastle.crypto.DataLengthException;
import org.spongycastle.crypto.InvalidCipherTextException;
@ -23,9 +19,6 @@ import org.spongycastle.crypto.modes.CBCBlockCipher;
import org.spongycastle.crypto.paddings.PaddedBufferedBlockCipher;
import org.spongycastle.crypto.params.KeyParameter;
import org.spongycastle.crypto.params.ParametersWithIV;
import org.tukaani.xz.LZMA2Options;
import org.tukaani.xz.LZMAInputStream;
import org.tukaani.xz.LZMAOutputStream;
/**
* Class to manage the Bin Files
@ -45,10 +38,11 @@ public abstract class FileBin {
public static String getBrainkeyFromByte(byte[] input, String password) {
try {
byte[] publicKey = new byte[33];
byte[] rawData = new byte[input.length-33];
byte[] rawData = new byte[input.length - 33];
System.arraycopy(input, 0, publicKey, 0, publicKey.length);
System.arraycopy(input, 33, rawData, 0, rawData.length);
MessageDigest md = MessageDigest.getInstance("SHA-256");
ECKey randomECKey = ECKey.fromPublicOnly(publicKey);
@ -59,20 +53,34 @@ public abstract class FileBin {
byte[] checksum = new byte[4];
System.arraycopy(rawData, 0, checksum, 0, 4);
byte[] compressedData = new byte[rawData.length-4];
byte[] compressedData = new byte[rawData.length - 4];
System.arraycopy(rawData, 4, compressedData, 0, compressedData.length);
byte[] wallet_object_bytes = Util.decompress(compressedData);
String wallet_string = byteToString(wallet_object_bytes);
String wallet_string = new String(wallet_object_bytes, "UTF-8");
JsonObject wallet = new JsonParser().parse(wallet_string).getAsJsonObject();
byte[] encKey_enc = wallet.get("encryption_key").getAsString().getBytes();
byte[] encKey = decryptAES(encKey_enc, password.getBytes("UTF-8"));
byte[] encBrain = wallet.get("encrypted_brainkey").getAsString().getBytes();
String BrainKey = byteToString(decryptAES(encBrain, encKey));
if (wallet.get("wallet").isJsonArray()) {
wallet = wallet.get("wallet").getAsJsonArray().get(0).getAsJsonObject();
} else {
wallet = wallet.get("wallet").getAsJsonObject();
}
byte[] encKey_enc = new BigInteger(wallet.get("encryption_key").getAsString(), 16).toByteArray();
byte[] temp = new byte[encKey_enc.length - (encKey_enc[0] == 0 ? 1 : 0)];
System.arraycopy(encKey_enc, (encKey_enc[0] == 0 ? 1 : 0), temp, 0, temp.length);
byte[] encKey = decryptAES(temp, password.getBytes("UTF-8"));
temp = new byte[encKey.length - 16];
System.arraycopy(encKey, 0, temp, 0, temp.length);
byte[] encBrain = new BigInteger(wallet.get("encrypted_brainkey").getAsString(), 16).toByteArray();
while(encBrain[0] == 0){
byte[]temp2 = new byte[encBrain.length-1];
System.arraycopy(encBrain, 1, temp2, 0, temp2.length);
encBrain = temp2;
}
String BrainKey = new String((decryptAES(encBrain, temp)), "UTF-8");
return BrainKey;
} catch (UnsupportedEncodingException | NoSuchAlgorithmException ex){
} catch (UnsupportedEncodingException | NoSuchAlgorithmException ex) {
}
//Creates cypher AES with password
@ -138,32 +146,6 @@ public abstract class FileBin {
return null;
}
public static byte[] compressDataLZMA(byte[] inputBytes) {
LZMAOutputStream out = null;
try {
ByteArrayInputStream input = new ByteArrayInputStream(inputBytes);
ByteArrayOutputStream output = new ByteArrayOutputStream(2048);
LZMA2Options options = new LZMA2Options();
out = new LZMAOutputStream(output, options, -1);
byte[] buf = new byte[inputBytes.length];
int size;
while ((size = input.read(buf)) != -1) {
out.write(buf, 0, size);
}
out.finish();
return output.toByteArray();
} catch (IOException ex) {
Logger.getLogger(FileBin.class.getName()).log(Level.SEVERE, null, ex);
} finally {
try {
out.close();
} catch (IOException ex) {
Logger.getLogger(FileBin.class.getName()).log(Level.SEVERE, null, ex);
}
}
return null;
}
private static byte[] encryptAES(byte[] input, byte[] key) {
try {
MessageDigest md = MessageDigest.getInstance("SHA-512");
@ -203,24 +185,24 @@ public abstract class FileBin {
cipher.doFinal(out, proc);
//Unpadding
int count = out[out.length-1];
int count = out[out.length - 1];
byte[] temp = new byte[count];
System.arraycopy(out, out.length-count, temp, 0, temp.length);
System.arraycopy(out, out.length - count, temp, 0, temp.length);
byte[] temp2 = new byte[count];
Arrays.fill(temp2, (byte)count);
if (Arrays.equals(temp, temp2)){
temp = new byte[out.length-count];
System.arraycopy(out, 0, temp, 0, out.length-count);
Arrays.fill(temp2, (byte) count);
if (Arrays.equals(temp, temp2)) {
temp = new byte[out.length - count];
System.arraycopy(out, 0, temp, 0, out.length - count);
return temp;
} else {
return out;
}
} catch (NoSuchAlgorithmException | DataLengthException | IllegalStateException | InvalidCipherTextException ex) {
ex.printStackTrace();
}
return null;
}
public static String byteToString(byte[] input) {
StringBuilder result = new StringBuilder();
for (byte in : input) {

View file

@ -38,7 +38,7 @@ public class Main {
// e.printStackTrace();
// }
// test.testCustomSerializer();
test.testUserAccountSerialization();
//test.testUserAccountSerialization();
// test.testTransactionSerialization();
// test.testLoginSerialization();
// test.testNetworkBroadcastSerialization();
@ -59,6 +59,6 @@ public class Main {
// test.testRelativeAccountHistory();
// test.testingInvoiceGeneration();
// test.testCompression();
// test.testCreateBinFile();
test.testCreateBinFile();
}
}

View file

@ -12,20 +12,15 @@ import com.luminiasoft.bitshares.ws.*;
import com.neovisionaries.ws.client.*;
import org.bitcoinj.core.*;
import org.spongycastle.crypto.Digest;
import org.spongycastle.crypto.digests.RIPEMD128Digest;
import org.spongycastle.crypto.digests.RIPEMD160Digest;
import org.spongycastle.crypto.digests.SHA512Digest;
import org.spongycastle.crypto.prng.DigestRandomGenerator;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLServerSocketFactory;
import javax.net.ssl.SSLSocketFactory;
import java.io.*;
import java.lang.reflect.Type;
import java.net.URL;
import java.nio.file.Paths;
import java.math.BigInteger;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.logging.Level;
@ -333,7 +328,7 @@ public class Test {
System.out.println(jsonAmount);
}
public void testUserAccountSerialization(){
public void testUserAccountSerialization() {
UserAccount account = new UserAccount("1.2.138632");
System.out.println(Util.bytesToHex(account.toBytes()));
}
@ -458,7 +453,7 @@ public class Test {
} catch (WebSocketException e) {
System.out.println("WebSocketException. Msg: " + e.getMessage());
} catch (NoSuchAlgorithmException e) {
System.out.println("NoSuchAlgoritmException. Msg: "+e.getMessage());
System.out.println("NoSuchAlgoritmException. Msg: " + e.getMessage());
}
}
@ -710,14 +705,15 @@ public class Test {
System.out.println("NoSuchAlgorithmException. Msg: " + e.getMessage());
}
}
public void testingInvoiceGeneration(){
Invoice.LineItem[] lineItem = new Invoice.LineItem[] { new Invoice.LineItem("Apples", 2, "20 CSD")};
public void testingInvoiceGeneration() {
Invoice.LineItem[] lineItem = new Invoice.LineItem[]{new Invoice.LineItem("Apples", 2, "20 CSD")};
Invoice invoice = new Invoice("bilthon-83", "Bilthon's store", "Invoice #12", "BTS", lineItem, "Thank you", "");
String qrCodeData = Invoice.toQrCode(invoice);
System.out.println("qrCodeData");
System.out.println(qrCodeData);
Invoice recovered = Invoice.fromQrCode(qrCodeData);
System.out.println("recovered invoice: "+recovered.toJsonString());
System.out.println("recovered invoice: " + recovered.toJsonString());
}
public void testCompression() {
@ -729,8 +725,10 @@ public class Test {
System.out.println(Util.bytesToHex(compressed));
}
public void testCreateBinFile(){
byte[] fileOutput = FileBin.getBytesFromBrainKey(Main.BRAIN_KEY, "123456","bithon-83");
public void testCreateBinFile() {
byte[] fileOutput = FileBin.getBytesFromBrainKey(Main.BRAIN_KEY, "123456", "bithon-83");
String stringFile = "02f9f3eb0f61a0a96134975d86048bf92e114d6a1ce286140cad3a96c33e697282bc0a8a24d1ad0c7bc084a79816ce38e36bd2d624aa8bf686f53fb4c7e25e3974da9b40e0b17e9d0b5b82793a04b19646169c49c58cd67f4950aee7d275141dd24f52baaaee772995a9bd6a6562a7a38aae08951236d3f612aecef7aedd720a91eacbab3a792ca3ebe0105838fe11f6e9d0e83e5d77eb82f17c7ba85c670e69294a8bcf8365cfeca487a60093498496bbec394c729e3fda9f32fdccdea56288b36fb14a26aa309b548a6dd9c1d616d22167348f8d580f9dc7361b4457d2dc6d75ec985d8e2d3dcdff89cd425d9f14037ac961eb10ac5f92bab356ccecd8cf018ec05ab40d915b628a75ae32cfa4005634f08b24c0dc8c5a7636ed70cbd86a7f0c4f6236d74310470fafe3af8b5346c8cb61957f7292b468d276498f9e806399588b0afd5777e6ee5fe7cd3a6691d9b5486cb5c7adbd5ad0b17588dd32d82b01d49ecf0f2bf24ee54a490ee620e8ab049047ffa416b5efa8f1f0155d8f1be866a10d0d62ae44a3a8ecc0121c08837c2ee1a25f8b6dd7266273c41f4b9a5e3d600e3fb4de870f99ab1a7196d93f222595f92e97a2480f58b61b62639154a374b987664fd317622aaad156f831b03f2d9606537b65b3b1fcfb1fb6be39560ad2c301dd1fc25cee755e61b49ebfe42ca7e64b4b0fc4aa347b48a85c0b585a3499fe278e25cb2141f8009b9afc875fa2a2c439bf6cdec4b5190a6deb7f9390f072beb24749a8a2114cc1870c07be079abb3ee0ebc827f9b53e158a529bc6552eba280f05edf5f7ae1911de7acb4888150a509d029ec7c9da6de8adabbca6773a0a293a0a42de8278c82e88b9390b42b56f58bd8633fb97130e799a47a744e2e8958fd5";
fileOutput = new BigInteger(stringFile, 16).toByteArray();
System.out.println(FileBin.getBrainkeyFromByte(fileOutput, "123456"));
}
}

View file

@ -1,14 +1,12 @@
package com.luminiasoft.bitshares;
import org.tukaani.xz.LZMA2Options;
import org.tukaani.xz.LZMAInputStream;
import org.tukaani.xz.LZMAOutputStream;
import java.io.ByteArrayInputStream;
import org.tukaani.xz.LZMA2Options;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.tukaani.xz.XZInputStream;
import org.tukaani.xz.XZOutputStream;
/**
@ -77,11 +75,11 @@ public class Util {
* @author Henry Varona
*/
public static byte[] decompress(byte[] inputBytes) {
LZMAInputStream in = null;
XZInputStream in = null;
try {
ByteArrayInputStream input = new ByteArrayInputStream(inputBytes);
ByteArrayOutputStream output = new ByteArrayOutputStream(2048);
in = new LZMAInputStream(input);
in = new XZInputStream(input);
int size;
while ((size = in.read()) != -1) {
output.write(size);