Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.tcg.utils;
- import javax.crypto.Cipher;
- import javax.crypto.SecretKey;
- import javax.crypto.SecretKeyFactory;
- import javax.crypto.spec.DESKeySpec;
- public class DesEncrypter {
- Cipher ecipher;
- Cipher dcipher;
- public DesEncrypter(String stringKey){
- try {
- DESKeySpec dks = new DESKeySpec(stringKey.getBytes());
- SecretKeyFactory skf = SecretKeyFactory.getInstance("DES");
- SecretKey key = skf.generateSecret(dks);
- ecipher = Cipher.getInstance("DES");
- dcipher = Cipher.getInstance("DES");
- ecipher.init(Cipher.ENCRYPT_MODE, key);
- dcipher.init(Cipher.DECRYPT_MODE, key);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- public String encrypt(String str){
- try {
- byte[] utf8 = str.getBytes("UTF8");
- byte[] enc = ecipher.doFinal(utf8);
- return encode64(enc);
- } catch (Exception e) {
- e.printStackTrace();
- }
- return "";
- }
- public String decrypt(String str){
- try {
- byte[] dec = decode64(str);
- byte[] utf8 = dcipher.doFinal(dec);
- return new String(utf8, "UTF8");
- } catch (Exception e) {
- e.printStackTrace();
- }
- return "";
- }
- // ################################# base64 routines ######################################################### //
- private String encode64(byte[] d) {
- if (d == null) return null;
- byte data[] = new byte[d.length+2];
- System.arraycopy(d, 0, data, 0, d.length);
- byte dest[] = new byte[(data.length/3)*4];
- // 3-byte to 4-byte conversion
- for (int sidx = 0, didx=0; sidx < d.length; sidx += 3, didx += 4) {
- dest[didx] = (byte) ((data[sidx] >>> 2) & 077);
- dest[didx+1] = (byte) ((data[sidx+1] >>> 4) & 017 | (data[sidx] << 4) & 077);
- dest[didx+2] = (byte) ((data[sidx+2] >>> 6) & 003 | (data[sidx+1] << 2) & 077);
- dest[didx+3] = (byte) (data[sidx+2] & 077);
- }
- // 0-63 to ascii printable conversion
- for (int idx = 0; idx <dest.length; idx++){
- if (dest[idx] < 26) dest[idx] = (byte)(dest[idx] + 'A');
- else if (dest[idx] < 52) dest[idx] = (byte)(dest[idx] + 'a' - 26);
- else if (dest[idx] < 62) dest[idx] = (byte)(dest[idx] + '0' - 52);
- else if (dest[idx] < 63) dest[idx] = (byte)'+';
- else dest[idx] = (byte)'/';
- }
- // add padding
- for (int idx = dest.length-1; idx > (d.length*4)/3; idx--){
- dest[idx] = (byte)'=';
- }
- return new String(dest);
- }
- @SuppressWarnings("unused")
- private String encode64(String s) {
- return encode64(s.getBytes());
- }
- private byte[] decode64(String str){
- if (str == null) return null;
- byte data[] = str.getBytes();
- return decode64(data);
- }
- private byte[] decode64(byte[] data){
- int tail = data.length;
- while (data[tail-1] == '=') tail--;
- byte dest[] = new byte[tail - data.length/4];
- // ascii printable to 0-63 conversion
- for (int idx = 0; idx <data.length; idx++){
- if (data[idx] == '=') data[idx] = 0;
- else if (data[idx] == '/') data[idx] = 63;
- else if (data[idx] == '+') data[idx] = 62;
- else if (data[idx] >= '0' && data[idx] <= '9')
- data[idx] = (byte)(data[idx] - ('0' - 52));
- else if (data[idx] >= 'a' && data[idx] <= 'z')
- data[idx] = (byte)(data[idx] - ('a' - 26));
- else if (data[idx] >= 'A' && data[idx] <= 'Z')
- data[idx] = (byte)(data[idx] - 'A');
- }
- // 4-byte to 3-byte conversion
- int sidx, didx;
- for (sidx = 0, didx=0; didx < dest.length-2; sidx += 4, didx += 3){
- dest[didx] = (byte) ( ((data[sidx] << 2) & 255) | ((data[sidx+1] >>> 4) & 3) );
- dest[didx+1] = (byte) ( ((data[sidx+1] << 4) & 255) | ((data[sidx+2] >>> 2) & 017) );
- dest[didx+2] = (byte) ( ((data[sidx+2] << 6) & 255) | (data[sidx+3] & 077) );
- }
- if (didx < dest.length) {
- dest[didx] = (byte) ( ((data[sidx] << 2) & 255) | ((data[sidx+1] >>> 4) & 3) );
- }
- if (++didx < dest.length){
- dest[didx] = (byte) ( ((data[sidx+1] << 4) & 255) | ((data[sidx+2] >>> 2) & 017) );
- }
- return dest;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement