Advertisement
Guest User

DesEncrypter.java

a guest
Nov 9th, 2013
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.05 KB | None | 0 0
  1. package com.tcg.utils;
  2.  
  3. import javax.crypto.Cipher;
  4. import javax.crypto.SecretKey;
  5. import javax.crypto.SecretKeyFactory;
  6. import javax.crypto.spec.DESKeySpec;
  7.  
  8.  
  9. public class DesEncrypter {
  10.     Cipher ecipher;
  11.     Cipher dcipher;
  12.  
  13.     public DesEncrypter(String stringKey){
  14.         try {
  15.             DESKeySpec dks = new DESKeySpec(stringKey.getBytes());
  16.             SecretKeyFactory skf = SecretKeyFactory.getInstance("DES");
  17.             SecretKey key = skf.generateSecret(dks);
  18.            
  19.             ecipher = Cipher.getInstance("DES");
  20.             dcipher = Cipher.getInstance("DES");
  21.             ecipher.init(Cipher.ENCRYPT_MODE, key);
  22.             dcipher.init(Cipher.DECRYPT_MODE, key);
  23.         } catch (Exception e) {
  24.             e.printStackTrace();
  25.         }
  26.     }
  27.  
  28.     public String encrypt(String str){
  29.         try {
  30.             byte[] utf8 = str.getBytes("UTF8");
  31.             byte[] enc = ecipher.doFinal(utf8);
  32.  
  33.             return encode64(enc);
  34.         } catch (Exception e) {
  35.             e.printStackTrace();
  36.         }
  37.         return "";
  38.     }
  39.  
  40.     public String decrypt(String str){
  41.         try {
  42.             byte[] dec = decode64(str);
  43.             byte[] utf8 = dcipher.doFinal(dec);
  44.             return new String(utf8, "UTF8");
  45.         } catch (Exception e) {
  46.             e.printStackTrace();
  47.         }
  48.         return "";
  49.     }
  50.    
  51. // ################################# base64 routines ######################################################### //
  52.     private String encode64(byte[] d) {
  53.         if (d == null) return null;
  54.         byte data[] = new byte[d.length+2];
  55.         System.arraycopy(d, 0, data, 0, d.length);
  56.         byte dest[] = new byte[(data.length/3)*4];
  57.        
  58.         //  3-byte to 4-byte conversion
  59.         for (int sidx = 0, didx=0; sidx < d.length; sidx += 3, didx += 4) {
  60.             dest[didx]   = (byte) ((data[sidx] >>> 2) & 077);
  61.             dest[didx+1] = (byte) ((data[sidx+1] >>> 4) & 017 | (data[sidx] << 4) & 077);
  62.             dest[didx+2] = (byte) ((data[sidx+2] >>> 6) & 003 | (data[sidx+1] << 2) & 077);
  63.             dest[didx+3] = (byte) (data[sidx+2] & 077);
  64.         }
  65.        
  66.         // 0-63 to ascii printable conversion
  67.         for (int idx = 0; idx <dest.length; idx++){
  68.             if (dest[idx] < 26)         dest[idx] = (byte)(dest[idx] + 'A');
  69.             else if (dest[idx] < 52)    dest[idx] = (byte)(dest[idx] + 'a' - 26);
  70.             else if (dest[idx] < 62)    dest[idx] = (byte)(dest[idx] + '0' - 52);
  71.             else if (dest[idx] < 63)    dest[idx] = (byte)'+';
  72.             else                        dest[idx] = (byte)'/';
  73.         }
  74.        
  75.         // add padding
  76.         for (int idx = dest.length-1; idx > (d.length*4)/3; idx--){
  77.             dest[idx] = (byte)'=';
  78.         }
  79.         return new String(dest);
  80.     }
  81.  
  82.     @SuppressWarnings("unused")
  83.     private String encode64(String s) {
  84.           return encode64(s.getBytes());
  85.     }
  86.  
  87.       private byte[] decode64(String str){
  88.           if (str == null)  return  null;
  89.           byte data[] = str.getBytes();
  90.           return decode64(data);
  91.       }
  92.  
  93.       private byte[] decode64(byte[] data){
  94.           int tail = data.length;
  95.           while (data[tail-1] == '=')  tail--;
  96.           byte dest[] = new byte[tail - data.length/4];
  97.  
  98.           // ascii printable to 0-63 conversion
  99.           for (int idx = 0; idx <data.length; idx++){
  100.               if (data[idx] == '=')         data[idx] = 0;
  101.               else if (data[idx] == '/')    data[idx] = 63;
  102.               else if (data[idx] == '+')    data[idx] = 62;
  103.               else if (data[idx] >= '0'  &&  data[idx] <= '9')
  104.                                             data[idx] = (byte)(data[idx] - ('0' - 52));
  105.               else if (data[idx] >= 'a'  &&  data[idx] <= 'z')
  106.                                             data[idx] = (byte)(data[idx] - ('a' - 26));
  107.               else if (data[idx] >= 'A'  &&  data[idx] <= 'Z')
  108.                                             data[idx] = (byte)(data[idx] - 'A');
  109.           }
  110.  
  111.           // 4-byte to 3-byte conversion
  112.           int sidx, didx;
  113.           for (sidx = 0, didx=0; didx < dest.length-2; sidx += 4, didx += 3){
  114.               dest[didx]   = (byte) ( ((data[sidx] << 2) & 255) | ((data[sidx+1] >>> 4) & 3) );
  115.               dest[didx+1] = (byte) ( ((data[sidx+1] << 4) & 255) | ((data[sidx+2] >>> 2) & 017) );
  116.               dest[didx+2] = (byte) ( ((data[sidx+2] << 6) & 255) | (data[sidx+3] & 077) );
  117.           }
  118.           if (didx < dest.length) {
  119.               dest[didx]   = (byte) ( ((data[sidx] << 2) & 255) | ((data[sidx+1] >>> 4) & 3) );
  120.           }
  121.           if (++didx < dest.length){
  122.               dest[didx]   = (byte) ( ((data[sidx+1] << 4) & 255) | ((data[sidx+2] >>> 2) & 017) );
  123.           }
  124.           return dest;
  125.       }
  126.  
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement