https://docs.oracle.com/en/java/javase/11/security/index.html describes a Java interface but does not specify the default algorithms for things like RSA encryption. I need to interface with code using the Java 11 java.security model for PK encryption using RSA on a linux server.
From what I can tell, Java uses PKCS v2.2 and probably uses sha256 (though they don't say). All of the examples on Oracle's site describe interfacing to java.security from Java only; most of what I can find on the net (or here) is quite old or doesn't actually describe the non-Java interface.
Q: Does anyone know of any working examples for using another language (preferably Perl or C) to interface with the Java-11 java.security library for PK encryption using RSA from linux?
If I can find any reference to a working library that will interface properly with Java-11's java.security I'll at least have working specs to start from -- or just interface directly to it with Inline.
Perl's Crypt::PK::RSA uses TomCrypt (https://github.com/libtom/libtomcrypt). This is a nice C implementation but it uses a v1.5 key padding, which isn't going to work with Java using v2.2. If I can find a working C v2.2 implementation I can try and graft it into TomCrypt; that or just a working stand-along RSA encryption algorithm in C that uses v2.2.
At this point I've tried using Crypt::PK::RSA with all of the available options for v1.5 & oeap using sha1 & sha256 to reproduce encrypting a given string to known ciphertext using both high- and low-endian transform to hex (via unpack H* and h*) without being able to reproduce the ciphertext hex output from Java.
At this point I've also tried using key pairs generated by OpenSSL & TomCrypt (via Crypt::PK::RSA) and can't even get Java to read them so I can't even generate a throwaway key to use here as an example.
As I commented, for any decent modern encryption scheme, you can't validate a ciphertext by comparing it to another ciphertext, because by design they are always different. This is required to satisfy the now generally-agreed criterion for security, that an adversary cannot gain any knowledge about the plaintext, other than its maximum size, by knowledge of any number of other plaintext-ciphertext pairs other than the one(s) being attacked. See https://en.wikipedia.org/wiki/Ciphertext_indistinguishability .
Instead, to determine whether a ciphertext is correct, you can (only) see if it decrypts (at all, and to the desired value). To answer your previous question, since it is now closed, here is an example of using perl Crypt::PK::RSA to encrypt a value that can be decrypted by Java as you specified: