title: PHP to Android encryption porting: AES in CFB mode
body: <p>I have created a PHP file with the following code that encodes/decodes a string:</p>
  
  <pre><code>$key = "D0QgiY8JYvx8qzKx0iaN8kwEJgwpEqAJ"; 
  
  $iv = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_CFB), MCRYPT_RAND);
  
  function encrypt($key, $text, $iv) {
      return trim(base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $text, MCRYPT_MODE_CFB, $iv)));
  }
  
  function decrypt($key, $text, $iv) { 
      return trim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, base64_decode($text), MCRYPT_MODE_CFB, $iv));
  }
  
  $text = $_GET['text'];
  
  echo "Plain Number : " . $text . "&lt;br&gt;&lt;br&gt;";
  
  $encrypted = encrypt($key, $text, $iv);
  echo "AES Number : " . $encrypted . "&lt;br&gt;&lt;br&gt;";
  echo "block size=";
  echo $block=mcrypt_get_block_size ( MCRYPT_RIJNDAEL_128,"cfb" );
   echo "&lt;br/&gt;"."IV=";
     echo $ivValue =mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128,MCRYPT_MODE_CFB);
  echo "&lt;br/&gt;";
  echo "Plain Number : ". decrypt($key,$encrypted, $iv) . "&lt;br&gt;&lt;br&gt;";
  </code></pre>
  
  <p>I wish to do the same thing using the Android API's </p>
  
  <pre><code>static String encryptString(final String RAWDATA, boolean ENCODE) { // This
                    // was a
                    // custom
     Log.d("TEST1", iv.toString()); 
    String encrypted = null;
    byte[] encryptedBytes = null;
    byte[] key;
  
    key = AES_V1_KEY.getBytes();
    //SecretKeySpec skeySpec = new SecretKeySpec(key, "AES");
    // Instantiate the cipher
    Cipher cipher = null;
    try {
     String input = Integer.toString(RAWDATA.length()) + '|' +RAWDATA;
     KeyGenerator kgen = KeyGenerator.getInstance(KEY_ALGORITHM);
     SecureRandom sr = SecureRandom.getInstance("SHA1PRNG");
     sr.setSeed(key);
      kgen.init(128, sr); // 192 and 256 bits may not be available
     SecretKey skey = kgen.generateKey();
        byte[] raw1 = skey.getEncoded();
  
        SecretKeySpec skeySpec = new SecretKeySpec(raw1,KEY_ALGORITHM);
     cipher = Cipher.getInstance(CIPHER_ALGORITHM);
     cipher.init(Cipher.ENCRYPT_MODE, skeySpec,MyEnc1.ivParameterSpec);
     //cipher.init(Cipher.ENCRYPT_MODE, MyEnc1.secretKeySpec,MyEnc1.ivParameterSpec);
      // encryptedBytes = cipher.doFinal(nullPadString(input).getBytes());
  
     encryptedBytes = cipher.doFinal(nullPadString(input).getBytes());
    }catch(InvalidAlgorithmParameterException e){
     Log.d("error",e.toString());
    } 
    catch (NoSuchAlgorithmException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    } catch (NoSuchPaddingException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    } catch (InvalidKeyException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    } catch (IllegalBlockSizeException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    } catch (BadPaddingException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }
  
    encrypted = new String(encryptedBytes);
    if (ENCODE)
     encrypted = new String(Base64.encodeBytes(encryptedBytes));
    else
     encrypted = new String(encryptedBytes);
    return encrypted;
  
    // /return encrypted;
   }// method end
  </code></pre>
  
  <p>Where the variables are:</p>
  
  <pre><code> final static String AES_V1_KEY = "D0QgiY8JYvx8qzKx0iaN8kwEJgwpEqAJ";
   private static final String KEY_ALGORITHM = "AES";
   private static final String CIPHER_ALGORITHM = "AES/CFB/NoPadding";
  </code></pre>
  
  <p>On the Android side encryption and decryption possible using its own API's.</p>