Package tahrir.tools

Examples of tahrir.tools.ByteArraySegment$ByteArraySegmentBuilder


  public ByteArraySegment decrypt(final ByteArraySegment toDecrypt) {
    try {
      final Cipher cipher = Cipher.getInstance(CIPHER_NAME);
      final IvParameterSpec ivSpec = new IvParameterSpec(toDecrypt.array, toDecrypt.offset, BLOCK_SIZE);
      cipher.init(Cipher.DECRYPT_MODE, skey, ivSpec);
      return new ByteArraySegment(cipher.doFinal(toDecrypt.array, toDecrypt.offset+BLOCK_SIZE, toDecrypt.length-BLOCK_SIZE));
    } catch (final Exception e) {
      throw new RuntimeException(e);
    }
  }
View Full Code Here


      throw new RuntimeException(e);
    }
  }

  public ByteArraySegment toByteArraySegment() {
    return new ByteArraySegment(toBytes());
  }
View Full Code Here

    final TrSymKey key = TrCrypto.createAesKey();
    final Random r = new Random();
    final byte[] plainText_ = new byte[512];
    r.nextBytes(plainText_);
    System.out.format("PlainText size: %s bytes%n", plainText_.length);
    final ByteArraySegment plainText = new ByteArraySegment(plainText_);
    final ByteArraySegment cypherText = key.encrypt(plainText);
    System.out.format("CypherText size: %s bytes%n", cypherText.length);
    final ByteArraySegment decryptedCypherText = key.decrypt(cypherText);
    Assert.assertEquals(decryptedCypherText, plainText);
  }
View Full Code Here

      public void run() {
        // Warning: We're assuming that the other node has cached
        // our outboundSymKey here.  Save assumption?  Not sure :-/
        final byte[] msg = new byte[1];
        msg[0] = PrimitiveMessageType.KEEPALIVE.id;
        final ByteArraySegment plainText = new ByteArraySegment(msg);
        final ByteArraySegment cipherText = encryptOutbound(plainText);
        iface.sendTo(remoteAddr, cipherText, TrNetworkInterface.CONNECTION_MAINTAINANCE_PRIORITY);
      }
    }, TrConstants.UDP_KEEP_ALIVE_DURATION, TimeUnit.SECONDS);
  }
View Full Code Here

    }
    keepAliveSender.cancel(false);
    shutdown = true;
    final byte[] msg = new byte[1];
    msg[0] = PrimitiveMessageType.SHUTDOWN.id;
    final ByteArraySegment plainText = new ByteArraySegment(msg);
    final ByteArraySegment cipherText = encryptOutbound(plainText);
    iface.sendTo(remoteAddress, cipherText, TrNetworkInterface.CONNECTION_MAINTAINANCE_PRIORITY);

    if (!unregisterScheduled) {
      unregisterScheduled = true;
      TrUtils.executor.schedule(new Runnable() {
View Full Code Here

      PrimitiveMessageType.SHORT.write(builder);
      final int messageId = TrUtils.rand.nextInt();
      builder.writeInt(messageId);
      ShortMessageType.SIMPLE.write(builder);
      builder.write(message);
            ByteArraySegment basMessage = encryptOutbound(builder.build());
            final Resender resender = new Resender(messageId, TrConstants.UDP_SHORT_MESSAGE_RETRY_ATTEMPTS, sentListener,
                    basMessage, this, priority);
      resenders.put(messageId, resender);
      resender.run();
    }
View Full Code Here

    if (!remoteHasCachedOurOutboundSymKey) {
      logger.debug("Remote hasn't yet cached our outboundSymKey, prepend it");
      toSend.write(TrCrypto.encryptRaw(outboundSymKey.toByteArraySegment(), remotePubKey));
    }
    toSend.write(outboundSymKey.encrypt(rawMessage));
        ByteArraySegment bas = toSend.build();
        return bas;
  }
View Full Code Here

    KeyGenerator kgen;
    try {
      kgen = KeyGenerator.getInstance("AES");
      kgen.init(128);
      final SecretKey skey = kgen.generateKey();
      return new TrSymKey(new ByteArraySegment(skey.getEncoded()));
    } catch (final NoSuchAlgorithmException e) {
      throw new RuntimeException(e);
    }
  }
View Full Code Here

    final ByteArraySegmentBuilder dos = ByteArraySegment.builder();
    try {
      TrSerializer.serializeTo(plainText, dos);
      dos.flush();
      final TrSymKey aesKey = createAesKey();
      final ByteArraySegment aesEncrypted = aesKey.encrypt(dos.build());
      final Cipher cipher = getRSACipher();
      cipher.init(Cipher.ENCRYPT_MODE, pubKey);
      final byte[] rsaEncryptedAesKey = cipher.doFinal(aesKey.toBytes());
      return new TrPPKEncrypted<T>(rsaEncryptedAesKey, aesEncrypted);
    } catch (final Exception e) {
View Full Code Here

  public static ByteArraySegment encryptRaw(final ByteArraySegment pt, final RSAPublicKey pubKey) {
    final Cipher cipher = getRSACipher();
    try {
      cipher.init(Cipher.ENCRYPT_MODE, pubKey);
      return new ByteArraySegment(cipher.doFinal(pt.array, pt.offset, pt.length));
    } catch (final Exception e) {
      throw new RuntimeException(e);
    }
  }
View Full Code Here

TOP

Related Classes of tahrir.tools.ByteArraySegment$ByteArraySegmentBuilder

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.