I\'m trying to encrypt some content with an RSA private key.
I\'m following this example:
http://www.junkheap.net/content/public_key_encryption_java
but c
Its not an accident that encryption with private key is allowed. If you want to break a signature into individual hashing and encryption, then encrypting with private key is essential. Lets say I have a document which i need to sign and my key resides on a network HSM. Now either I stream the entire document to the HSM to sign or I can create a local hash and stream it to the HSM for encryption alone. My choice will depend on whether the local hash computation gives me better performance viz a viz delegated hash computation with network latency.
You can't encrypt with private key. If JCE allows you to do that, it's just by accident.
You need to use signature. Here are the code snippet to do that,
signer = Signature.getInstance("SHA1withRSA");
signer.initSign(privateKey); // PKCS#8 is preferred
signer.update(dataToSign);
byte[] signature = signer.sign();
This question is pretty old, but I recently stumbled upon the problem (I'm implementing requirements of some protocol which requires encryption with private key). I will just quote the post from forum:
I recently stumbled upon the same issue, submitted PMR 22265,49R, and IBM Support after consultation with "development" (whoever those are) ruled that private keys cannot be used for encryption. No matter how much I tried to argue with them that private keys should not be used for data protection, which is only one purpose behind encryption, and that it is perfectly fine to use private keys for encryption to achieve non-repudiation, they were unshakable in their belief. You have got to love people, who insist that 2x2=5.
Here is how I worked around this problem: Essentially, I created a public key object with private key's crypto material. You will need to do the reverse, create a private key object with public key's crypto material, to decrypt with public key if you want to avoid the "Public key cannot be used to decrypt" exception.
RSAPrivateCrtKey privateKey = (RSAPrivateCrtKey) ks.getKey(keyAlias, ksPassword.trim().toCharArray());
RSAPublicKeySpec spec = new RSAPublicKeySpec(
privateKey.getModulus(),
privateKey.getPrivateExponent()
);
Key fakePublicKey = KeyFactory.getInstance("RSA").generatePublic(spec);
encryptCipher.init(Cipher.ENCRYPT_MODE, fakePublicKey);
try this:
java.security.Security.addProvider(
new org.bouncycastle.jce.provider.BouncyCastleProvider()
);
First of all, I'm confused why you are planning to use a Cipher
to encrypt with a private key, rather than signing with a Signature
. I'm not sure that all RSA Cipher
providers will use the correct block type for setup, but it's worth a try.
Setting that aside, though, I think that you are trying to load a non-standard OpenSSL-format key. Converting it to DER with rsa
is essentially just a base-64 decode; the structure of the key is not PKCS #8.
Instead, after genrsa
, use the openssl pkcs8
command to convert the generated key to unencrypted PKCS #8, DER format:
openssl pkcs8 -topk8 -nocrypt -in private.pem -outform der -out private.der
This will produce an unencrypted private key that can be loaded with a PKCS8EncodedKeySpec
.