int outputOffset = cipher.processBytes(input, 0, input.length, output, 0);
int outputOffset = cipher.processBytes(plainText, 0, plainText.length, output, 0);
int bytesEncrypted; while ((bytesRead = in.read(buffer)) != -1) { bytesEncrypted = realCipher.processBytes(buffer, 0, bytesRead, encBuffer, 0); out.write(encBuffer, 0, bytesEncrypted);
int encLen = gcmEngine.processBytes(inMsg, 0, inMsg.length, encMsg, 0); encLen += gcmEngine.doFinal(encMsg, encLen); int decLen = gcmEngine.processBytes(encMsg, 0, encMsg.length, decMsg, 0); decLen += gcmEngine.doFinal(decMsg, decLen);
public static byte[] decrypt(byte[] cipheredApdu, byte[] systemTitle, SecuritySuite securitySuite) throws IOException { byte[] iv = ByteBuffer.allocate(IV_LENGTH).put(systemTitle).put(cipheredApdu, 1, FC_LENGTH).array(); byte[] additionalAssociationData = createAadIfExists(securitySuite); AEADParameters parameters = new AEADParameters(new KeyParameter(securitySuite.getGlobalUnicastEncryptionKey()), IV_LENGTH_BITS, iv, additionalAssociationData); GCMBlockCipher decCipher = createBlockCipher(false, parameters); byte[] dec = new byte[decCipher.getOutputSize(cipheredApdu.length - 5)]; int length = decCipher.processBytes(cipheredApdu, 5, cipheredApdu.length - 5, dec, 0); try { decCipher.doFinal(dec, length); } catch (IllegalStateException | InvalidCipherTextException e) { throw new IOException("Unable to decipher/decrypt xDLMS pdu", e); } return dec; }
int outputOffset = cipher.processBytes(input, 0, input.length, output, 0);
int outputOffset = cipher.processBytes(input, 0, input.length, output, 0);
public static byte[] processPlain(byte[] plaintext, int off, int len, byte[] systemTitle, int frameCounter, SecuritySuite securitySuite, byte apduTag) throws IOException { byte[] frameCounterBytes = ByteBuffer.allocate(4).putInt(frameCounter).array(); byte[] iv = ByteBuffer.allocate(systemTitle.length + 4).put(systemTitle).putInt(frameCounter).array(); byte[] additionalAssociationData = createAadIfExists(securitySuite); AEADParameters parameters = new AEADParameters(new KeyParameter(securitySuite.getGlobalUnicastEncryptionKey()), IV_LENGTH_BITS, iv, additionalAssociationData); GCMBlockCipher encCipher = createBlockCipher(true, parameters); byte[] chipherText = new byte[encCipher.getOutputSize(len)]; int length = encCipher.processBytes(plaintext, off, len, chipherText, 0); try { encCipher.doFinal(chipherText, length); } catch (IllegalStateException | InvalidCipherTextException e) { throw new IOException("Unable to cipher/encrypt xDLMS APDU", e); } int chipherTextLength = chipherText.length; if (!securitySuite.getSecurityPolicy().isAuthenticated()) { chipherTextLength -= 12; } byte[] lengthBytes = AxdrLength.encodeLength(chipherTextLength + 5); final int cipheredApduLength = chipherTextLength + LENGTH_FC_TAG_SC + lengthBytes.length; byte controlByte = securityControlByteFrom(securitySuite); return ByteBuffer.allocate(cipheredApduLength) .put(apduTag) // COSEM PDU type. e.g. GLO_GET_REQUEST .put(lengthBytes) .put(controlByte) .put(frameCounterBytes) .put(chipherText, 0, chipherTextLength) .array(); }
int outputOffset = cipher.processBytes(plainText, 0, plainText.length, output, 0);
int outputOffset = cipher.processBytes(plainText, 0, plainText.length, output, 0);
int outputOffset = cipher.processBytes(input, 0, input.length, output, 0);
int outputOffset = cipher.processBytes(input, 0, input.length, output, 0);
int outputOffset = cipher.processBytes(plainText, 0, plainText.length, output, 0);
int pos = cipher.processBytes(encryptedData, 0, encryptedData.length, out, 0); pos += cipher.processBytes(tag, 0, tag.length, out, pos); pos += cipher.doFinal(out, pos);
int outputOffset = cipher.processBytes(plainText, 0, plainText.length, output, 0);
public static byte[] decrypt(byte[] key, byte[] data) { // TODO utilize GCMAES#decrypt method try { if (data.length < NONCE_LENGTH + TAG_LENGTH) { throw new IllegalArgumentException("data packet too short"); } int cipherTextLength = data.length - NONCE_LENGTH - TAG_LENGTH; byte[] nonce = Arrays.copyOf(data, NONCE_LENGTH); GCMBlockCipher cipher = new GCMBlockCipher(new AESFastEngine()); AEADParameters parameters = new AEADParameters(new KeyParameter(key), TAG_LENGTH * 8, nonce); cipher.init(false, parameters); byte[] out = new byte[cipher.getOutputSize(cipherTextLength + TAG_LENGTH)]; int pos = cipher.processBytes(data, NONCE_LENGTH, data.length - NONCE_LENGTH, out, 0); pos += cipher.doFinal(out, pos); return Arrays.copyOf(out, pos); } catch (IllegalStateException | InvalidCipherTextException ex) { throw new IllegalArgumentException(ex); } } }