|
|
@@ -5,26 +5,37 @@ import android.support.annotation.NonNull;
|
|
|
|
|
|
import org.bouncycastle.bcpg.ArmoredOutputStream;
|
|
|
import org.bouncycastle.bcpg.CompressionAlgorithmTags;
|
|
|
+import org.bouncycastle.bcpg.HashAlgorithmTags;
|
|
|
+import org.bouncycastle.bcpg.sig.KeyFlags;
|
|
|
+import org.bouncycastle.crypto.generators.RSAKeyPairGenerator;
|
|
|
+import org.bouncycastle.crypto.params.RSAKeyGenerationParameters;
|
|
|
import org.bouncycastle.openpgp.PGPCompressedData;
|
|
|
import org.bouncycastle.openpgp.PGPCompressedDataGenerator;
|
|
|
import org.bouncycastle.openpgp.PGPEncryptedData;
|
|
|
import org.bouncycastle.openpgp.PGPEncryptedDataGenerator;
|
|
|
import org.bouncycastle.openpgp.PGPEncryptedDataList;
|
|
|
import org.bouncycastle.openpgp.PGPException;
|
|
|
+import org.bouncycastle.openpgp.PGPKeyPair;
|
|
|
+import org.bouncycastle.openpgp.PGPKeyRingGenerator;
|
|
|
import org.bouncycastle.openpgp.PGPLiteralData;
|
|
|
import org.bouncycastle.openpgp.PGPLiteralDataGenerator;
|
|
|
import org.bouncycastle.openpgp.PGPObjectFactory;
|
|
|
import org.bouncycastle.openpgp.PGPPBEEncryptedData;
|
|
|
import org.bouncycastle.openpgp.PGPPrivateKey;
|
|
|
+import org.bouncycastle.openpgp.PGPPublicKey;
|
|
|
import org.bouncycastle.openpgp.PGPPublicKeyEncryptedData;
|
|
|
import org.bouncycastle.openpgp.PGPSecretKey;
|
|
|
import org.bouncycastle.openpgp.PGPSecretKeyRing;
|
|
|
import org.bouncycastle.openpgp.PGPSecretKeyRingCollection;
|
|
|
+import org.bouncycastle.openpgp.PGPSignature;
|
|
|
+import org.bouncycastle.openpgp.PGPSignatureSubpacketGenerator;
|
|
|
import org.bouncycastle.openpgp.PGPUtil;
|
|
|
import org.bouncycastle.openpgp.operator.PBESecretKeyDecryptor;
|
|
|
import org.bouncycastle.openpgp.operator.PBESecretKeyEncryptor;
|
|
|
import org.bouncycastle.openpgp.operator.bc.BcPBEDataDecryptorFactory;
|
|
|
+import org.bouncycastle.openpgp.operator.bc.BcPGPContentSignerBuilder;
|
|
|
import org.bouncycastle.openpgp.operator.bc.BcPGPDigestCalculatorProvider;
|
|
|
+import org.bouncycastle.openpgp.operator.bc.BcPGPKeyPair;
|
|
|
import org.bouncycastle.openpgp.operator.bc.BcPublicKeyDataDecryptorFactory;
|
|
|
import org.bouncycastle.openpgp.operator.jcajce.JcaKeyFingerprintCalculator;
|
|
|
import org.bouncycastle.openpgp.operator.jcajce.JcaPGPDigestCalculatorProviderBuilder;
|
|
|
@@ -42,6 +53,7 @@ import java.io.FileNotFoundException;
|
|
|
import java.io.IOException;
|
|
|
import java.io.InputStream;
|
|
|
import java.io.OutputStream;
|
|
|
+import java.math.BigInteger;
|
|
|
import java.security.SecureRandom;
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.Date;
|
|
|
@@ -349,8 +361,38 @@ public class GPGUtil {
|
|
|
return "";
|
|
|
}
|
|
|
|
|
|
- public static void Generate(final OutputStream out) {
|
|
|
- // FIXME
|
|
|
+ public static byte[] Generate(String user) {
|
|
|
+ try {
|
|
|
+ final Date now = new Date();
|
|
|
+ RSAKeyPairGenerator rsakeygen = new RSAKeyPairGenerator();
|
|
|
+ rsakeygen.init(new RSAKeyGenerationParameters(BigInteger.valueOf(257), new SecureRandom(), 2048, 12));
|
|
|
+ PGPKeyPair rsaEncr = new BcPGPKeyPair(PGPPublicKey.RSA_GENERAL, rsakeygen.generateKeyPair(), now);
|
|
|
+
|
|
|
+ PGPSignatureSubpacketGenerator encHashGen = new PGPSignatureSubpacketGenerator();
|
|
|
+ encHashGen.setKeyFlags(false, KeyFlags.ENCRYPT_COMMS | KeyFlags.ENCRYPT_STORAGE);
|
|
|
+
|
|
|
+ final PBESecretKeyEncryptor newEncryptor = new JcePBESecretKeyEncryptorBuilder(PGPEncryptedData.AES_256).setProvider("BC").build("".toCharArray());
|
|
|
+ PGPKeyRingGenerator keygen = new PGPKeyRingGenerator(
|
|
|
+ PGPSignature.POSITIVE_CERTIFICATION,
|
|
|
+ rsaEncr,
|
|
|
+ user,
|
|
|
+ new BcPGPDigestCalculatorProvider().get(HashAlgorithmTags.SHA1),
|
|
|
+ encHashGen.generate(),
|
|
|
+ null,
|
|
|
+ new BcPGPContentSignerBuilder(
|
|
|
+ rsaEncr.getPublicKey().getAlgorithm(),
|
|
|
+ HashAlgorithmTags.SHA1),
|
|
|
+ newEncryptor);
|
|
|
+ ArrayList<PGPSecretKeyRing> keyringCollection = new ArrayList<>();
|
|
|
+ keyringCollection.add(keygen.generateSecretKeyRing());
|
|
|
+ ByteArrayOutputStream stream = new ByteArrayOutputStream();
|
|
|
+ new PGPSecretKeyRingCollection(keyringCollection).encode(stream);
|
|
|
+ return stream.toByteArray();
|
|
|
+ }
|
|
|
+ catch (IOException | PGPException e) {
|
|
|
+ log.log(Level.SEVERE, "Cannot generate new key", e);
|
|
|
+ return null;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
public static boolean CheckIsPasswordProtected(Context ctx) throws IOException {
|