/* * File : Encryption.java * Created : 03-jul-2001 09:51 * By : allastar * * JClic - Authoring and playing system for educational activities * * Copyright (C) 2000 - 2005 Francesc Busquets & Departament * d'Educacio de la Generalitat de Catalunya * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details (see the LICENSE file). */ package edu.xtec.util; /** * Utilities to encrypt and decrypt strings using simple methods, just to avoid write * passwords in plain text in data and configuration files. Do not use it as a * secure cryptographic system! * @author Albert Llastarri (allastar@xtec.cat) * @version 13.08.08 */ public final class Encryption { private static final String BLANK="___blank___##"; public static String Encrypt(String txt) { if(txt==null || txt.length()==0) txt=BLANK; String result=null; try{ result=codify(txt); } catch(Exception ex){ System.err.println("Error encripting text!"); } return result; } public static String Decrypt(String txt) { if(txt==null || txt.length()==0) return null; String s=decodify(txt); if(BLANK.equals(s)) s=new String(); return s; } private static char hexCharArrayToChar(char[] cA, int fromIndex){ char[] hex=new char[4]; int n=0; for (int i=0;i<=3;i++){ int j=Character.digit(cA[fromIndex+i],16); n=(n*16)+j; } return (char)n; } private static char[] charToHexCharArray(char c){ char[] hex=new char[4]; int j=(int)c; for (int i=3;i>=0;i--){ hex[i]=Character.forDigit(j%16,16); j/=16; } return hex; } private static char[] intToHexCharArray(int c){ char[] hex=new char[2]; int j=(int)c; for (int i=1;i>=0;i--){ hex[i]=Character.forDigit(j%16,16); j/=16; } return hex; } private static int hexCharArrayToInt(char[] cA, int fromIndex){ int n=0; for (int i=0;i<=1;i++){ int j=Character.digit(cA[fromIndex+i],16); n=(n*16)+j; } return n; } private static StringBuilder compressZeros(char[] cA){ int total=0; StringBuilder sb=new StringBuilder(cA.length); int[] zeros=new int[(cA.length+7)/8]; //it will be better to use bytes, but //then it takes the first bit as a sign int j; for (j=0;total1){ char c1=hexZeros.charAt(0); char c2=hexZeros.charAt(1); int num=1; int currentChar=2; while (currentChar0){ sb.append(cA[(i*3)+1]); sb.append(cA[(i*3)+2]); num--; k++; } if (cA.length>((i*3)+3)) num=Character.digit(cA[(i*3)+3],32); else num=0; } for (int j=(i*3)+1;j24) throw new TooLargePasswordException(); return new String(changeOrder(compressZeros(codifyToHex(word)))); } private static String decodify(String word){ try{ return decodifyFromHex(decompressZeros(unchangeOrder(word))); } catch (Exception e){ //The supplied word was not codified using this system return ""; } } }