Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import org.mindrot.jbcrypt.BCrypt;
- import sun.misc.BASE64Encoder;
- import javax.crypto.Cipher;
- import javax.crypto.KeyAgreement;
- import javax.crypto.interfaces.DHPublicKey;
- import javax.crypto.spec.DHParameterSpec;
- import javax.crypto.spec.SecretKeySpec;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.io.InputStream;
- import java.nio.charset.StandardCharsets;
- import java.nio.file.Files;
- import java.nio.file.Path;
- import java.nio.file.Paths;
- import java.security.*;
- import java.security.spec.PKCS8EncodedKeySpec;
- import java.security.spec.X509EncodedKeySpec;
- public class DHKeyAgreement2 {
- private DHKeyAgreement2() {}
- private static final int logRounds=12;
- public static String hashpw(String password) {
- return BCrypt.hashpw(password, BCrypt.gensalt(logRounds));
- }
- public static boolean verifyHash(String password, String hash) {
- return BCrypt.checkpw(password, hash);
- }
- public static void main(String argv[]) throws Exception {
- /*
- * Alice creates her own DH key pair with 2048-bit key size
- */
- System.out.println("ALICE: Generate DH keypair ...");
- KeyPairGenerator aliceKpairGen = KeyPairGenerator.getInstance("DH");
- aliceKpairGen.initialize(2048);
- KeyPair aliceKpair = aliceKpairGen.generateKeyPair();
- // Alice creates and initializes her DH KeyAgreement object
- System.out.println("ALICE: Initialization ...");
- KeyAgreement aliceKeyAgree = KeyAgreement.getInstance("DH");
- aliceKeyAgree.init(aliceKpair.getPrivate());
- // Alice encodes her public key, and sends it over to Bob.
- byte[] alicePubKeyEnc = aliceKpair.getPublic().getEncoded();
- /*
- * Let's turn over to Bob. Bob has received Alice's public key
- * in encoded format.
- * He instantiates a DH public key from the encoded key material.
- */
- KeyFactory bobKeyFac = KeyFactory.getInstance("DH");
- X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(alicePubKeyEnc);
- PublicKey alicePubKey = bobKeyFac.generatePublic(x509KeySpec);
- System.out.println("aquiiiiiiiiiiiiii:");
- /*
- * Bob gets the DH parameters associated with Alice's public key.
- * He must use the same parameters when he generates his own key
- * pair.
- */
- DHParameterSpec dhParamFromAlicePubKey = ((DHPublicKey)alicePubKey).getParams();
- // Bob creates his own DH key pair
- System.out.println("BOB: Generate DH keypair ...");
- KeyPairGenerator bobKpairGen = KeyPairGenerator.getInstance("DH");
- bobKpairGen.initialize(dhParamFromAlicePubKey);
- KeyPair bobKpair = bobKpairGen.generateKeyPair();
- // Bob creates and initializes his DH KeyAgreement object
- System.out.println("BOB: Initialization ...");
- KeyAgreement bobKeyAgree = KeyAgreement.getInstance("DH");
- bobKeyAgree.init(bobKpair.getPrivate());
- // Bob encodes his public key, and sends it over to Alice.
- byte[] bobPubKeyEnc = bobKpair.getPublic().getEncoded();
- /*
- * Alice uses Bob's public key for the first (and only) phase
- * of her version of the DH
- * protocol.
- * Before she can do so, she has to instantiate a DH public key
- * from Bob's encoded key material.
- */
- KeyFactory aliceKeyFac = KeyFactory.getInstance("DH");
- x509KeySpec = new X509EncodedKeySpec(bobPubKeyEnc);
- PublicKey bobPubKey = aliceKeyFac.generatePublic(x509KeySpec);
- System.out.println("ALICE: Execute PHASE1 ...");
- aliceKeyAgree.doPhase(bobPubKey, true);
- /*
- * Bob uses Alice's public key for the first (and only) phase
- * of his version of the DH
- * protocol.
- */
- System.out.println("BOB: Execute PHASE1 ...");
- bobKeyAgree.doPhase(alicePubKey, true);
- /*
- * At this stage, both Alice and Bob have completed the DH key
- * agreement protocol.
- * Both generate the (same) shared secret.
- */
- byte[] aliceSharedSecret = aliceKeyAgree.generateSecret();
- int aliceLen = aliceSharedSecret.length;
- byte[] bobSharedSecret = new byte[aliceLen];
- int bobLen;
- bobLen = bobKeyAgree.generateSecret(bobSharedSecret, 0);
- System.out.println("Alice secret: " +
- toHexString(aliceSharedSecret));
- System.out.println("Bob secret: " +
- toHexString(bobSharedSecret));
- if (!java.util.Arrays.equals(aliceSharedSecret, bobSharedSecret))
- throw new Exception("Shared secrets differ");
- System.out.println("Shared secrets are the same");
- System.out.println(aliceLen);
- System.out.println("Use shared secret as SecretKey object ...");
- SecretKeySpec bobAesKey = new SecretKeySpec(bobSharedSecret, 0, 16, "AES");
- SecretKeySpec aliceAesKey = new SecretKeySpec(aliceSharedSecret, 0, 16, "AES");
- /*
- * Bob encrypts, using AES in CBC mode
- */
- Cipher bobCipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
- bobCipher.init(Cipher.ENCRYPT_MODE, bobAesKey);
- byte[] cleartext = "This is just an example".getBytes();
- byte[] ciphertext = bobCipher.doFinal(cleartext);
- // Retrieve the parameter that was used, and transfer it to Alice in
- // encoded format
- byte[] encodedParams = bobCipher.getParameters().getEncoded();
- /*
- * Alice decrypts, using AES in CBC mode
- */
- // Instantiate AlgorithmParameters object from parameter encoding
- // obtained from Bob
- AlgorithmParameters aesParams = AlgorithmParameters.getInstance("AES");
- aesParams.init(encodedParams);
- Cipher aliceCipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
- aliceCipher.init(Cipher.DECRYPT_MODE, aliceAesKey, aesParams);
- byte[] recovered = aliceCipher.doFinal(ciphertext);
- if (!java.util.Arrays.equals(cleartext, recovered))
- throw new Exception("AES in CBC mode recovered text is " +
- "different from cleartext");
- System.out.println("AES in CBC mode recovered text is same as cleartext");
- System.out.println(new String(recovered, StandardCharsets.UTF_8));
- System.out.println("-------------------------------------------------------------");
- KeyPairGenerator kpg=KeyPairGenerator.getInstance("RSA");
- kpg.initialize(2048);
- KeyPair AliceTrueKeyPare=kpg.generateKeyPair();
- String hash=hashpw("password");
- conectBD con=new conectBD();
- con.inserirUser("Alice",hash,AliceTrueKeyPare.getPublic());
- if(con.confirmLogin("Alice","password"))
- System.out.println("fuck yeah boiiiiiii");
- if(toHexString(con.userPublic("Alice").getEncoded()).equals(toHexString(AliceTrueKeyPare.getPublic().getEncoded())))
- System.out.println("sou mesmo bom");
- System.out.println(toHexString(AliceTrueKeyPare.getPublic().getEncoded()));
- System.out.println(toHexString(con.userPublic("Alice").getEncoded()));
- System.out.println("++++++++++++++++++++++++++++++++++++++++++++++++++++++");
- Signature signature=Signature.getInstance("SHA256WithRSA");
- signature.initSign(AliceTrueKeyPare.getPrivate());
- String original="this is the string test";
- System.out.println("original message:"+original);
- try{
- byte[] data=original.getBytes("UTF8");
- signature.update(data);
- byte[] signatureBytes=signature.sign();
- System.out.println("Singature:" + new BASE64Encoder().encode(signatureBytes));
- signature.initVerify(AliceTrueKeyPare.getPublic());
- signature.update(data);
- System.out.println(signature.verify(signatureBytes));
- System.out.println("all good");
- }catch (Exception e){
- System.out.println("rip :( "+e);
- }
- String outFile = "testfile";
- FileOutputStream out = new FileOutputStream(outFile + ".key");
- out.write(AliceTrueKeyPare.getPrivate().getEncoded());
- out.close();
- out = new FileOutputStream(outFile + ".pub");
- out.write(AliceTrueKeyPare.getPublic().getEncoded());
- out.close();
- Path path = Paths.get("testfile.pub");
- byte[] bytes = Files.readAllBytes(path);
- /* Generate public key. */
- X509EncodedKeySpec ks = new X509EncodedKeySpec(bytes);
- KeyFactory kf = KeyFactory.getInstance("RSA");
- PublicKey pub = kf.generatePublic(ks);
- path=Paths.get("testfile.key");
- bytes = Files.readAllBytes(path);
- PKCS8EncodedKeySpec ksp = new PKCS8EncodedKeySpec(bytes);
- KeyFactory kfp = KeyFactory.getInstance("RSA");
- PrivateKey pvt = kfp.generatePrivate(ksp);
- System.out.println(toHexString(pub.getEncoded()).equals(toHexString(AliceTrueKeyPare.getPublic().getEncoded())));
- System.out.println(toHexString(pvt.getEncoded()).equals(toHexString(AliceTrueKeyPare.getPrivate().getEncoded())));
- }
- /*
- * Converts a byte to hex digit and writes to the supplied buffer
- */
- private static void byte2hex(byte b, StringBuffer buf) {
- char[] hexChars = { '0', '1', '2', '3', '4', '5', '6', '7', '8',
- '9', 'A', 'B', 'C', 'D', 'E', 'F' };
- int high = ((b & 0xf0) >> 4);
- int low = (b & 0x0f);
- buf.append(hexChars[high]);
- buf.append(hexChars[low]);
- }
- /*
- * Converts a byte array to hex string
- */
- private static String toHexString(byte[] block) {
- StringBuffer buf = new StringBuffer();
- int len = block.length;
- for (int i = 0; i < len; i++) {
- byte2hex(block[i], buf);
- if (i < len-1) {
- buf.append(":");
- }
- }
- return buf.toString();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement