Encryption and decryption using Blowfish and base64 on Google App Engine

Encryption and Decryption  using Blowfish and base64 on Google App Engine using Java

1.. Encryption double using blowfish and then base64

  public static String doubleEncryption_Blowfish_Base64(String to_encrypt,String key,String charEncoding){
           //charEncoding="UTF-8";
           Cipher cipher;
            try {
               byte[] encodedBytes =to_encrypt.getBytes(charEncoding);       
              
               if(encodedBytes.length % 8 != 0){ //not a multiple of 8
                   System.out.println("encodedBytes is not padded properly in 8 bits");
                   //create a new array with a size which is a multiple of 8
                   byte[] padded = new byte[encodedBytes.length + 8 - (encodedBytes.length % 8)];
                   //copy the old array into it
                   System.arraycopy(encodedBytes, 0, padded, 0, encodedBytes.length);
                   encodedBytes = padded;
               }else{
                   System.out.println("encodedBytes is padded properly in 8 bits");
               }
              
               byte[] keyBytes=key.getBytes(charEncoding);
               SecretKeySpec secretkey = new SecretKeySpec(keyBytes, "Blowfish");                  
               cipher = Cipher.getInstance("Blowfish/ECB/NoPadding");
               cipher.init(Cipher.ENCRYPT_MODE, secretkey);      
               byte[] encryptedBlowFish = cipher.doFinal(encodedBytes);
               byte[] encryptedFinalBase64 = Base64.encodeBase64(encryptedBlowFish);             
               return new String(encryptedFinalBase64);
            } catch (Exception e) {
                 log.warning("Exception:"+e.getMessage());
                 e.printStackTrace();              
                 return null;
            }
       }


2.. Decryption double using base64 and then blowfish

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

Read Excel using Apache POI - Exception in thread "main" org.apache.poi.poifs.filesystem.OfficeXmlFileException:

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