Witam.
Jestem w trakcie pisania aplikacji do generowania kluczy publicznych i prywatnych oraz szyfrowania wiadomości.
Do tego celu wykorzystałem bibliotekę Spongy Castle https://rtyley.github.io/spongycastle/.
Problem polega na tym, że klucze są błędnie generowane. Gdy chcę nimi zaszyfrować jakąś wiadomość to dostaję komunikat o błędzie:
"Can't use an RSA_SIGN key for encryption". Próbowałem również zaimportować je w gpg na komputerze i widać, że brakuje "uid" (screen niżej, pierwszy klucz jest poprawny)
Przy próbie szyfrowania wiadomości w na kompie gpg wyświetla komunikat: "ten klucz jest bezużyteczny".
Poniżej zamieszczam kod, który użyłem do generowania kluczy:
public final PGPKeyRingGenerator genRsaKeyRing(String id, char[] pass) {
try {
RSAKeyPairGenerator kpg = new RSAKeyPairGenerator();
// RSA KeyGen parameters
BigInteger publicExponent = BigInteger.valueOf(0x10001);
int certainty = 12;
RSAKeyGenerationParameters rsaKeyGenerationParameters = new RSAKeyGenerationParameters(publicExponent, new SecureRandom(), secpar, certainty);
kpg.init(rsaKeyGenerationParameters);
// generate master key (signing) and subkey (enc)
Date now = new Date();
PGPKeyPair kpSign = new BcPGPKeyPair(PGPPublicKey.RSA_SIGN, kpg.generateKeyPair(), now);
PGPKeyPair kpEnc = new BcPGPKeyPair(PGPPublicKey.RSA_ENCRYPT, kpg.generateKeyPair(), now);
// sign the master key packet
PGPSignatureSubpacketGenerator signSigPacket = new PGPSignatureSubpacketGenerator();
// metadata for master key
boolean isCritical = true;
int keyPurpose = KeyFlags.SIGN_DATA | KeyFlags.CERTIFY_OTHER;
signSigPacket.setKeyFlags(isCritical, keyPurpose);
int[] symAlgos = new int[] {SymmetricKeyAlgorithmTags.AES_256, SymmetricKeyAlgorithmTags.AES_192, SymmetricKeyAlgorithmTags.BLOWFISH, SymmetricKeyAlgorithmTags.CAST5};
signSigPacket.setPreferredSymmetricAlgorithms(isCritical, symAlgos);
int[] hashAlgos = new int[] {HashAlgorithmTags.SHA512, HashAlgorithmTags.SHA384, HashAlgorithmTags.SHA256};
signSigPacket.setPreferredHashAlgorithms(isCritical, hashAlgos);
signSigPacket.setFeature(isCritical, Features.FEATURE_MODIFICATION_DETECTION);
// sign encryption subkey
PGPSignatureSubpacketGenerator signEncPacket = new PGPSignatureSubpacketGenerator();
// metadata for subkey
keyPurpose = KeyFlags.ENCRYPT_COMMS | KeyFlags.ENCRYPT_STORAGE;
signEncPacket.setKeyFlags(isCritical, keyPurpose);
// digests
PGPDigestCalculator digest1 = new BcPGPDigestCalculatorProvider().get(HashAlgorithmTags.SHA1);
PGPDigestCalculator digest256 = new BcPGPDigestCalculatorProvider().get(HashAlgorithmTags.SHA256);
// encryption for secret key
PBESecretKeyEncryptor pske = (new BcPBESecretKeyEncryptorBuilder(PGPEncryptedData.AES_256, digest256, pwdCount)).build(pass);
// create the keyring
BcPGPContentSignerBuilder contentSignerBuilder = new BcPGPContentSignerBuilder(kpSign.getPublicKey().getAlgorithm(), HashAlgorithmTags.SHA256);
rsaKr = new PGPKeyRingGenerator(PGPSignature.POSITIVE_CERTIFICATION, kpSign, id, digest1, signSigPacket.generate(), null, contentSignerBuilder, pske);
// encryption subkey
rsaKr.addSubKey(kpEnc, signEncPacket.generate(), null);
} catch (PGPException e){
e.printStackTrace();
}
return rsaKr;
}
public void writeKeys(){
String iconsStoragePath = Environment.getExternalStorageDirectory() + "/myKeys2/";
File sdIconStorageDir = new File(iconsStoragePath);
sdIconStorageDir.mkdir();
try{
String filePath = sdIconStorageDir.toString() + "pgpPublicKeyRing.pkr";
FileOutputStream fileOutputStream = new FileOutputStream(filePath);
BufferedOutputStream out = new BufferedOutputStream(fileOutputStream);
rsaKr.generatePublicKeyRing().encode(out);
out.flush();
out.close();
String filePath2 = sdIconStorageDir.toString() + "pgpSecretKeyRing.skr";
FileOutputStream fileOutputStream2 = new FileOutputStream(filePath2);
BufferedOutputStream out2 = new BufferedOutputStream(fileOutputStream2);
rsaKr.generateSecretKeyRing().encode(out2);
out2.close();
} catch (IOException e){
e.printStackTrace();
}
}
Czy ktoś wie, czego mi tu może brakować?