RSA cipher keeps giving error BadPaddingException: Message is largerthan modulus

E

ech0

I'm trying to encrypt a byte array using a public key I get from a
network stream. The problem is that that the cipher keeps giving me an
error:

"BadPaddingException: Message is larger than modulus"

here is the code:

_rsaKey is a byte array (size 128)
temp is a zero filled byte array (size)
======================================================
kfac = KeyFactory.getInstance("RSA")
modulus = BigInteger(self._rsaKey)
kspec1 = RSAPublicKeySpec(modulus, RSAKeyGenParameterSpec.F4)
publicKey = kfac.generatePublic(kspec1)

rsaCipher = Cipher.getInstance("RSA/ECB/nopadding")
rsaCipher.init(Cipher.ENCRYPT_MODE, publicKey)
rsaEncryptedData = rsaCipher.doFinal(temp,0x00,0x80)
======================================================

everything goes well up until rsaCipher.doFinal(temp,0x00,0x80)
which throws the exception mentioned above

sometimes i get the exception, sometimes i don't. don't know whats
wrong.

example data values that threw that exception:

==============================================
_rsaKey: array('b',[-111, 56, 32, -77, 103, -86, -102, -89, 20, -74,
70, -116, 8
1, -107, -118, -67, -11, 120, 100, 17, -6, -21, -34, -1, 80, 46, -28,
-90, -42,
-2, -113, -61, -43, 30, 34, -35, -4, -8, 75, 98, -82, 23, -16, -19,
87, 56, -89,
-89, 95, 57, -47, -70, 40, -126, -17, 106, -81, 62, 76, -79, 6, -37,
54, -64, -
3, 75, 79, -92, 115, -97, 96, 40, 16, -90, -31, 101, 102, 88, 78, -47,
21, -90,
50, -12, 112, -54, -78, -38, 17, -108, -60, 96, 123, 74, 87, -46, -44,
63, -19,
-62, 89, 48, -14, -35, 81, 76, 96, 82, 105, -112, -91, 30, -34, -3,
96, 96, 104,
-97, 120, -86, 17, -127, -86, 120, 63, -23, -45, 67])

modulus:
-73073110056286640340097954246863433406112399401091916201456987043760182047884172
921300903878547695582385476703704119802885172774202508430737971758333982209240166
114559892444958685053471229837771943027402558952284852854396505019440462809471970
625231422840483143051964274679306161359974365557628502797717641392

temp:
array('b',[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0
, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0])
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top