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;
            }
       }      


Popular posts from this blog

Can not connect to git using ssh (Permissions 0644 for '/root/.ssh/id_rsa' are too open.

Upload file on Google cloud storage using Java servlet on google app engine

Create Nested table inside a table on a pdf document using iText