Guest User

Untitled

a guest
Jan 10th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.98 KB | None | 0 0
  1.    @Command
  2.     public void startsecure() throws Exception {
  3.  
  4.         SecureRandom secureRandom = new SecureRandom();
  5.         this.clientChallenge = new byte[32];
  6.         secureRandom.nextBytes(this.clientChallenge);
  7.  
  8.         this.initVector = new byte[16];
  9.         secureRandom.nextBytes(this.initVector);
  10.  
  11.         KeyGenerator generator = KeyGenerator.getInstance("AES");
  12.         generator.init(256);  // KEYSIZE is in bits
  13.         this.secretKey = generator.generateKey();
  14.  
  15.         try {
  16.             //establish connection
  17.             mailboxWriter.println("startsecure\r");
  18.             mailboxWriter.flush();// 1
  19.             String[] response = mailbodxReader.readLine().split(" "); // ok <component-id>
  20.  
  21.             if (!response[0].equals("ok")) {
  22.                 //throw exception here
  23.             }
  24.                
  25.             this.publicServerKey = Keys.readPublicKey(new File("./keys/client/" + response[1] + ".pub"));
  26.  
  27.             String challengeBase64 = new String(Base64.encodeMessage(clientChallenge), StandardCharsets.UTF_8);
  28.             String initVectorBase64 = new String(Base64.encodeMessage(initVector), StandardCharsets.UTF_8);
  29.             String secretKeyBase64 = new String(Base64.encodeMessage(secretKey.getEncoded()), StandardCharsets.UTF_8);
  30.  
  31.             String msg = "ok " + challengeBase64 + " " + initVectorBase64 + " " + secretKeyBase64;
  32.  
  33.  
  34.             System.out.println(msg + " " + Base64.decodeMessage(challengeBase64.getBytes()) + " " + msg.getBytes());
  35.             byte[] b = msg.getBytes(Charset.forName("UTF-8"));
  36.             String serverMessage =  new String(RSAMethod.encodeMessage(publicServerKey,b), StandardCharsets.UTF_8);
  37.  
  38.             mailboxWriter.println("Test" + serverMessage + "\r");  // 3
  39.             mailboxWriter.flush();
  40.  
  41.  
  42.  
  43.           //  mailboxWriter.println("login" + this.mailboxUser + " " + mailboxPassword +"\r");
  44.  
  45.  
  46.         } catch (IOException e) {
  47.             e.printStackTrace();
  48.         }
  49.  
  50.         //login
  51.  
  52.  
  53.  
  54.     }
Advertisement
Add Comment
Please, Sign In to add comment