/* * yubikey simulator class + demo program * GNU LGPL * */ import javax.crypto.Cipher; import javax.crypto.SecretKey; import javax.crypto.spec.SecretKeySpec; import javax.xml.bind.DatatypeConverter; import java.util.Random; import java.io.*; class yubikey { /* The following are stored on the token (or disk) */ String public_id; /* 12 modhex bytes */ String secret_aes_key; /* 128 hex bits = 32 chars */ String secret_id; /* 48 hex bits = 32 chars */ int counter; /* 16 bit Usage counter within session + flags (what flags?) */ /* this is internal state */ int counter_session; /* 8 bit button presses within the session */ int timer; /* 24 bit increased at about 8 Hz */ /* *****: we use epoch here! */ int random; /* 16 bit pseudo random */ /* This is implementation specific */ /* *****: use cryptographicvally secure random, not Knuth's PRNG */ Random rnd; /* Basically a tr */ private String modhex_encode(String h) throws IllegalArgumentException { final String cset="cbdefghijklnrtuv"; final String hset="0123456789abcdef"; String m = h.toLowerCase(); int len = m.length(); int i,ord; String encoded = ""; /* ***** range checking! */ for (i = 0; i < len; i++) { ord = hset.indexOf(m.charAt(i)); if ((ord < 0) || (ord > 15)) { throw new IllegalArgumentException("modhex incorrect"); } encoded += cset.charAt(ord); } return encoded; } /* *****: range checking! (arbitrary size is possible) */ private int crc(String buffer) throws IllegalArgumentException { byte[] buf; int bpos,i,j; int m_crc = 0x5af0; /* Magic start value */ if (buffer.length() != 28) { throw new IllegalArgumentException("bufferlength incorrect"); } buf = DatatypeConverter.parseHexBinary(buffer); /* Carefully simulating 16-bit unsigned Calaculation */ /* Hints: Always & operator length; >>> operator fill in 0's as we need */ for (bpos = 0; bpos < 14; bpos++) { m_crc ^= buf[bpos] & 0xff; for (i = 0; i < 8; i++) { j = m_crc & 1; m_crc >>>= 1; if (j != 0) { m_crc ^= 0x8408 & 0xffff; } } } return m_crc; } /* return current state as string */ private String token() throws IllegalStateException { String hextok; int crc; SecretKey key; Cipher cipher; if (secret_aes_key.length() != 32) { throw new IllegalStateException("keylength incorrect"); } try { key = new SecretKeySpec(DatatypeConverter.parseHexBinary(secret_aes_key), "AES"); cipher = Cipher.getInstance("AES/ECB/NoPadding"); cipher.init(Cipher.ENCRYPT_MODE, key); hextok = secret_id + String.format("%02x%02x%02x%02x%02x%02x%02x%02x", counter%256, counter/256, timer%256, (timer/256)%256, timer/65536, counter_session, random%256, random/256); crc = crc(hextok); hextok += String.format("%02x%02x", crc%256, crc/256); return public_id+modhex_encode(DatatypeConverter.printHexBinary(cipher.doFinal(DatatypeConverter.parseHexBinary(hextok)))); } catch (Exception e) { /* NoSuchAlgorithmException, InvalidKeyException, IllegalBlockSizeException */ /* Should not happen */ throw new IllegalStateException("Internal Encryption Parameters", e); } /* not reached */ } private void sanitize() { random = rnd.nextInt(65536); counter %= 65536; counter_session %= 256; timer = (int)(System.currentTimeMillis()/1000/256/8); /* make epoch time in 24 bit - hope it is monoton */ } private String rndhexstring(int c) { byte[] bytes; bytes = new byte[c];; rnd.nextBytes(bytes); return DatatypeConverter.printHexBinary(bytes).toLowerCase(); } private String rndmodhexstring(int c) { return modhex_encode(rndhexstring(c)); } /* new token from scratch */ public yubikey() { rnd = new Random(); public_id = rndmodhexstring(6); secret_aes_key = rndhexstring(16); secret_id = rndhexstring(6); counter = 0; insert(); } /* new token from file */ public yubikey(String s) throws Exception { String t; rnd = new Random(); String[] lines = s.split(System.getProperty("line.separator")); try { if (lines.length != 5) { throw new IllegalArgumentException("format incorrect in "+s); } if (lines[0].compareTo("Yubisim Token 0.1") != 0 ) { throw new IllegalArgumentException("format incorrect in "+s); } if (! lines[1].startsWith("Public ID: ")) { throw new IllegalArgumentException("format incorrect in "+s); } t = lines[1].substring(11); if (! t.matches("^[cbdefghijklnrtuv]{12}$")) { throw new IllegalArgumentException("format incorrect in "+s); } public_id = t.trim(); if (! lines[2].startsWith("Secret AES Key: ")) { throw new IllegalArgumentException("format incorrect in "+s); } t = lines[2].substring(16); if (! t.matches("^[0-9a-f]{32}$")) { throw new IllegalArgumentException("format incorrect in "+s); } secret_aes_key = t.trim(); if (! lines[3].startsWith("Secret ID: ")) { throw new IllegalArgumentException("format incorrect in "+s); } t = lines[3].substring(11); if (! t.matches("^[0-9a-f]{12}$")) { throw new IllegalArgumentException("format incorrect in "+s); } secret_id = t.trim(); if (! lines[4].startsWith("Counter: ")) { throw new IllegalArgumentException("format incorrect in "+s); } t = lines[4].substring(9); if (! t.matches("^[0-9]{1,5}$")) { throw new IllegalArgumentException("format incorrect in "+s); } counter = Integer.parseInt(t); if ((counter < 0 ) || (counter > 65535)) { throw new IllegalArgumentException("format incorrect in "+s); } } catch (Exception e) { throw new Exception("Problem parsing string "+s, e); } insert(); } /* simulated (re-)insert */ public void insert() { counter++; counter_session = 0; sanitize(); } /* simulated button press and return key sequence */ public String button() { counter_session++; sanitize(); return token(); } /* return string containing state */ public String serialise() { return "Yubisim Token 0.1\n"+ "Public ID: "+public_id+"\n"+ "Secret AES Key: "+secret_aes_key+"\n"+ "Secret ID: "+secret_id+"\n"+ "Counter: "+counter+"\n"; } } public class yubi_simulator { private static yubikey load(String fn) throws Exception { /* FileNotFoundException, IOException, Exception from yubikey */ File f = new File(fn); InputStream in = new FileInputStream(f); byte[] b = new byte[(int)f.length()]; int len = b.length; int total = 0; while (total < len) { int result = in.read(b, total, len - total); if (result == -1) { break; /* EOF */ } total += result; } in.close(); return new yubikey(new String(b)); } private static void save(yubikey yk, String fn) throws FileNotFoundException { PrintStream out = new PrintStream(fn); out.print(yk.serialise()); out.close(); } public static void main(String []args) throws Exception { String fn; if (args.length != 1) { System.err.println("Usage: yubi_simulator <tokenfile>"); System.exit(1); } fn = args[0]; yubikey yk = load(fn); System.out.println(yk.button()); save(yk, fn); } }
Write, Run & Share Java code online using OneCompiler's Java online compiler for free. It's one of the robust, feature-rich online compilers for Java language, running the Java LTS version 17. Getting started with the OneCompiler's Java editor is easy and fast. The editor shows sample boilerplate code when you choose language as Java and start coding.
OneCompiler's Java online editor supports stdin and users can give inputs to the programs using the STDIN textbox under the I/O tab. Using Scanner class in Java program, you can read the inputs. Following is a sample program that shows reading STDIN ( A string in this case ).
import java.util.Scanner;
class Input {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter your name: ");
String inp = input.next();
System.out.println("Hello, " + inp);
}
}
OneCompiler supports Gradle for dependency management. Users can add dependencies in the build.gradle
file and use them in their programs. When you add the dependencies for the first time, the first run might be a little slow as we download the dependencies, but the subsequent runs will be faster. Following sample Gradle configuration shows how to add dependencies
apply plugin:'application'
mainClassName = 'HelloWorld'
run { standardInput = System.in }
sourceSets { main { java { srcDir './' } } }
repositories {
jcenter()
}
dependencies {
// add dependencies here as below
implementation group: 'org.apache.commons', name: 'commons-lang3', version: '3.9'
}
Java is a very popular general-purpose programming language, it is class-based and object-oriented. Java was developed by James Gosling at Sun Microsystems ( later acquired by Oracle) the initial release of Java was in 1995. Java 17 is the latest long-term supported version (LTS). As of today, Java is the world's number one server programming language with a 12 million developer community, 5 million students studying worldwide and it's #1 choice for the cloud development.
short x = 999; // -32768 to 32767
int x = 99999; // -2147483648 to 2147483647
long x = 99999999999L; // -9223372036854775808 to 9223372036854775807
float x = 1.2;
double x = 99.99d;
byte x = 99; // -128 to 127
char x = 'A';
boolean x = true;
When ever you want to perform a set of operations based on a condition If-Else is used.
if(conditional-expression) {
// code
} else {
// code
}
Example:
int i = 10;
if(i % 2 == 0) {
System.out.println("i is even number");
} else {
System.out.println("i is odd number");
}
Switch is an alternative to If-Else-If ladder and to select one among many blocks of code.
switch(<conditional-expression>) {
case value1:
// code
break; // optional
case value2:
// code
break; // optional
...
default:
//code to be executed when all the above cases are not matched;
}
For loop is used to iterate a set of statements based on a condition. Usually for loop is preferred when number of iterations is known in advance.
for(Initialization; Condition; Increment/decrement){
//code
}
While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.
while(<condition>){
// code
}
Do-while is also used to iterate a set of statements based on a condition. It is mostly used when you need to execute the statements atleast once.
do {
// code
} while (<condition>);
Class is the blueprint of an object, which is also referred as user-defined data type with variables and functions. Object is a basic unit in OOP, and is an instance of the class.
class
keyword is required to create a class.
class Mobile {
public: // access specifier which specifies that accessibility of class members
string name; // string variable (attribute)
int price; // int variable (attribute)
};
Mobile m1 = new Mobile();
public class Greeting {
static void hello() {
System.out.println("Hello.. Happy learning!");
}
public static void main(String[] args) {
hello();
}
}
Collection is a group of objects which can be represented as a single unit. Collections are introduced to bring a unified common interface to all the objects.
Collection Framework was introduced since JDK 1.2 which is used to represent and manage Collections and it contains:
This framework also defines map interfaces and several classes in addition to Collections.
Collection | Description |
---|---|
Set | Set is a collection of elements which can not contain duplicate values. Set is implemented in HashSets, LinkedHashSets, TreeSet etc |
List | List is a ordered collection of elements which can have duplicates. Lists are classified into ArrayList, LinkedList, Vectors |
Queue | FIFO approach, while instantiating Queue interface you can either choose LinkedList or PriorityQueue. |
Deque | Deque(Double Ended Queue) is used to add or remove elements from both the ends of the Queue(both head and tail) |
Map | Map contains key-values pairs which don't have any duplicates. Map is implemented in HashMap, TreeMap etc. |