Showing posts with label Cryptography. Show all posts
Showing posts with label Cryptography. Show all posts

26/08/2018

[Java] Useful keytool commands

Keytool is a tool to manage Java keystores. Here are some useful commands to:

  • generate a new key and place it in a keystore:
keytool -genkey -keystore myKeystore.jks -storepass myPass -alias myKey -keyalg RSA -sigalg SHA256withRSA -keysize 2048 -validity 3650 -ext ku=digitalSignature

  • verify the content of a keystore:
keytool -list -keystore myKeystore.jks -storepass myPass -v

  • export the public key:
keytool -exportcert -keystore myKeystore.jks -storepass myPass -alias myAlias -rfc -file myPubKey.cer

  • delete a key:
keytool -delete -alias myAlias -keystore myKeystore.jks -storepass myPass

  • add a private key:
keytool -importkeystore -srckeystore myKeystore.jks -srcstorepass myPass -srcalias myAlias -destkeystore myNewKeystore.jks -deststorepass myNewPass -deststoretype JKS -destalias myNewAlias




  • add a public key:
keytool -import -noprompt -alias myAlias -keystore myKeystore.jks -file myPubKey.cer -storepass myPass

07/06/2014

[OpenSSH] Convert putty PPK key to OpenSSH format

To convert a putty-generated PPK key to the OpenSSH format, you can use puttygen:

puttygen MY_PPK_KEY.ppk -O private-openssh -o MY_OpenSSH_KEY

to convert a private key and:

puttygen MY_PPK_KEY.ppk -O public-openssh -o MY_OpenSSH_KEY

to convert a public key

17/12/2013

[Java] Compute discrete logarithm

The discrete logarithm is a widely used concept in cryptography since it's pretty easy and fast to compute when knowing the crypto secrets, but it's otherwise really hard to crack by normal means.

In this example, from Coursera's cryptography I course, we'll try to compute it for some 153-digits numbers using a MITM technique. The exercise is as such:

Your goal this week is to write a program to compute discrete log modulo a prime p. Let g be some element in Z*p and suppose you are given h in Z*p such that h = g^x where x is between 1 and 2^40. A basic brute force attack is to just try all possible values in 2^40 time, but our MITM attack will take just 2^20 time.
Let B=2^20. Since x is less than B^2 we can write the unknown x base B as x=x0 B+x1 where x0,x1 are in the range [0,B-1]. Then h = g^x = g^(x0 B+x1) = (g^B)^x0·g^x1 in Zp. And then we have h/(g^x1) = (g^B) ^ x0 in Zp.
The variables in this equation are x0,x1 and everything else is known: you are given g,h and B=2^20. Since the variables x0 and x1 are now on different sides of the equation we can find a solution using meet in the middle:

  • First build a hash table of all possible values of the left hand side h/(g^x1) for x1=0,1,…,2^20. 
  • Then for each value x0=0,1,2,…,2^20 check if the right hand side (g^B)^x0 is in this hash table. If so, then you have found a solution (x0,x1) from which you can compute the required x as x=x0B+x1.
We will now find x such that h = g^x.

[Java] Hashing with SHA-256

As for the cryptography algorithms, Java offers native libraries for hashing too.

In this example I'll describe how to use apply the SHA-256 hashing to a file. However, following the example found in Coursera's cryptography I course, we won't compute the whole file's hash at once; instead, we'll break the file into 1KB blocks (1024 bytes). Then we'll compute the hash of the last block and appended said value to the second to last block. Then again we'll compute the hash of this augmented second to last block and the resulting hash is appended to the third block from the end, and so on..

Update: Hashing this way doesn't make the hash stronger or weaker, but allows us to stream the file while hashing instead of having to wait for the whole operation to complete.

As usual, auxiliary functions such as string2Hex can be found here.
The basic imports are:
 import java.io.ByteArrayInputStream;  
import java.io.DataInputStream;  
import java.io.File;  
import java.security.MessageDigest;  
import java.util.LinkedList;  
import java.util.List;  
import javax.xml.bind.DatatypeConverter;  
import org.apache.commons.io.FileUtils;  

Also note, we're using Apache commons library to handle our File operations

[Java] AES encryption and decryption

To perform AES encryption and decryption, without implementing your own algorithms - since it is usually unsafe - you may use the standard Java crypto libraries. A more complete crypto reference is found in the JCA reference guide.