encrypt in php and decrypt in java using blowfish/base64 on Google App Engine

encrypt in php and decrypt in java using blowfish/base64 on Google App Engine
--------------------------------------------------------------------------------

Suppose you have a encryptedData with some key using blowfish and base64 from PHP code and you want to decrypt it using Java....

public static String doubleDecryption_Base64_Blowfish(String to_decrypt,String key,String charSet){          
          // charSet="UTF-8";
           Cipher cipher;
            try {
               byte[] decodedBytes = Base64.decodeBase64(to_decrypt.getBytes(charSet));
               if(decodedBytes.length % 8 != 0){ //not a multiple of 8
                   System.out.println("decodedBytes is not padded properly in 8 bits");
                   //create a new array with a size which is a multiple of 8
                   byte[] padded = new byte[decodedBytes.length + 8 - (decodedBytes.length % 8)];
                   //copy the old array into it
                   System.arraycopy(decodedBytes, 0, padded, 0, decodedBytes.length);
                   decodedBytes = padded;
               }else{
                   System.out.println("decodedBytes is padded properly in 8 bits");
               }
              
               byte[] keyBytes=key.getBytes(charSet);
               SecretKeySpec secretkey = new SecretKeySpec(keyBytes, "Blowfish");
               cipher = Cipher.getInstance("Blowfish/ECB/NoPadding");
               cipher.init(Cipher.DECRYPT_MODE, secretkey);      
               byte[] decryptedFinal = cipher.doFinal(decodedBytes);
               return new String(decryptedFinal);
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
       }      

Comments

Popular posts from this blog

Read Images from a xlsx file using Apache POI

Struts 2 : Warning :No configuration found for the specified action: 'Login.action' in namespace: '/'

How to create mail message in FRC822 format