title: Common Encryption algorithm for Rails and Java
body: <p>I am going to integrate two web applications written in different platforms (Java and Ruby),
  I have to use common encryption algorithm for password in both application. </p>
  
  <p>Is there any common encryption/decryption algorithm for both? If yes, please mention any useful link or any example. </p>
  
  <p>It would be highly appreciated. Thanks in advance</p>
  
  <p>In addition during my digging out it I found,</p>
  
  <p>I have used Base64 with DES in both, interesting thing is that Characters and special characters give me same result in both but as i adding any number like (1,2,3), half of result is same and half encryption is something different.</p>
  
  <p>*Ruby Code</p>
  
  <pre><code> require 'openssl'
   require 'base64'
  
   c = OpenSSL::Cipher::Cipher.new("des")
   c.encrypt 
   c.key ="REPPIFY_ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  
   e = c.update("ankit@123")
   e &lt;&lt; c.final
   puts Base64.encode64(e)
  </code></pre>
  
  <p>Output: Cbe9GslMs8mh33jAOD9qsw==</p>
  
  <p>*Java Code
  I am defining only encryption method here:-</p>
  
  <pre><code>public static String encryptPassword(String pass) {
  
          public static final String DESKEY = "REPPIFY_ABCDEFGHIJKLMNOPQRSTUVWXYZ";
              System.out.println("Here is my password = "+pass);
          DESKeySpec keySpec = null;
          SecretKeyFactory keyFactory = null;
          SecretKey key = null;
          Cipher cipher = null;
          BASE64Encoder base64encoder = new BASE64Encoder();
  
          byte[] cleartext = null;
          String encrypedPwd = null;
              String pass = "ankit@123";
  
          try {
              keySpec = new DESKeySpec(DESKEY.getBytes("UTF8"));
              keyFactory = SecretKeyFactory.getInstance("DES");
              key = keyFactory.generateSecret(keySpec);
              if(pass!=null) {
                  cleartext = pass.getBytes("UTF8");
                  cipher = Cipher.getInstance("DES");
                  cipher.init(Cipher.ENCRYPT_MODE, key);
                  encrypedPwd = base64encoder.encode(cipher.doFinal(cleartext));
              }
          } catch (InvalidKeyException e) {
              e.printStackTrace();
          } catch (NoSuchAlgorithmException e) {
              e.printStackTrace();
          } catch (InvalidKeySpecException e) {
              e.printStackTrace();
          } catch (UnsupportedEncodingException e) {
              e.printStackTrace();
          } catch (NoSuchPaddingException e) {
              e.printStackTrace();
          } // cipher is not thread safe 
          catch (IllegalBlockSizeException e) {
              e.printStackTrace();
          } catch (BadPaddingException e) {
              e.printStackTrace();
          }
          System.out.println("Here I am printing encrypted pwd = "+encrypedPwd);
          return encrypedPwd;
      }
  </code></pre>
  
  <p>Output in Java :- Cbe9GslMs8mWn9yTmZrUiw==</p>