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