Data encryption using Base64 PublicKey & decryption using Base64 PrivateKey #5703
|
Data encryption & decryption works with Public & Private Keys in raw binary formats like these Changing keys to Base64 formats like these These Base64 keys formats are already working in backend. How can they also work in Codename One app? |
Replies: 3 comments 5 replies
|
The problem is that Rather than have you write the stripping code, I've added it to the API. On the next update you'll be able to write: InputStream publicStream = Display.getInstance().getResourceAsStream(MyApp.class, "/Base64TestPublicKey.pem");
PublicKey publicKey = PublicKey.fromPem(Util.readInputStream(publicStream));
InputStream privateStream = Display.getInstance().getResourceAsStream(MyApp.class, "/Base64TestPrivateKey.pem");
PrivateKey privateKey = PrivateKey.fromPem(Util.readInputStream(privateStream));The rest of your code can remain unchanged. In the meantime, on the current release you can strip the armor yourself. Note you can't just decode the whole file private static byte[] pemToDer(InputStream is) throws IOException {
String pem = new String(Util.readInputStream(is), "UTF-8");
StringBuilder b = new StringBuilder();
for (String line : Util.split(pem, "\n")) {
line = line.trim();
if (line.length() == 0 || line.startsWith("-----")) {
continue;
}
b.append(line);
}
return Base64.decode(b.toString().getBytes("UTF-8"));
}Then One thing to check on the private key if you go that route: it must be PKCS#8 ( |
|
We fixed certificates so they now work directly with no InputStream is = Display.getInstance().getResourceAsStream(MyApp.class, "/cert.pem");
PublicKey publicKey = PublicKey.fromPem(Util.readInputStream(is));
On the Cipher c = Cipher.getInstance("RSA/ECB/OAEPPadding");
c.init(Cipher.DECRYPT_MODE, privateKey, new OAEPParameterSpec(
"SHA-256", "MGF1", MGF1ParameterSpec.SHA256, PSource.PSpecified.DEFAULT));We can't follow the JCE default here. Web Crypto and Apple's SecKey both take one hash and apply it to both, so the split SHA-256/SHA-1 pairing can't be expressed on the iOS or JavaScript ports at all. I updated the developer guide next and the algorithm table. |
|
That's already in The One thing to change back: now that the backend names the parameters explicitly, revert |
That's already in
7.0.271is out, soPublicKey.fromPemnow reads the subject public key straight out of a-----BEGIN CERTIFICATE-----block with noopenssl x509 -pubkey -nooutstep. Bumpcn1.version/cn1.plugin.versionto7.0.271and the code in your first post works unchanged against the certificate file fromBase64Keys.zip. In a chain the leaf comes first, which is the key you want.The
DerValue.getOID, not an OID -96you hit with the hand-rolledpemToDerwas the same problem wearing a different message: -96 is 0xA0, the[0] EXPLICIT versiontag that opens aTBSCertificate. So the stripper was working correctly and handingPublicKey.rsaa whole certificate, where it expects a SubjectPu…