Skip to content

Instantly share code, notes, and snippets.

@halysongoncalves
Created July 14, 2016 12:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save halysongoncalves/d09ece187e3b6ddcfc59f1b070681313 to your computer and use it in GitHub Desktop.
Save halysongoncalves/d09ece187e3b6ddcfc59f1b070681313 to your computer and use it in GitHub Desktop.
public class SessionController {
private static final String BOB = "bob";
private static final String ALICE = "alice";
private static final String KEY_PUBLIC = "KEY_PUBLIC";
private static final String KEY_PRIVATE = "KEY_PRIVATE";
private static final String PROVIDER = "SC";
private static final String CURVE = "secp256K1";
private static final String ALGORITHM = "ECDH";
private final ECParameterSpec ecNamedCurveParameterSpec = ECNamedCurveTable.getParameterSpec(CURVE);
static {
Security.addProvider(new BouncyCastleProvider());
}
@AfterInject
void afterInject() {
clearKey();
generateKey();
readKey();
}
void generateKey() {
try {
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(ALGORITHM, PROVIDER);
keyPairGenerator.initialize(new ECGenParameterSpec(CURVE), new SecureRandom());
Log.d(SessionController.class.getSimpleName(), "<<<<<<<<<<GENERATE KEYS>>>>>>>>>>");
//Alice
Log.d(SessionController.class.getSimpleName(), "<<<<<<<<<<Alice>>>>>>>>>>");
KeyPair pairA = keyPairGenerator.generateKeyPair();
String publicKeyAlice = bytesToHex((((ECPublicKey) pairA.getPublic()).getQ().getEncoded(true)));
String privateKeyAlice = bytesToHex((((ECPrivateKey) pairA.getPrivate()).getD().toByteArray()));
Hawk.put(ALICE + KEY_PUBLIC, publicKeyAlice);
Hawk.put(ALICE + KEY_PRIVATE, privateKeyAlice);
Log.d(SessionController.class.getSimpleName(), "Alice Pub: " + publicKeyAlice);
Log.d(SessionController.class.getSimpleName(), "Alice Prv: " + privateKeyAlice);
//Bob
Log.d(SessionController.class.getSimpleName(), "<<<<<<<<<<Bob>>>>>>>>>>");
KeyPair pairB = keyPairGenerator.generateKeyPair();
String publicKeyBob = bytesToHex((((ECPublicKey) pairB.getPublic()).getQ().getEncoded(true)));
String privateKeyBob = bytesToHex((((ECPrivateKey) pairB.getPrivate()).getD().toByteArray()));
Hawk.put(BOB + KEY_PUBLIC, publicKeyBob);
Hawk.put(BOB + KEY_PRIVATE, privateKeyBob);
Log.d(SessionController.class.getSimpleName(), "Bob Pub: " + publicKeyBob);
Log.d(SessionController.class.getSimpleName(), "Bob Prv: " + privateKeyBob);
} catch (Exception exception) {
Log.e(SessionController.class.getSimpleName(), exception.getMessage());
Crashlytics.logException(exception);
}
}
void readKey() {
try {
Log.d(SessionController.class.getSimpleName(), "<<<<<<<<<<READ KEYS>>>>>>>>>>");
//Alice
Log.d(SessionController.class.getSimpleName(), "<<<<<<<<<<Alice>>>>>>>>>>");
byte[] publicKeyAlice = hexToBytes(Hawk.get(ALICE + KEY_PUBLIC));
byte[] privateKeyAlice = hexToBytes(Hawk.get(ALICE + KEY_PRIVATE));
Log.d(SessionController.class.getSimpleName(), "Alice Pub: " + Arrays.toString(publicKeyAlice));
Log.d(SessionController.class.getSimpleName(), "Alice Prv: " + Arrays.toString(privateKeyAlice));
//Bob
Log.d(SessionController.class.getSimpleName(), "<<<<<<<<<<Bob>>>>>>>>>>");
byte[] publicKeyBob = hexToBytes(Hawk.get(BOB + KEY_PUBLIC));
byte[] privateKeyBob = hexToBytes(Hawk.get(BOB + KEY_PRIVATE));
Log.d(SessionController.class.getSimpleName(), "Bob Pub: " + Arrays.toString(publicKeyBob));
Log.d(SessionController.class.getSimpleName(), "Bob Prv: " + Arrays.toString(privateKeyBob));
doECDH("Alice secret: ", convertByteToPublicKey(publicKeyBob), convertByteToPrivateKey(privateKeyAlice));
doECDH("Bob secret: ", convertByteToPublicKey(publicKeyAlice), convertByteToPrivateKey(privateKeyBob));
} catch (Exception exception) {
Log.e(SessionController.class.getSimpleName(), exception.getMessage());
Crashlytics.logException(exception);
}
}
public void clearKey(){
Hawk.clear();
}
private String bytesToHex(byte[] bytes) {
try {
return new String(Hex.encode(bytes), "UTF-8");
} catch (UnsupportedEncodingException unsupportedEncodingException) {
Log.e(SessionController.class.getSimpleName(), unsupportedEncodingException.getMessage());
Crashlytics.logException(unsupportedEncodingException);
}
return "";
}
private byte[] hexToBytes(String hex) {
int length = hex.length();
byte[] data = new byte[length / 2];
for (int i = 0; i < length; i += 2) {
data[i / 2] = (byte) ((Character.digit(hex.charAt(i), 16) << 4)
+ Character.digit(hex.charAt(i + 1), 16));
}
return data;
}
private PublicKey convertByteToPublicKey(byte[] data) throws Exception {
return KeyFactory.getInstance(ALGORITHM, PROVIDER).generatePublic(new ECPublicKeySpec(ecNamedCurveParameterSpec.getCurve().decodePoint(data), ecNamedCurveParameterSpec));
}
private PrivateKey convertByteToPrivateKey(byte[] data) throws Exception {
return KeyFactory.getInstance(ALGORITHM, PROVIDER).generatePrivate(new ECPrivateKeySpec(new BigInteger(data), ecNamedCurveParameterSpec));
}
private void doECDH(String name, PublicKey publicKey, PrivateKey privateKey) throws Exception {
KeyAgreement keyAgreement = KeyAgreement.getInstance(ALGORITHM, PROVIDER);
keyAgreement.init(privateKey);
keyAgreement.doPhase(publicKey, true);
byte[] secret = keyAgreement.generateSecret();
Log.d(SessionController.class.getSimpleName(), name + bytesToHex(secret));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment