Welcome to the Official BlackBerry® Support Community Forums. This is your resource to discuss support topics with your peers, and learn from each other. New to the forum? Please visit the ‘Getting Started’ link below.
inside custom component

Java Development

Reply
Regular Contributor
8vius
Posts: 52
Registered: 10-07-2010
My Carrier: Digitel

RSA Encryption Throwing InvalidKeyException

I'm trying to use RIM's Crypto API's in my project i have made a public/private keypair in Java as follows: 

 

 

try {
            keyPairGenerator = KeyPairGenerator.getInstance("RSA");
            keyPairGenerator.initialize(1024);
            keyFactory = KeyFactory.getInstance("RSA");
            } catch (NoSuchAlgorithmException ex) {
            Logger.getLogger(EncryptorDecryptor.class.getName()).log(Level.SEVERE, null, ex);
            }
            keyPair = keyPairGenerator.generateKeyPair();
            publicKey = keyPair.getPublic();
            privateKey = keyPair.getPrivate();
            try {
            publicKeySpec = keyFactory.getKeySpec(publicKey, RSAPublicKeySpec.class);
            privateKeySpec = keyFactory.getKeySpec(privateKey, RSAPrivateKeySpec.class);
            } catch (InvalidKeySpecException ex) {
            Logger.getLogger(EncryptorDecryptor.class.getName()).log(Level.SEVERE, null, ex);
            }
            privateKeyModulus = privateKeySpec.getModulus().toString();
            privateKeyExponent = privateKeySpec.getPrivateExponent().toString();
            publicKeyModulus = publicKeySpec.getModulus().toString();
            publicKeyExponent = publicKeySpec.getPublicExponent().toString();

 

 

I save the modulus and exponent strings as part of my code in the server. On the client i save the modulus and exponent required to create the public key as strings. I try to do the following to encrypt:

 

 

public byte[] Encrypt(byte[] data)
  {
	  try {
		  	
			RSACryptoSystem cryptoSystem = new RSACryptoSystem(1024);
			RSAPublicKey publicKey = new RSAPublicKey(cryptoSystem,_publicKeyExponent.getBytes(), _publicKeyModulus.getBytes());
			RSAEncryptorEngine encryptorEngine = new RSAEncryptorEngine(publicKey);
			
			PKCS5FormatterEngine formatterEngine = new PKCS5FormatterEngine( encryptorEngine );
			
			ByteArrayOutputStream output = new ByteArrayOutputStream();
	        BlockEncryptor encryptor = new BlockEncryptor( formatterEngine, output );
	        
	        encryptor.write(data);
	        encryptor.close();
	        output.close();
	        
	        return output.toByteArray();
		} catch (InvalidKeyException e) {
			// TODO Auto-generated catch block
			System.out.println();
			e.printStackTrace();
		} catch (CryptoTokenException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (CryptoUnsupportedOperationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (UnsupportedCryptoSystemException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	  return null;
  } 

 

When creating the public key i get the error, anybody have any idea what the issue might be? Or have an alternative?

 

Please use plain text.