protected boolean testKey() { String data = UID.random(24); byte[] bb = RSA.encode(data.getBytes(), TConn.pub_key); if (bb != null) { bb = RSA.decode(bb, TConn.pri_key); if (bb != null && data.equals(new String(bb))) { return true; } } return false; }
/** Inits the. */ public static synchronized void init() { if (inited) { return; } _conf = Config.getConfig(); /** initialize app command */ Command.init(); /** initialize the RSA key, hardcode 2048 bits */ TConn.pub_key = SystemConfig.s("pub_key", null); if (TConn.pub_key == null) { Key k = RSA.generate(2048); TConn.pri_key = k.pri_key; TConn.pub_key = k.pub_key; /** set back in database */ SystemConfig.setConfig("pri_key", TConn.pri_key); SystemConfig.setConfig("pub_key", TConn.pub_key); } else { /** get from the database */ TConn.pri_key = SystemConfig.s("pri_key", null); } inited = true; }
public String getEncryptedSharedSecret(String sharedSecret) { byte[] bytes = sharedSecret.getBytes(); int i = 0; String encSecret = ""; BigInteger givenMessage, p1, q1, e1; p1 = new BigInteger("13"); q1 = new BigInteger("19"); e1 = new BigInteger("5"); RSA rsa = new RSA(p1, q1, e1); while (i < bytes.length) { givenMessage = BigInteger.valueOf(bytes[i]); BigInteger encryptMessage = rsa.rsaEncrypt(givenMessage, new BigInteger("5"), new BigInteger("247")); encSecret = encSecret + new String(encryptMessage.toByteArray()); i++; } return encSecret; }
/** * Instantiates a new MDC server. * * @param host the host * @param port the port */ protected MDCServer(String host, int port) { _conf = Config.getConfig(); address = (host == null) ? new InetSocketAddress(port) : new InetSocketAddress(host, port); /** initialize app command */ Command.init(); /** initialize the connection center */ TConnCenter.init(_conf, port); synchronized (_conf) { /** load public key from database */ TConn.pub_key = SystemConfig.s("pub_key", null); TConn.pri_key = SystemConfig.s("pri_key", null); /** initialize the RSA key, hardcode 2048 bits */ if (TConn.pub_key == null || TConn.pri_key == null || "".equals(TConn.pub_key) || "".equals(TConn.pri_key)) { /** print out the old state */ log.warn( "the pub_key or pri_key missed, the old state are pub_key:[" + TConn.pub_key + "], pri_key:[" + TConn.pri_key + "]"); Key k = RSA.generate(2048); TConn.pri_key = k.pri_key; TConn.pub_key = k.pub_key; /** print out the new public key */ log.warn("create new RSA key pair, pub_key:[" + TConn.pub_key + ']'); /** set back in database */ SystemConfig.setConfig("pri_key", TConn.pri_key); SystemConfig.setConfig("pub_key", TConn.pub_key); } MAX_SIZE = SystemConfig.i("mdc.max_size", MAX_SIZE); } }
public static void main(String[] args) { RSA myRSA = new RSA(); String text = "Ὅσον ζῇς φαίνου"; System.out.println(text); System.out.println(myRSA.decrypt(myRSA.encrypt(text, myRSA.getPublicKey()))); }