org.bouncycastle.util.Arrays.copyOfRange()方法的使用及代码示例

x33g5p2x  于2022-01-16 转载在 其他  
字(10.2k)|赞(0)|评价(0)|浏览(211)

本文整理了Java中org.bouncycastle.util.Arrays.copyOfRange()方法的一些代码示例,展示了Arrays.copyOfRange()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Arrays.copyOfRange()方法的具体详情如下:
包路径:org.bouncycastle.util.Arrays
类名称:Arrays
方法名:copyOfRange

Arrays.copyOfRange介绍

[英]Make a copy of a range of bytes from the passed in data array. The range can extend beyond the end of the input array, in which case the return array will be padded with zeroes.
[中]从传入的数据数组中复制一个字节范围。该范围可以扩展到输入数组的末尾之外,在这种情况下,返回数组将用零填充。

代码示例

代码示例来源:origin: cloudfoundry/uaa

public byte[] decrypt(byte[] encrypt) throws EncryptionServiceException {
  try {
    byte[] myNonce = new byte[GCM_IV_NONCE_SIZE_BYTES];
    byte[] mySalt = new byte[PBKDF2_SALT_SIZE_BYTES];
    ByteArrayInputStream fileInputStream = new ByteArrayInputStream(encrypt);
    fileInputStream.read(myNonce);
    fileInputStream.read(mySalt);
    SecretKey key = new SecretKeySpec(generateKey(mySalt), CIPHER);
    Cipher myCipher = Cipher.getInstance(CIPHERSCHEME);
    GCMParameterSpec spec = new GCMParameterSpec(GCM_AUTHENTICATION_TAG_SIZE_BITS, myNonce);
    myCipher.init(Cipher.DECRYPT_MODE, key, spec);
    return myCipher.doFinal(Arrays.copyOfRange(encrypt, GCM_IV_NONCE_SIZE_BYTES + PBKDF2_SALT_SIZE_BYTES, encrypt.length));
  } catch (Exception e) {
    logger.error("Decryption failed", e);
    throw new EncryptionServiceException(e);
  }
}

代码示例来源:origin: ZhangJiupeng/AgentX

public byte[] unwrapFromResponse(final byte[] bytes) {
  // caution: placeholder bytes' end-pos must less than 200
  String fuzzyHeader = new String(Arrays.copyOfRange(bytes, 0, 200));
  if (!fuzzyHeader.startsWith(Http.VERSION_1_1)) {
    throw new RuntimeException("unknown format");
  }
  fuzzyHeader = fuzzyHeader.substring(fuzzyHeader.indexOf("Content-Length: ") + "Content-Length: ".length());
  fuzzyHeader = fuzzyHeader.substring(0, fuzzyHeader.indexOf(Http.CRLF));
  int rawLen = Integer.parseInt(fuzzyHeader);
  return Arrays.copyOfRange(bytes, bytes.length - rawLen, bytes.length);
}

代码示例来源:origin: org.bouncycastle/bcprov-debug-jdk15on

protected BigInteger decodeValue(BigInteger n, byte[] buf, int off, int len)
{
  byte[] bs = Arrays.copyOfRange(buf, off, off + len);
  return checkValue(n, new BigInteger(1, bs));
}

代码示例来源:origin: wangweiX/blockchain-java

/**
 * 生成公钥的校验码
 *
 * @param payload
 * @return
 */
public static byte[] checksum(byte[] payload) {
  return Arrays.copyOfRange(doubleHash(payload), 0, 4);
}

代码示例来源:origin: org.bouncycastle/bcprov-debug-jdk15on

public byte[] readString()
{
  int len = readU32();
  if (len == 0)
  {
    return new byte[0];
  }
  if (pos + len > buffer.length)
  {
    throw new IllegalArgumentException("not enough data for string");
  }
  return Arrays.copyOfRange(buffer, pos, pos += len);
}

代码示例来源:origin: org.bouncycastle/bcprov-debug-jdk15on

public byte[] encodePlaintext(long seqNo, short type, byte[] plaintext, int offset, int len)
  throws IOException
{
  if (writeMac == null)
  {
    return Arrays.copyOfRange(plaintext, offset, offset + len);
  }
  byte[] mac = writeMac.calculateMac(seqNo, type, plaintext, offset, len);
  byte[] ciphertext = new byte[len + mac.length];
  System.arraycopy(plaintext, offset, ciphertext, 0, len);
  System.arraycopy(mac, 0, ciphertext, len, mac.length);
  return ciphertext;
}

代码示例来源:origin: org.bouncycastle/bcprov-debug-jdk15on

public byte[] readPaddedString()
{
  int len = readU32();
  if (len == 0)
  {
    return new byte[0];
  }
  if (pos + len > buffer.length)
  {
    throw new IllegalArgumentException("not enough data for string");
  }
  return Arrays.copyOfRange(buffer, pos, pos += (len - (buffer[pos + len - 1] & 0xff)));
}

代码示例来源:origin: redfish64/TinyTravelTracker

public byte[] encodePlaintext(long seqNo, short type, byte[] plaintext, int offset, int len)
  throws IOException
{
  if (writeMac == null)
  {
    return Arrays.copyOfRange(plaintext, offset, offset + len);
  }
  byte[] mac = writeMac.calculateMac(seqNo, type, plaintext, offset, len);
  byte[] ciphertext = new byte[len + mac.length];
  System.arraycopy(plaintext, offset, ciphertext, 0, len);
  System.arraycopy(mac, 0, ciphertext, len, mac.length);
  return ciphertext;
}

代码示例来源:origin: io.nessus/nessus-cipher

@Override
  public void nextBytes(byte[] buffer) {
    
    if (512 * 1024 < total)
      throw new IllegalStateException("Upper limit exceeded");
    
    int idx = 0;
    byte[] seed = new byte[0];
    while (seed.length < buffer.length) {
      idx = (idx + 7) % digest.length;
      byte[] head = Arrays.copyOfRange(digest, 0, idx);
      byte[] tail = Arrays.copyOfRange(digest, idx, digest.length);
      digest = Arrays.concatenate(tail, head);
      seed = Arrays.concatenate(seed, md.digest(digest));
    }
    
    System.arraycopy(seed, 0, buffer, 0, buffer.length);
    total += buffer.length;
  }
}

代码示例来源:origin: RUB-NDS/TLS-Attacker

public static List<SSL2CipherSuite> getCiphersuites(byte[] values) {
  List<SSL2CipherSuite> cipherSuites = new LinkedList<>();
  int pointer = 0;
  while (pointer < values.length) {
    byte[] suiteBytes = Arrays.copyOfRange(values, pointer, pointer + SSL2CipherSuiteLength);
    int suiteValue = ArrayConverter.bytesToInt(suiteBytes);
    cipherSuites.add(getCipherSuite(suiteValue));
    pointer += SSL2CipherSuiteLength;
  }
  return cipherSuites;
}

代码示例来源:origin: horrorho/InflatableDonkey

Optional<byte[]> type2(byte[] chunkEncryptionKey, byte[] keyEncryptionKey) {
    if (chunkEncryptionKey.length != 0x19) {
      logger.warn("-- type2() - bad chunk encryption key length: 0x:{}", Hex.toHexString(chunkEncryptionKey));
      return Optional.empty();
    }
    byte[] wrappedKey = Arrays.copyOfRange(chunkEncryptionKey, 0x01, 0x19);
    return RFC3394Wrap.unwrapAES(keyEncryptionKey, wrappedKey)
        .map(u -> {
          byte[] k = new byte[0x11];
          k[0] = 0x01;
          System.arraycopy(u, 0, k, 1, u.length);
          return k;
        });
  }
}

代码示例来源:origin: org.bouncycastle/bcprov-debug-jdk15on

/**
 * Generate a key parameter derived from the password, salt, and iteration
 * count we are currently initialised with.
 *
 * @param keySize the size of the key we want (in bits)
 * @return a KeyParameter object.
 */
public CipherParameters generateDerivedParameters(
  int keySize)
{
  keySize = keySize / 8;
  byte[]  dKey = Arrays.copyOfRange(generateDerivedKey(keySize), 0, keySize);
  return new KeyParameter(dKey, 0, keySize);
}

代码示例来源:origin: redfish64/TinyTravelTracker

public byte[] decodeCiphertext(long seqNo, short type, byte[] ciphertext, int offset, int len)
    throws IOException
  {
    if (readMac == null)
    {
      return Arrays.copyOfRange(ciphertext, offset, offset + len);
    }

    int macSize = readMac.getSize();
    if (len < macSize)
    {
      throw new TlsFatalAlert(AlertDescription.decode_error);
    }

    int macInputLen = len - macSize;

    byte[] receivedMac = Arrays.copyOfRange(ciphertext, offset + macInputLen, offset + len);
    byte[] computedMac = readMac.calculateMac(seqNo, type, ciphertext, offset, macInputLen);

    if (!Arrays.constantTimeAreEqual(receivedMac, computedMac))
    {
      throw new TlsFatalAlert(AlertDescription.bad_record_mac);
    }

    return Arrays.copyOfRange(ciphertext, offset, offset + macInputLen);
  }
}

代码示例来源:origin: org.bouncycastle/bcprov-debug-jdk15on

protected void checkMAC(long seqNo, short type, byte[] recBuf, int recStart, int recEnd, byte[] calcBuf, int calcOff, int calcLen)
  throws IOException
{
  byte[] receivedMac = Arrays.copyOfRange(recBuf, recStart, recEnd);
  byte[] computedMac = readMac.calculateMac(seqNo, type, calcBuf, calcOff, calcLen);
  if (!Arrays.constantTimeAreEqual(receivedMac, computedMac))
  {
    throw new TlsFatalAlert(AlertDescription.bad_record_mac);
  }
}

代码示例来源:origin: redfish64/TinyTravelTracker

protected void checkMAC(long seqNo, short type, byte[] recBuf, int recStart, int recEnd, byte[] calcBuf, int calcOff, int calcLen)
  throws IOException
{
  byte[] receivedMac = Arrays.copyOfRange(recBuf, recStart, recEnd);
  byte[] computedMac = readMac.calculateMac(seqNo, type, calcBuf, calcOff, calcLen);
  if (!Arrays.constantTimeAreEqual(receivedMac, computedMac))
  {
    throw new TlsFatalAlert(AlertDescription.bad_record_mac);
  }
}

代码示例来源:origin: org.bouncycastle/bcprov-debug-jdk15on

public byte[] decodeCiphertext(long seqNo, short type, byte[] ciphertext, int offset, int len)
  throws IOException
{
  if (usesNonce)
  {
    updateIV(decryptCipher, false, seqNo);
  }
  int macSize = readMac.getSize();
  if (len < macSize)
  {
    throw new TlsFatalAlert(AlertDescription.decode_error);
  }
  int plaintextLength = len - macSize;
  byte[] deciphered = new byte[len];
  decryptCipher.processBytes(ciphertext, offset, len, deciphered, 0);
  checkMAC(seqNo, type, deciphered, plaintextLength, len, deciphered, 0, plaintextLength);
  return Arrays.copyOfRange(deciphered, 0, plaintextLength);
}

代码示例来源:origin: org.bouncycastle/bcprov-debug-jdk15on

public static ECField convertField(FiniteField field)
{
  if (ECAlgorithms.isFpField(field))
  {
    return new ECFieldFp(field.getCharacteristic());
  }
  else //if (ECAlgorithms.isF2mField(curveField))
  {
    Polynomial poly = ((PolynomialExtensionField)field).getMinimalPolynomial();
    int[] exponents = poly.getExponentsPresent();
    int[] ks = Arrays.reverse(Arrays.copyOfRange(exponents, 1, exponents.length - 1));
    return new ECFieldF2m(poly.getDegree(), ks);
  }
}

代码示例来源:origin: org.bouncycastle/bcprov-debug-jdk15on

private static ECField convertField(FiniteField field)
{
  if (ECAlgorithms.isFpField(field))
  {
    return new ECFieldFp(field.getCharacteristic());
  }
  else //if (ECAlgorithms.isF2mField(curveField))
  {
    Polynomial poly = ((PolynomialExtensionField)field).getMinimalPolynomial();
    int[] exponents = poly.getExponentsPresent();
    int[] ks = Arrays.reverse(Arrays.copyOfRange(exponents, 1, exponents.length - 1));
    return new ECFieldF2m(poly.getDegree(), ks);
  }
}

代码示例来源:origin: redfish64/TinyTravelTracker

public byte[] decodeCiphertext(long seqNo, short type, byte[] ciphertext, int offset, int len) throws IOException
{
  if (getPlaintextLimit(len) < 0)
  {
    throw new TlsFatalAlert(AlertDescription.decode_error);
  }
  int plaintextLength = len - 16;
  byte[] receivedMAC = Arrays.copyOfRange(ciphertext, offset + plaintextLength, offset + len);
  KeyParameter macKey = initRecordMAC(decryptCipher, false, seqNo);
  byte[] additionalData = getAdditionalData(seqNo, type, plaintextLength);
  byte[] calculatedMAC = calculateRecordMAC(macKey, additionalData, ciphertext, offset, plaintextLength);
  if (!Arrays.constantTimeAreEqual(calculatedMAC, receivedMAC))
  {
    throw new TlsFatalAlert(AlertDescription.bad_record_mac);
  }
  byte[] output = new byte[plaintextLength];
  decryptCipher.processBytes(ciphertext, offset, plaintextLength, output, 0);
  return output;
}

代码示例来源:origin: org.bouncycastle/bcprov-debug-jdk15on

public byte[] decodeCiphertext(long seqNo, short type, byte[] ciphertext, int offset, int len) throws IOException
{
  if (getPlaintextLimit(len) < 0)
  {
    throw new TlsFatalAlert(AlertDescription.decode_error);
  }
  KeyParameter macKey = initRecord(decryptCipher, false, seqNo, decryptIV);
  int plaintextLength = len - 16;
  byte[] additionalData = getAdditionalData(seqNo, type, plaintextLength);
  byte[] calculatedMAC = calculateRecordMAC(macKey, additionalData, ciphertext, offset, plaintextLength);
  byte[] receivedMAC = Arrays.copyOfRange(ciphertext, offset + plaintextLength, offset + len);
  if (!Arrays.constantTimeAreEqual(calculatedMAC, receivedMAC))
  {
    throw new TlsFatalAlert(AlertDescription.bad_record_mac);
  }
  byte[] output = new byte[plaintextLength];
  decryptCipher.processBytes(ciphertext, offset, plaintextLength, output, 0);
  return output;
}

相关文章