@Test
  public void testAESCipherByteArraySecretKey() {
    try {
      byte[] encrypted = AESCipher.encrypt(MESSAGE, SECRET_KEY);
      assertNotNull(encrypted);
      assertTrue(encrypted.length > 0);

      byte[] result = AESCipher.decrypt(encrypted, SECRET_KEY);
      assertNotNull(result);
      assertTrue(result.length > 0);

      assertArrayEquals(MESSAGE.getBytes(CHARSET), result);
      assertEquals(MESSAGE, new String(result, CHARSET));
    } catch (AESCipherException | UnsupportedEncodingException e) {
      fail(e.getMessage());
    }
  }
示例#2
0
  /* (non-Javadoc)
   * @see net.java.sip.communicator.impl.media.transform.srtp.
   * SRTPCipher#process(byte[], int, int, byte[])
   */
  public void process(byte[] data, int off, int len, byte[] iv) {
    F8Context f8ctx = new F8Context();

    /*
     * Get memory for the derived IV (IV')
     */
    f8ctx.ivAccent = new byte[BLKLEN];

    /*
     * Get memory for the special key. This is the key to compute the
     * derived IV (IV').
     */
    byte[] saltMask = new byte[this.key.length];
    byte[] maskedKey = new byte[this.key.length];

    /*
     * First copy the salt into the mask field, then fill with 0x55 to
     * get a full key.
     */
    System.arraycopy(this.salt, 0, saltMask, 0, this.salt.length);
    for (int i = this.salt.length; i < saltMask.length; ++i) {
      saltMask[i] = 0x55;
    }

    /*
     * XOR the original key with the above created mask to
     * get the special key.
     */
    for (int i = 0; i < this.key.length; i++) {
      maskedKey[i] = (byte) (this.key[i] ^ saltMask[i]);
    }

    /*
     * Prepare the a new AES cipher with the special key to compute IV'
     */
    AESCipher cipher = new AESCipher(maskedKey);

    /*
     * Use the masked key to encrypt the original IV to produce IV'.
     */
    cipher.encryptBlock(iv, 0, f8ctx.ivAccent, 0);

    saltMask = null;
    maskedKey = null;

    f8ctx.J = 0; // initialize the counter
    f8ctx.S = new byte[BLKLEN]; // get the key stream buffer

    Arrays.fill(f8ctx.S, (byte) 0);

    int inLen = len;

    while (inLen >= BLKLEN) {
      processBlock(f8ctx, data, off, data, off, BLKLEN);
      inLen -= BLKLEN;
      off += BLKLEN;
    }

    if (inLen > 0) {
      processBlock(f8ctx, data, off, data, off, inLen);
    }
  }