patternjavaMinor
Java program to encrypt files using Shamir Secret Sharing
Viewed 0 times
programjavafilesusingsecretencryptshamirsharing
Problem
Shamir's Secret Sharing scheme essentially splits a secret into n parts, at least k of which are needed to recover it. I'm using that to encrypt/decrypt arbitrary files (it's part of a college project).
Here's a rough idea of what I'm doing:
-
Take a file, and read N bytes. Treat those bytes as an integer, and encrypt it using this implementation of the Shamir algorithm. I get n integers, and I write one each to a file. Read N more bytes, and so on until I am done.
-
Similarly, when decrypting, take n files, read an integer from each (I write a byte to denote the length of an integer before each, so I know how many bytes to read), decrypt the n integers to get the original one, and convert it to bytes to get N bytes, and write them to a file, and then read more integers.
This is working, but is rather slow - with encrypting to 3 files a 170 MB file, it takes me 5 minutes to encrypt, 4 minutes to decrypt. How can I speed it up? Of course, any other suggestions are also welcome.
```
package crypto;
import com.tiemens.secretshare.main.cli.MainCombine;
import com.tiemens.secretshare.main.cli.MainSplit;
import java.io.*;
import java.math.BigInteger;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import static java.lang.Integer.min;
import static java.util.Arrays.copyOfRange;
/**
* Created by hooda on 2/3/2015.
*/
public class Shamir {
//The encoding that will be used when splitting and combining files.
static String encoding = "ISO-8859-1";
//The number of bytes per piece (except maybe the last one)!
static int pieceSize = 128;
//Mode 0 for strings, 1 for ints.
public static ArrayList shamirSplit(String inputString, int numPieces, int minPieces, int mode) {
String type = "-sS";
if (mode == 1) {
type = "-sN";
}
ArrayList parts = new ArrayList<>();
String[] splitArgs = {"-n", Intege
Here's a rough idea of what I'm doing:
-
Take a file, and read N bytes. Treat those bytes as an integer, and encrypt it using this implementation of the Shamir algorithm. I get n integers, and I write one each to a file. Read N more bytes, and so on until I am done.
-
Similarly, when decrypting, take n files, read an integer from each (I write a byte to denote the length of an integer before each, so I know how many bytes to read), decrypt the n integers to get the original one, and convert it to bytes to get N bytes, and write them to a file, and then read more integers.
This is working, but is rather slow - with encrypting to 3 files a 170 MB file, it takes me 5 minutes to encrypt, 4 minutes to decrypt. How can I speed it up? Of course, any other suggestions are also welcome.
```
package crypto;
import com.tiemens.secretshare.main.cli.MainCombine;
import com.tiemens.secretshare.main.cli.MainSplit;
import java.io.*;
import java.math.BigInteger;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import static java.lang.Integer.min;
import static java.util.Arrays.copyOfRange;
/**
* Created by hooda on 2/3/2015.
*/
public class Shamir {
//The encoding that will be used when splitting and combining files.
static String encoding = "ISO-8859-1";
//The number of bytes per piece (except maybe the last one)!
static int pieceSize = 128;
//Mode 0 for strings, 1 for ints.
public static ArrayList shamirSplit(String inputString, int numPieces, int minPieces, int mode) {
String type = "-sS";
if (mode == 1) {
type = "-sN";
}
ArrayList parts = new ArrayList<>();
String[] splitArgs = {"-n", Intege
Solution
You are probably doing it wrong. The scheme says it all: secret sharing. The algorithm is to share secrets rather than data. I would rather suggest you to encrypt the whole file using a symmetric key algorithm like AES, using a random key and then share the key using Shamir's. This is the preferred way as going your way is guaranteed to be time intensive.
For reconstruction, you may regenerate the secret key and reconstruct the file using it.
Shamir, in his original paper, "How to share a secret?" writes:
If the number D is long, it is advisable to break it into shorter blocks of bits (which are handled separately) in order to avoid muti precision arithmetic operations.
For reconstruction, you may regenerate the secret key and reconstruct the file using it.
Shamir, in his original paper, "How to share a secret?" writes:
If the number D is long, it is advisable to break it into shorter blocks of bits (which are handled separately) in order to avoid muti precision arithmetic operations.
Context
StackExchange Code Review Q#85926, answer score: 3
Revisions (0)
No revisions yet.