private byte[] pkcs7UnPad(byte[] buffer) throws BadPaddingException { byte last = buffer[buffer.length - 1]; int i = buffer.length - 2; while (buffer[i] == last) { i--; } if (i + 1 + last != buffer.length) { throw new BadPaddingException(); } return Arrays.copyOfRange(buffer, 0, i + 1); }
/** * Throws an instance of AEADBadTagException. * * @throws BadPaddingException The AEAD exception. * * If the underlying JDK does not have the AEADBadTagException * class, then this function will instead throw an instance of * the superclass BadPaddingException. */ static void throwBadTagException() throws BadPaddingException { try { // java since 1.7; android since API 19 Class<?> c = Class.forName("javax.crypto.AEADBadTagException"); throw (BadPaddingException)(c.getDeclaredConstructor().newInstance()); } catch (ClassNotFoundException e) { } catch (InstantiationException e) { } catch (IllegalAccessException e) { } catch (InvocationTargetException e) { } catch (NoSuchMethodException e) { } throw new BadPaddingException(); } }
public static byte[] unpad80(byte[] text) throws BadPaddingException { if (text.length < 1) throw new BadPaddingException("Invalid ISO 7816-4 padding"); int offset = text.length - 1; while (offset > 0 && text[offset] == 0) { offset--; } if (text[offset] != (byte) 0x80) { throw new BadPaddingException("Invalid ISO 7816-4 padding"); } return Arrays.copyOf(text, offset); }
private byte[] pkcs7UnPad(byte[] buffer) throws BadPaddingException { byte last = buffer[buffer.length - 1]; int i = buffer.length - 2; while (buffer[i] == last) { i--; } if (i + 1 + last != buffer.length) { throw new BadPaddingException(); } return Arrays.copyOfRange(buffer, 0, i + 1); }
private byte[] pkcs7UnPad(byte[] buffer) throws BadPaddingException { byte last = buffer[buffer.length - 1]; int i = buffer.length - 2; while (buffer[i] == last) { i--; } if (i + 1 + last != buffer.length) { throw new BadPaddingException(); } return Arrays.copyOfRange(buffer, 0, i + 1); }
private byte[] pkcs7UnPad(byte[] buffer) throws BadPaddingException { byte last = buffer[buffer.length - 1]; int i = buffer.length - 2; while (buffer[i] == last) { i--; } if (i + 1 + last != buffer.length) { throw new BadPaddingException(); } return Arrays.copyOfRange(buffer, 0, i + 1); }
throw new BadPaddingException("Null remote public key");
private static final String SHA1_PAD = "3021300906052b0e03021a05000414"; private static final byte[] sha1pad = DatatypeConverter.parseHexBinary(SHA1_PAD); public static byte[] unpadSHA1(byte[] padded) throws BadPaddingException { int k = 0; if (padded.length < sha1pad.length) { throw new BadPaddingException("Padding string too short"); } while (true) { if (padded[k] != sha1pad[k]) { break; } k++; if (k == sha1pad.length) { break; } } int n = padded.length - k; if (n > 256) { throw new BadPaddingException("Padding string too short"); } byte[] data = new byte[n]; System.arraycopy(padded, padded.length - n, data, 0, n); return data; }
@Override public String decrypt(final Key key, final byte[] encryptedValue, final byte[] nonce) throws Exception { throw new BadPaddingException(""); } });
public int doFinal(byte[] out, int outOff) throws IllegalStateException, BadPaddingException { try { return cipher.doFinal(out, outOff); } catch (InvalidCipherTextException e) { throw new BadPaddingException(e.getMessage()); } } }
public int doFinal(byte[] out, int outOff) throws IllegalStateException, BadPaddingException { try { return cipher.doFinal(out, outOff); } catch (InvalidCipherTextException e) { throw new BadPaddingException(e.getMessage()); } } }
public 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 int doFinal(byte[] out, int outOff) throws IllegalStateException, BadPaddingException { try { return cipher.doFinal(out, outOff); } catch (InvalidCipherTextException e) { throw new BadPaddingException(e.getMessage()); } } }
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()); } }
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 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()); } }
protected byte[] engineDoFinal( byte[] input, int inputOffset, int inputLen) throws IllegalBlockSizeException, BadPaddingException { cipher.processBytes(input, inputOffset, inputLen); try { return cipher.doFinal(); } catch (InvalidCipherTextException e) { throw new BadPaddingException(e.getMessage()); } }
protected byte[] engineDoFinal( byte[] input, int inputOffset, int inputLen) throws IllegalBlockSizeException, BadPaddingException { cipher.processBytes(input, inputOffset, inputLen); try { return cipher.doFinal(); } catch (InvalidCipherTextException e) { throw new BadPaddingException(e.getMessage()); } }
protected int engineDoFinal( byte[] input, int inputOffset, int inputLen, byte[] output, int outputOffset) throws IllegalBlockSizeException, BadPaddingException { int len = 0; if (inputLen != 0) { len = cipher.processBytes(input, inputOffset, inputLen, output, outputOffset); } try { return len + cipher.doFinal(output, outputOffset + len); } catch (DataLengthException e) { throw new IllegalBlockSizeException(e.getMessage()); } catch (InvalidCipherTextException e) { throw new BadPaddingException(e.getMessage()); } }
@Test public void failsToEncrypt() throws Exception { CryptoUtils cryptoUtils = new CryptoUtils(mContext, mCryptoFactory, Build.VERSION_CODES.KITKAT); when(mCipher.doFinal(any(byte[].class))).thenThrow(new BadPaddingException()); String data = "anythingThatWouldMakeTheCipherFailForSomeReason"; String encryptedData = cryptoUtils.encrypt(data); assertEquals(data, encryptedData); CryptoUtils.DecryptedData decryptedData = cryptoUtils.decrypt(encryptedData, false); assertEquals(data, decryptedData.getDecryptedData()); assertNull(decryptedData.getNewEncryptedData()); decryptedData = cryptoUtils.decrypt(encryptedData, true); assertEquals(data, decryptedData.getDecryptedData()); assertNull(decryptedData.getNewEncryptedData()); }