title: RSA ENCRYPTION IN ANDROID AND DECRYPTION IN SERVER SIDE
body: <p>I have made a https api request from android to server. The API request contains one parameter that needs to be encrypted before it is send (i.e. it's a password). <code>RSA/ECB/PKCS1Padding</code> is the encryption used in both end.</p>
  
  <p>Encryption in android side does the following things:</p>
  
  <pre><code>/*Encrypt the password using public key.public key is obtained from generateRsaPublicKey(BigInteger modulus, BigInteger publicExponent) function)*/
  
  public static String rsaEncrypt(String originalString, PublicKey key) {
      try {
          Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
          cipher.init(Cipher.ENCRYPT_MODE, key);
          byte[] cipherByte = cipher.doFinal(original);
          return bytesToHex(cipherByte);
      } catch (Exception e) {
          e.printStackTrace();
      }
      return null;
  }
  
  //generate public key with given module and exponent value
  public static PublicKey generateRsaPublicKey(BigInteger modulus, BigInteger publicExponent) {
      PublicKey key = null;
      try {
          key = KeyFactory.getInstance("RSA").generatePublic(new RSAPublicKeySpec(modulus, publicExponent));
        return key;
      } catch (Exception e) {
          Log.e("error", e.toString());
          // return null;
      }
      return key;
  }
  
  // Helper methods
  
  final protected static char[] hexArray = "0123456789abcdef".toCharArray();
  public static String bytesToHex(byte[] bytes) {
      char[] hexChars = new char[bytes.length * 2];
      for ( int j = 0; j &lt; bytes.length; j++ ) {
          int v = bytes[j] &amp; 0xFF;
         // Log.d("byte array representaion","value in integrer"+v);
          hexChars[j * 2] = hexArray[v &gt;&gt;&gt; 4];           
          hexChars[j * 2 + 1] = hexArray[v &amp; 0x0F];
      }
      return new String(hexChars);
  }
  </code></pre>
  
  <p>below is the source for decrypting the password on the server</p>
  
  <pre><code>// *** setup private key
  
  RSAPrivateKeySpec privateRPKS
  = new RSAPrivateKeySpec(new BigInteger(gModulusPlainS, 16), new BigInteger(privateExponentPlainS, 16));
  KeyFactory keyFactoryKF = KeyFactory.getInstance("RSA");
  RSAPrivateKey gPrivateKeyRPK = (RSAPrivateKey) keyFactoryKF.generatePrivate(privateRPKS);
  
  // *** setup cipher
  Cipher cipherC = Cipher.getInstance("RSA/ECB/PKCS1Padding");
  cipherC.init(Cipher.DECRYPT_MODE, gPrivateKeyRPK);
  
  // *** decrypt hex-encoded cipherTxS
  byte[] baCipherText = hexToBin(cipherTxS.getBytes());
  byte[] baPlainText2 = cipherC.doFinal(baCipherText);
  String decryptedTextS = new String(baPlainText2);
  </code></pre>
  
  <p>But I got the following error log </p>
  
  <pre><code>javax.crypto.IllegalBlockSizeException: Data size too large
      at com.ibm.crypto.provider.RSASSL.a(Unknown Source)
      at com.ibm.crypto.provider.RSASSL.engineDoFinal(Unknown Source)
      at javax.crypto.Cipher.doFinal(Unknown Source)
  
  javax.crypto.BadPaddingException: Not PKCS#1 block type 2 or Zero padding
  </code></pre>
  
  <p>But it is working on websight part. Why it isn't working in android?</p>
  
  <p>Thank you for your kindness to looking my code.</p>