Creating a derived RSAPublicKey
Posted: Mon Mar 20, 2017 3:15 am
Hi folks,
I'm trying to create a class that implements RSAPublicKey, so that I can pass it through to Cipher.init(). I'm doing this because I want to override the behavior of getModulus() slightly.
Since RSAPublicKey is an interface, not a class, I cant extend it, so I created a class that implemented it.
e.g. public class DynamicRSAPublicKey implements RSAPublicKey { ... }
To make sure that I could get it working, I first created an instance of this class that did absolutely nothing to change the behavior. It is just a simple wrapper class and the code is here: http://pastebin.com/iVcMt5fU
I then create a really basic RSA encryption test, but I'm getting a CryptoException when I call Cipher.init (passing it an instance of DynamicRSAPublicKey). My test code works perfectly if I just pass an actual RSAPublicKey through.
The reason code is CryptoException.ILLEGAL_VALUE, so my theory is that the init() method is internally doing a check along the lines of 'if (!key instanceof <SomeClass>) CryptoException.throwIt(CryptoException.ILLEGAL_VALUE)'.
My problem is, because ultimately I'm using KeyBuilder.buildKey to create the interal RSAPublicKey, I have absolutely no idea what the actual class is that I should be deriving from?
Is there any way around this that anyone can think of?
Cheers,
kos
I'm trying to create a class that implements RSAPublicKey, so that I can pass it through to Cipher.init(). I'm doing this because I want to override the behavior of getModulus() slightly.
Since RSAPublicKey is an interface, not a class, I cant extend it, so I created a class that implemented it.
e.g. public class DynamicRSAPublicKey implements RSAPublicKey { ... }
To make sure that I could get it working, I first created an instance of this class that did absolutely nothing to change the behavior. It is just a simple wrapper class and the code is here: http://pastebin.com/iVcMt5fU
I then create a really basic RSA encryption test, but I'm getting a CryptoException when I call Cipher.init (passing it an instance of DynamicRSAPublicKey). My test code works perfectly if I just pass an actual RSAPublicKey through.
The reason code is CryptoException.ILLEGAL_VALUE, so my theory is that the init() method is internally doing a check along the lines of 'if (!key instanceof <SomeClass>) CryptoException.throwIt(CryptoException.ILLEGAL_VALUE)'.
My problem is, because ultimately I'm using KeyBuilder.buildKey to create the interal RSAPublicKey, I have absolutely no idea what the actual class is that I should be deriving from?
Is there any way around this that anyone can think of?
Cheers,
kos