throw new InvalidCipherTextException("unable to recover ephemeral public key: " + e.getMessage(), e);
public int doFinal(byte[] out, int outOff) throws DataLengthException, IllegalStateException { try { return ccm.doFinal(out, 0); } catch (InvalidCipherTextException e) { throw new IllegalStateException("exception on doFinal(): " + e.toString()); } }
public int doFinal(byte[] out, int outOff) throws IllegalStateException, BadPaddingException { try { return cipher.doFinal(out, outOff); } catch (InvalidCipherTextException e) { if (aeadBadTagConstructor != null) { BadPaddingException aeadBadTag = null; try { aeadBadTag = (BadPaddingException)aeadBadTagConstructor .newInstance(new Object[]{e.getMessage()}); } catch (Exception i) { // Shouldn't happen, but fall through to BadPaddingException } if (aeadBadTag != null) { throw aeadBadTag; } } throw new BadPaddingException(e.getMessage()); } } }
public static byte[] aesDecrypt(byte[] ivCiphertext, byte[] myPrivateKey, byte[] theirPublicKey, byte[] nonce) { try { if (ivCiphertext.length < 16 || ivCiphertext.length % 16 != 0) { throw new InvalidCipherTextException("invalid ciphertext"); } byte[] iv = Arrays.copyOfRange(ivCiphertext, 0, 16); byte[] ciphertext = Arrays.copyOfRange(ivCiphertext, 16, ivCiphertext.length); byte[] dhSharedSecret = new byte[32]; Curve25519.curve(dhSharedSecret, myPrivateKey, theirPublicKey); for (int i = 0; i < 32; i++) { dhSharedSecret[i] ^= nonce[i]; } byte[] key = sha256().digest(dhSharedSecret); PaddedBufferedBlockCipher aes = new PaddedBufferedBlockCipher(new CBCBlockCipher( new AESEngine())); CipherParameters ivAndKey = new ParametersWithIV(new KeyParameter(key), iv); aes.init(false, ivAndKey); byte[] output = new byte[aes.getOutputSize(ciphertext.length)]; int plaintextLength = aes.processBytes(ciphertext, 0, ciphertext.length, output, 0); plaintextLength += aes.doFinal(output, plaintextLength); byte[] result = new byte[plaintextLength]; System.arraycopy(output, 0, result, 0, result.length); return result; } catch (InvalidCipherTextException e) { throw new RuntimeException(e.getMessage(), e); } }
decryptedNetworkPayload = SecureUtils.decryptCCM(transportPdu, encryptionKey, nonce, micLength); } catch (InvalidCipherTextException ex) { throw new ExtendedInvalidCipherTextException(ex.getMessage(), ex.getCause(), TAG); throw new ExtendedInvalidCipherTextException(ex.getMessage(), ex.getCause(), TAG);
private static byte[] cipherData(BufferedBlockCipher cipher, byte[] data) { int minSize = cipher.getOutputSize(data.length); byte[] outBuf = new byte[minSize]; int len1 = cipher.processBytes(data, 0, data.length, outBuf, 0); int len2 = -1; try { len2 = cipher.doFinal(outBuf, len1); } catch (InvalidCipherTextException icte) { icte.printStackTrace(); } int actualLength = len1 + len2; byte[] result = new byte[actualLength]; System.arraycopy(outBuf, 0, result, 0, result.length); return result; }
protected byte[] engineDoFinal( byte[] input, int inputOffset, int inputLen) throws IllegalBlockSizeException, BadPaddingException { if (inputLen != 0) { buffer.write(input, inputOffset, inputLen); } try { byte[] buf = buffer.toByteArray(); buffer.reset(); return cipher.processBlock(buf, 0, buf.length); } catch (InvalidCipherTextException e) { throw new BadPaddingException(e.getMessage()); } }
public static byte[] aesDecrypt(byte[] ivCiphertext, byte[] myPrivateKey, byte[] theirPublicKey, byte[] nonce) { try { if (ivCiphertext.length < 16 || ivCiphertext.length % 16 != 0) { throw new InvalidCipherTextException("invalid ciphertext"); } byte[] iv = Arrays.copyOfRange(ivCiphertext, 0, 16); byte[] ciphertext = Arrays.copyOfRange(ivCiphertext, 16, ivCiphertext.length); byte[] dhSharedSecret = new byte[32]; Curve25519.curve(dhSharedSecret, myPrivateKey, theirPublicKey); for (int i = 0; i < 32; i++) { dhSharedSecret[i] ^= nonce[i]; } byte[] key = sha256().digest(dhSharedSecret); PaddedBufferedBlockCipher aes = new PaddedBufferedBlockCipher(new CBCBlockCipher( new AESEngine())); CipherParameters ivAndKey = new ParametersWithIV(new KeyParameter(key), iv); aes.init(false, ivAndKey); byte[] output = new byte[aes.getOutputSize(ciphertext.length)]; int plaintextLength = aes.processBytes(ciphertext, 0, ciphertext.length, output, 0); plaintextLength += aes.doFinal(output, plaintextLength); byte[] result = new byte[plaintextLength]; System.arraycopy(output, 0, result, 0, result.length); return result; } catch (InvalidCipherTextException e) { throw new RuntimeException(e.getMessage(), e); } }
throw new ExtendedInvalidCipherTextException(ex.getMessage(), ex.getCause(), TAG);
public static byte[] encryptCCM(final byte[] data, final byte[] key, final byte[] nonce) { final byte[] ccm = new byte[25 + 8]; final ByteBuffer buffer = ByteBuffer.allocate(ccm.length + 8); CCMBlockCipher ccmBlockCipher = new CCMBlockCipher(new AESEngine()); AEADParameters aeadParameters = new AEADParameters(new KeyParameter(key), 64, nonce); ccmBlockCipher.init(true, aeadParameters); ccmBlockCipher.processBytes(data, 0, data.length, ccm, data.length); try { ccmBlockCipher.doFinal(ccm, 0); } catch (InvalidCipherTextException e) { e.printStackTrace(); } return ccm; }
throw new InvalidCipherTextException("Length of input must be greater than the MAC"); throw new InvalidCipherTextException("Invalid MAC.");
public int doFinal(byte[] out, int outOff) throws IllegalStateException, BadPaddingException { try { return cipher.doFinal(out, outOff); } catch (InvalidCipherTextException e) { throw new BadPaddingException(e.getMessage()); } } }
decryptedNetworkPayload = SecureUtils.decryptCCM(transportPdu, encryptionKey, networkNonce, micLength); } catch (InvalidCipherTextException ex) { throw new ExtendedInvalidCipherTextException(ex.getMessage(), ex.getCause(), TAG); throw new ExtendedInvalidCipherTextException(ex.getMessage(), ex.getCause(), TAG);
public int doFinal(byte[] out, int outOff) throws DataLengthException, IllegalStateException { try { return cipher.doFinal(out, outOff); } catch (InvalidCipherTextException e) { // Impossible in encrypt mode throw new IllegalStateException(e.toString()); } }
@Test public void publishXpub() throws Exception { Contacts contacts = init(); LinkedList<String> responseList = new LinkedList<>(); responseList.add(fail);//magic - string doesn't matter 404 will be caught responseList.add(success);//put metadata mockInterceptor.setResponseStringList(responseList); LinkedList<Integer> responseCodeList = new LinkedList<>(); responseCodeList.add(404);//fetch magic - 404 = new magic hash responseCodeList.add(200); mockInterceptor.setResponseCodeList(responseCodeList); try { contacts.publishXpub(); Assert.assertTrue(true); } catch (MetadataException e) { e.printStackTrace(); Assert.fail(); } catch (IOException e) { e.printStackTrace(); Assert.fail(); } catch (InvalidCipherTextException e) { e.printStackTrace(); Assert.fail(); } }
/** * return the number of pad bytes present in the block. */ public int padCount(byte[] in) throws InvalidCipherTextException { int count = in[in.length - 1] & 0xff; if (count > in.length) { throw new InvalidCipherTextException("pad block corrupted"); } return count; } }
protected int engineDoFinal( byte[] input, int inputOffset, int inputLen, byte[] output, int outputOffset) throws IllegalBlockSizeException, BadPaddingException { if (inputLen != 0) { buffer.write(input, inputOffset, inputLen); } try { byte[] buf = buffer.toByteArray(); buffer.reset(); buf = cipher.processBlock(buf, 0, buf.length); System.arraycopy(buf, 0, output, outputOffset, buf.length); return buf.length; } catch (InvalidCipherTextException e) { throw new BadPaddingException(e.getMessage()); } }
decryptedNetworkPayload = SecureUtils.decryptCCM(transportPdu, encryptionKey, networkNonce, micLength); } catch (InvalidCipherTextException ex) { throw new ExtendedInvalidCipherTextException(ex.getMessage(), ex.getCause(), TAG); throw new ExtendedInvalidCipherTextException(ex.getMessage(), ex.getCause(), TAG);
public int doFinal(byte[] out, int outOff) throws DataLengthException, IllegalStateException { try { return cipher.doFinal(out, outOff); } catch (InvalidCipherTextException e) { // Impossible in encrypt mode throw new IllegalStateException(e.toString()); } }
@Test public void publishXpub_IOException() throws Exception { Contacts contacts = init(); try { mockInterceptor.setIOException(true); contacts.publishXpub(); } catch (MetadataException e) { e.printStackTrace(); } catch (IOException e) { Assert.assertTrue(true); return; } catch (InvalidCipherTextException e) { e.printStackTrace(); } finally { mockInterceptor.setIOException(false); } Assert.fail(); }