文字列の暗号化を実行する OSGi プラグイン (バンドル) を開発しています。文字列自体は AES を使用して暗号化されます。AES キーを RSA で暗号化しています。私の単体テストでは、すべてが完全に機能します。
プラグインを Karaf にデプロイすると (現在、他の OSGi コンテナーは試していません)、暗号化されたキーの結果は、最後の 1 バイトを含む 0 バイトの束になります。スローされる例外はありません。デバッガーを使用すると、RSA 公開鍵暗号が公開指数の値が zero である key-spec を使用していることを発見したことを除いて、すべてが正常に見えます。これは明らかに意味がなく、出力が主にゼロであることは驚くことではありません。
なぜこれが起こっているのか誰にも示唆できますか?
いくつかのコード フラグメントを追加します。
public static Cipher createRsaCipher(final boolean keyTypePublic, final int mode, final KeySpec keySpec) throws GeneralSecurityException
{
final KeyFactory kfpri = KeyFactory.getInstance(RSA);
final Cipher result = Cipher.getInstance(RSA);
result.init(mode, keyTypePublic ? kfpri.generatePublic(keySpec) : kfpri.generatePrivate(keySpec));
return result;
}
private static Cipher createPublicKeyEncryptionCipher(final URL key) throws IOException, GeneralSecurityException {
try (InputStream stream = key.openStream()) {
final byte[] encodedKey = readPublicKeyBytes(stream);
final X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(encodedKey);
return CipherFactory.createRsaCipher(true, Cipher.ENCRYPT_MODE, publicKeySpec);
}
}
private static byte[] encrypt(final byte[] source, Cipher cipher) throws GeneralSecurityException {
final int bytes = source.length;
final int outputSize = cipher.getOutputSize(bytes);
final byte[] buffer = new byte[outputSize];
int resultLength = 0;
final int n = cipher.doFinal(source, 0, bytes, buffer, 0);
resultLength += n;
final byte[] result = new byte[resultLength];
System.arraycopy(buffer, 0, result, 0, resultLength);
return result;
}
openssl rsa -in private.pem -pubout -outform DER -out public.der
more private.pem
-----BEGIN RSA PRIVATE KEY-----
MIIEpQIBAAKCAQEA6LhJ1xCjo2mOMYO3Km5rk+1jpSUgeFLX296apNHgHVb7e9H/
.....etc...........
o6ZYdYg05ubEu+jRQkdudbA/7AXLwYOzGtzhla7ow5QhYcWtJEOwX4U=
-----END RSA PRIVATE KEY-----