/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package buzzerproxy; //import java.math.BigInteger; import java.security.SecureRandom; //import java.util.UUID; import java.util.logging.Level; import java.util.logging.Logger; import org.bouncycastle.util.encoders.Hex; import org.bouncycastle.crypto.BufferedBlockCipher; import org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher; import org.bouncycastle.crypto.modes.CBCBlockCipher; import org.bouncycastle.crypto.engines.AESEngine; import org.bouncycastle.crypto.params.KeyParameter; import org.bouncycastle.crypto.CryptoException; /** * * @author Enger */ public class Crypto { private byte[] cipherText = null; public SecureRandom random = new SecureRandom(); private int len = 256; private long sessionIDLenght = 32; public final long generateSessionID() { // return UUID.randomUUID().toString()+new BigInteger(130, random).toString(len); // return new BigInteger(130, random).longValue(); SecureRandom randomr = new SecureRandom(); return randomr.nextLong(); } // public final String generateSessionID(){ public final String performEncrypt(byte[] key, String plainText) { if (plainText == null || !(plainText instanceof String) || key == null || !(key instanceof byte[])) { Exception e = new Exception(); Logger.getLogger(Crypto.class.getName()).log(Level.SEVERE, null, e); System.exit(1); } // if ( str == null || !(str instanceof String)){ byte[] ptBytes = plainText.getBytes(); BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESEngine())); String name = cipher.getUnderlyingCipher().getAlgorithmName(); cipher.init(true, new KeyParameter(key)); byte[] rv = new byte[cipher.getOutputSize(ptBytes.length)]; int oLen = cipher.processBytes(ptBytes, 0, ptBytes.length, rv, 0); try { cipher.doFinal(rv, oLen); } catch (CryptoException ce) { System.out.println(ce.toString()); Logger.getLogger(Crypto.class.getName()).log(Level.SEVERE, null, ce); } return new String(Hex.encode(rv)); //return new String(rv).trim(); } // public final String performEncrypt(byte[] key, String plainText) { public final String performDecrypt(byte[] key, String st) { if (st == null || !(st instanceof String) || key == null || !(key instanceof byte[])) { Exception e = new Exception(); Logger.getLogger(Crypto.class.getName()).log(Level.SEVERE, null, e); System.exit(1); } // if ( str == null || !(str instanceof String)){ cipherText = Hex.decode(st); BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESEngine())); cipher.init(false, new KeyParameter(key)); byte[] rv = new byte[cipher.getOutputSize(cipherText.length)]; int oLen = cipher.processBytes(cipherText, 0, cipherText.length, rv, 0); try { cipher.doFinal(rv, oLen); } catch (CryptoException ce) { System.out.println(ce.toString()); Logger.getLogger(Crypto.class.getName()).log(Level.SEVERE, null, ce); } return new String(rv).trim(); } // public final String performDecrypt(byte[] key, String st) { } // public class Crypto {