refactor encryption/decryption to Render class
This commit is contained in:
parent
34056b9ab8
commit
cdbb309f4d
|
@ -1,15 +1,11 @@
|
|||
package org.surfsite.android.secretshare;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.app.AlertDialog;
|
||||
import android.app.Fragment;
|
||||
import android.content.DialogInterface;
|
||||
import android.graphics.Bitmap;
|
||||
import android.net.Uri;
|
||||
import android.os.AsyncTask;
|
||||
import android.os.Bundle;
|
||||
import android.support.v4.print.PrintHelper;
|
||||
import android.util.Base64;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
|
@ -19,9 +15,8 @@ import android.widget.TextView;
|
|||
import com.tiemens.secretshare.engine.SecretShare;
|
||||
import com.tiemens.secretshare.engine.SecretShare.ShareInfo;
|
||||
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.math.BigInteger;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
|
||||
|
@ -66,17 +61,6 @@ public class GenerateFragment extends Fragment implements FragmentSupport {
|
|||
return fragment;
|
||||
}
|
||||
|
||||
static BigInteger stringToBigInteger(String in) {
|
||||
BigInteger bigint;
|
||||
try {
|
||||
bigint = new BigInteger(in.getBytes("UTF-8"));
|
||||
} catch (UnsupportedEncodingException ex) {
|
||||
ex.printStackTrace();
|
||||
bigint = BigInteger.ZERO;
|
||||
}
|
||||
return bigint;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
|
@ -163,6 +147,8 @@ public class GenerateFragment extends Fragment implements FragmentSupport {
|
|||
|
||||
private class GenerateSharesTask extends AsyncTask<Void, Void, Void> {
|
||||
private final Activity activity;
|
||||
SecretShare.PublicInfo pi = null;
|
||||
ShareInfo si[];
|
||||
private TextView tv;
|
||||
private List<SecretShare.ShareInfo> pieces;
|
||||
private int n;
|
||||
|
@ -179,6 +165,7 @@ public class GenerateFragment extends Fragment implements FragmentSupport {
|
|||
this.n = n;
|
||||
this.k = k;
|
||||
this.cleartext = cleartext;
|
||||
si = new ShareInfo[n];
|
||||
this.tv.setText("Generating shared secrets for " + cleartext.length()
|
||||
+ " chars. Please Wait. This can take a long time.");
|
||||
}
|
||||
|
@ -189,7 +176,7 @@ public class GenerateFragment extends Fragment implements FragmentSupport {
|
|||
|
||||
@Override
|
||||
protected Void doInBackground(Void... params) {
|
||||
final BigInteger secretInteger = stringToBigInteger(cleartext);
|
||||
final BigInteger secretInteger = Renderer.stringToBigInteger(cleartext);
|
||||
final BigInteger modulus;
|
||||
|
||||
modulus = SecretShare.createAppropriateModulusForSecret(secretInteger);
|
||||
|
@ -211,32 +198,20 @@ public class GenerateFragment extends Fragment implements FragmentSupport {
|
|||
for (int i = 0; i < out.length; i++) {
|
||||
final ShareInfo piece = pieces.get(i);
|
||||
|
||||
byte[] bmodulus = publicInfo.getPrimeModulus().toByteArray();
|
||||
byte[] bshare = piece.getShare().toByteArray();
|
||||
int blen = 4 + 4 + 4 + 4 + 4 + bmodulus.length + bshare.length;
|
||||
final ByteBuffer bencoded = ByteBuffer.allocate(blen);
|
||||
bencoded.putInt(n).putInt(k).putInt(piece.getX());
|
||||
bencoded.putInt(bmodulus.length).put(bmodulus);
|
||||
bencoded.putInt(bshare.length).put(bshare);
|
||||
final String data = Renderer.encodeShareInfo(piece);
|
||||
|
||||
tv.append("Length: " + blen + "\n");
|
||||
|
||||
out[i] = n + ":" + k + ":" + piece.getX() + ":"
|
||||
out[i] = "ssss-android:" + piece.getX() + "/" + k + ":" + n + "-"
|
||||
+ publicInfo.getDescription()
|
||||
+ publicInfo.getPrimeModulus() + ":" + piece.getShare();
|
||||
|
||||
tv.append("OutLen: " + out[i].length() + "\n");
|
||||
tv.append(out[i] + "\n");
|
||||
tv.append(bencoded + "\n0x");
|
||||
for (byte b : bencoded.array()) {
|
||||
tv.append(String.format("%x", b));
|
||||
}
|
||||
tv.append("\n");
|
||||
String bencoded64 = Base64.encodeToString(bencoded.array(), Base64.DEFAULT);
|
||||
tv.append("B64Len: " + bencoded64.length() + "\n");
|
||||
tv.append(bencoded64 + "\n");
|
||||
tv.append(bencoded + "\n\n");
|
||||
tv.append("B64Len: " + data.length() + "\n");
|
||||
tv.append(data + "\n");
|
||||
if (pi == null)
|
||||
pi = Renderer.decodePublicInfo(data);
|
||||
|
||||
final String data = "ssss-android:" + bencoded64;
|
||||
si[n] = Renderer.decodeShareInfo(data, pi);
|
||||
/*
|
||||
if (i == 0) {
|
||||
View view = activity.getLayoutInflater().inflate(R.layout.address_qr, null);
|
||||
final TextView tv = (TextView) view.findViewById(R.id.secret_text);
|
||||
|
@ -259,8 +234,14 @@ public class GenerateFragment extends Fragment implements FragmentSupport {
|
|||
|
||||
builder.show();
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
List<ShareInfo> li = new ArrayList<ShareInfo>();
|
||||
li.add(si[0]);
|
||||
li.add(si[1]);
|
||||
li.add(si[2]);
|
||||
final SecretShare.CombineOutput combineOutput = new SecretShare(pi)
|
||||
.combine(li);
|
||||
this.finished = true;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -9,12 +9,17 @@ import android.graphics.Rect;
|
|||
import android.os.AsyncTask;
|
||||
import android.support.v4.print.PrintHelper;
|
||||
import android.text.TextPaint;
|
||||
import android.util.Base64;
|
||||
|
||||
import com.google.zxing.BarcodeFormat;
|
||||
import com.google.zxing.EncodeHintType;
|
||||
import com.google.zxing.aztec.AztecWriter;
|
||||
import com.google.zxing.common.BitMatrix;
|
||||
import com.tiemens.secretshare.engine.SecretShare;
|
||||
|
||||
import java.math.BigInteger;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.security.InvalidParameterException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Hashtable;
|
||||
|
||||
|
@ -180,4 +185,87 @@ public class Renderer {
|
|||
return lines;
|
||||
}
|
||||
|
||||
static BigInteger stringToBigInteger(String in) {
|
||||
return new BigInteger(in.getBytes());
|
||||
}
|
||||
|
||||
public static String encodeShareInfo(final SecretShare.ShareInfo piece) {
|
||||
final SecretShare.PublicInfo publicInfo = piece.getPublicInfo();
|
||||
final byte[] bytePrimeModulus = publicInfo.getPrimeModulus().toByteArray();
|
||||
final byte[] byteShare = piece.getShare().toByteArray();
|
||||
final byte[] byteDescription = publicInfo.getDescription().getBytes();
|
||||
final int byteLen = 4 + 4 + 4
|
||||
+ (4 + byteDescription.length)
|
||||
+ (4 + bytePrimeModulus.length)
|
||||
+ (4 + byteShare.length);
|
||||
final ByteBuffer byteBuffer = ByteBuffer.allocate(byteLen);
|
||||
byteBuffer.putInt(piece.getX());
|
||||
byteBuffer.putInt(publicInfo.getK());
|
||||
byteBuffer.putInt(publicInfo.getN());
|
||||
byteBuffer.putInt(byteDescription.length).put(byteDescription);
|
||||
byteBuffer.putInt(bytePrimeModulus.length).put(bytePrimeModulus);
|
||||
byteBuffer.putInt(byteShare.length).put(byteShare);
|
||||
final String byteEncoded64 = Base64.encodeToString(byteBuffer.array(), Base64.DEFAULT);
|
||||
|
||||
return "ssss-android:" + piece.getX() + "/" + publicInfo.getK()
|
||||
+ ":" + publicInfo.getN() + "=" + byteEncoded64;
|
||||
}
|
||||
|
||||
public static SecretShare.PublicInfo decodePublicInfo(final String buf) {
|
||||
int index64 = buf.indexOf("=") + 1;
|
||||
String substr = buf.substring(index64);
|
||||
final byte[] decodeBytes = Base64.decode(substr, Base64.DEFAULT);
|
||||
final ByteBuffer byteBuffer = ByteBuffer.wrap(decodeBytes);
|
||||
int x = byteBuffer.getInt();
|
||||
int k = byteBuffer.getInt();
|
||||
int n = byteBuffer.getInt();
|
||||
int byteDescriptionLength = byteBuffer.getInt();
|
||||
final byte[] byteDescription = new byte[byteDescriptionLength];
|
||||
byteBuffer.get(byteDescription);
|
||||
int bytePrimeModulusLength = byteBuffer.getInt();
|
||||
final byte[] bytePrimeModulus = new byte[bytePrimeModulusLength];
|
||||
byteBuffer.get(bytePrimeModulus);
|
||||
BigInteger inPrimeModulus = new BigInteger(bytePrimeModulus);
|
||||
return new SecretShare.PublicInfo(n, k, inPrimeModulus, new String(byteDescription));
|
||||
}
|
||||
|
||||
public static SecretShare.ShareInfo decodeShareInfo(final String buf, final SecretShare.PublicInfo publicInfo) throws InvalidParameterException {
|
||||
int index64 = buf.indexOf("=") + 1;
|
||||
final ByteBuffer byteBuffer = ByteBuffer.wrap(Base64.decode(buf.substring(index64), Base64.DEFAULT));
|
||||
int x = byteBuffer.getInt();
|
||||
int k = byteBuffer.getInt();
|
||||
int n = byteBuffer.getInt();
|
||||
|
||||
if (n != publicInfo.getN()) {
|
||||
throw new InvalidParameterException("SecretShare.PublicInfo.N does not match.");
|
||||
}
|
||||
|
||||
if (k != publicInfo.getK()) {
|
||||
throw new InvalidParameterException("SecretShare.PublicInfo.K does not match.");
|
||||
}
|
||||
|
||||
if (x > n) {
|
||||
throw new InvalidParameterException("SecretShare x > n.");
|
||||
}
|
||||
|
||||
int byteDescriptionLength = byteBuffer.getInt();
|
||||
final byte[] byteDescription = new byte[byteDescriptionLength];
|
||||
byteBuffer.get(byteDescription);
|
||||
if (publicInfo.getDescription().compareTo(new String(byteDescription)) != 0) {
|
||||
throw new InvalidParameterException("SecretShare.PublicInfo.Description does not match.");
|
||||
}
|
||||
int bytePrimeModulusLength = byteBuffer.getInt();
|
||||
final byte[] bytePrimeModulus = new byte[bytePrimeModulusLength];
|
||||
byteBuffer.get(bytePrimeModulus);
|
||||
BigInteger inPrimeModulus = new BigInteger(bytePrimeModulus);
|
||||
if (inPrimeModulus.compareTo(publicInfo.getPrimeModulus()) != 0) {
|
||||
throw new InvalidParameterException("SecretShare.PublicInfo.PrimeModulus does not match.");
|
||||
}
|
||||
int byteShareLength = byteBuffer.getInt();
|
||||
final byte[] byteShare = new byte[byteShareLength];
|
||||
byteBuffer.get(byteShare);
|
||||
BigInteger inShare = new BigInteger(byteShare);
|
||||
return new SecretShare.ShareInfo(x, inShare, publicInfo);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue