crypto

That's how i studied cryptography - by writing code. There are four hash functions (sha256, sha512, ripemd160 and keccak shake128), hash-based message authentication code using sha512, rsa cryptosystem and elliptic curve cryptography. Check the project files.

build

Create source and output directories.

$ mkdir -p src/com/aqoleg/crypto out

Download java files .

$ cd src/com/aqoleg/crypto/
$ wget -i https://crypto.aqoleg.com/src/com/aqoleg/crypto/files.txt
$ cd ../../../../

Compile.

$ javac -d out src/com/aqoleg/crypto/*.java

Create jar file.

$ cd out
$ jar cf ../Crypto.jar ./
$ cd ../

Directories out, src and file Crypto.jar will remain after all.

test

Build the jar file as described above, or download Crypto.jar.

$ wget crypto.aqoleg.com/Crypto.jar

Create test and output directories.

$ mkdir -p test/com/aqoleg/crypto/test out

Download java files.

$ cd test/com/aqoleg/crypto/test
$ wget -i https://crypto.aqoleg.com/test/com/aqoleg/crypto/test/files.txt
$ cd ../../../../../

Compile.

$ javac -d out -cp Crypto.jar test/com/aqoleg/crypto/test/*.java

Run one test or all tests and read results.

$ java -cp out:Crypto.jar com.aqoleg.crypto.test.Sha256Test
$ java -cp out:Crypto.jar com.aqoleg.crypto.test.AllTests

Directories out, test and file Crypto.jar will remain after all.

use

Build the jar file as described above, or download Crypto.jar.

$ wget crypto.aqoleg.com/Crypto.jar

Create example.

$ cat > Example.java

Copy-paste this example, then press ctrl + D.

import com.aqoleg.crypto.Rsa;
public class Example {
  public static void main(String args[]) {
    Rsa.KeyPair keyPair = Rsa.createKeyPair(1200);
    Rsa.PublicKey publicKey = keyPair.getPublicKey();
    String message = "so very secret message";
    System.out.println("message: " + message);
    byte[] encrypted = publicKey.encrypt(message.getBytes());
    System.out.println("encrypted message: " + new String(encrypted));
    byte[] decrypted = keyPair.decrypt(encrypted);
    System.out.println("decrypted message:" + new String(decrypted));
  }
}

Compile.

$ javac -cp Crypto.jar Example.java

Run.

$ java -cp .:Crypto.jar Example

Files Example.class, Example.java, Crypto.jar will remain after all.

one more example

Create source and output directories.

$ mkdir -p src/com/aqoleg/crypto out

Download java files .

$ cd src/com/aqoleg/crypto/
$ wget -i https://crypto.aqoleg.com/src/com/aqoleg/crypto/files.txt
$ cd ../../../../

Download the example: Example.java.

$ wget -O src/Example.java crypto.aqoleg.com/src/Example.java

Compile.

$ javac -d out -cp src src/Example.java

Create jar file.

$ cd out
$ jar cfe ../Example.jar Example ./
$ cd ../

Run and read results 0xcf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e 0xddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f.

$ java -jar Example.jar
$ java -jar Example.jar abc

Directories out, src and file Example.jar will remain after all.