Note that there are some explanatory texts on larger screens.

plurals
  1. POError while using RSA encryption on BlackBerry
    primarykey
    data
    text
    <p>I'm trying to use RSA encryption on Blackberry with their native API's. I made a public/private key pair in Java and saved the Modulus and Exponents of the keys as strings so i can generate the keys from this for encryption and decryption. The following code is from the client side and i'm getting a <code>InvalidKeyException</code> and the backtrace is null so I don't know what's happening: </p> <pre><code>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; } </code></pre> <p>And this is what i did server side to generate my keys:</p> <pre><code>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(); </code></pre> <p>Any ideas?</p> <p>EDIT: i tried doing a simple test on the server by encrypting and decrypting there and when when I try to decrypt I get a <code>IllegalBlockSizeException</code> these are my encrytion and decryption methods (server side):</p> <pre><code>public byte[] Decrypt(byte[] data) { try { Cipher cipher = Cipher.getInstance("RSA"); cipher.init(Cipher.DECRYPT_MODE, privateKey); byte[] cipherData = cipher.doFinal(data); return cipherData; } catch (NoSuchAlgorithmException ex) { Logger.getLogger(EncryptorDecryptor.class.getName()).log(Level.SEVERE, null, ex); } catch (NoSuchPaddingException ex) { Logger.getLogger(EncryptorDecryptor.class.getName()).log(Level.SEVERE, null, ex); } catch(IllegalBlockSizeException ex) { Logger.getLogger(EncryptorDecryptor.class.getName()).log(Level.SEVERE, null, ex); } catch(InvalidKeyException ex) { Logger.getLogger(EncryptorDecryptor.class.getName()).log(Level.SEVERE, null, ex); } catch(BadPaddingException ex) { Logger.getLogger(EncryptorDecryptor.class.getName()).log(Level.SEVERE, null, ex); } return null; } public byte[] Encrypt(byte[] data) { try { Cipher cipher = Cipher.getInstance("RSA"); cipher.init(Cipher.ENCRYPT_MODE, publicKey); byte[] cipherData = cipher.doFinal(data); return cipherData; } catch (NoSuchAlgorithmException ex) { Logger.getLogger(EncryptorDecryptor.class.getName()).log(Level.SEVERE, null, ex); } catch (NoSuchPaddingException ex) { Logger.getLogger(EncryptorDecryptor.class.getName()).log(Level.SEVERE, null, ex); } catch(IllegalBlockSizeException ex) { Logger.getLogger(EncryptorDecryptor.class.getName()).log(Level.SEVERE, null, ex); } catch(InvalidKeyException ex) { Logger.getLogger(EncryptorDecryptor.class.getName()).log(Level.SEVERE, null, ex); } catch(BadPaddingException ex) { Logger.getLogger(EncryptorDecryptor.class.getName()).log(Level.SEVERE, null, ex); } return null; } </code></pre> <p>And this is the simple test i'm trying:</p> <pre><code>userName = Base64.encode(encryptorDecryptor.Encrypt(userName.getBytes())); password = Base64.encode(encryptorDecryptor.Encrypt(password.getBytes())); userName = new String(encryptorDecryptor.Decrypt(Base64.decode(userName))); password = new String(encryptorDecryptor.Decrypt(Base64.decode(password))); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload