use of javax.crypto.BadPaddingException in project robovm by robovm.
the class AlgorithmParameterSymmetricHelper method test.
@Override
public void test(AlgorithmParameters parameters) {
KeyGenerator generator = null;
try {
generator = KeyGenerator.getInstance(algorithmName);
} catch (NoSuchAlgorithmException e) {
Assert.fail(e.getMessage());
}
generator.init(keySize);
Key key = generator.generateKey();
Cipher cipher = null;
try {
String transformation = algorithmName;
if (blockmode != null) {
transformation += "/" + blockmode;
}
cipher = Cipher.getInstance(transformation);
} catch (NoSuchAlgorithmException e) {
Assert.fail(e.getMessage());
} catch (NoSuchPaddingException e) {
Assert.fail(e.getMessage());
}
try {
cipher.init(Cipher.ENCRYPT_MODE, key, parameters);
} catch (InvalidKeyException e) {
Assert.fail(e.getMessage());
} catch (InvalidAlgorithmParameterException e) {
Assert.fail(e.getMessage());
}
byte[] bs = null;
try {
bs = cipher.doFinal(plainData.getBytes());
} catch (IllegalBlockSizeException e) {
Assert.fail(e.getMessage());
} catch (BadPaddingException e) {
Assert.fail(e.getMessage());
}
try {
cipher.init(Cipher.DECRYPT_MODE, key, parameters);
} catch (InvalidKeyException e) {
Assert.fail(e.getMessage());
} catch (InvalidAlgorithmParameterException e) {
Assert.fail(e.getMessage());
}
byte[] decrypted = null;
try {
decrypted = cipher.doFinal(bs);
} catch (IllegalBlockSizeException e) {
Assert.fail(e.getMessage());
} catch (BadPaddingException e) {
Assert.fail(e.getMessage());
}
Assert.assertTrue(Arrays.equals(plainData.getBytes(), decrypted));
}
use of javax.crypto.BadPaddingException in project jgnash by ccavanaugh.
the class EncryptionManager method encrypt.
/**
* Encrypts the supplied string.
*
* @param plain String to encrypt
* @return the encrypted string
*/
public String encrypt(final String plain) {
try {
final Cipher cipher = Cipher.getInstance(ENCRYPTION_ALGORITHM);
cipher.init(Cipher.ENCRYPT_MODE, key);
return printBase64Binary(cipher.doFinal(plain.getBytes(StandardCharsets.UTF_8)));
} catch (final InvalidKeyException | NoSuchAlgorithmException | NoSuchPaddingException | BadPaddingException | IllegalBlockSizeException e) {
logger.log(Level.SEVERE, e.getMessage(), e);
}
return null;
}
use of javax.crypto.BadPaddingException in project jgnash by ccavanaugh.
the class EncryptionManager method decrypt.
/**
* Decrypts the supplied string.
*
* @param encrypted String to decrypt
* @return The decrypted string of {@code DECRYPTION_ERROR_TAG} if decryption fails
* @see #DECRYPTION_ERROR_TAG
*/
public String decrypt(final String encrypted) {
try {
final Cipher cipher = Cipher.getInstance(ENCRYPTION_ALGORITHM);
cipher.init(Cipher.DECRYPT_MODE, key);
return new String(cipher.doFinal(parseBase64Binary(encrypted)), StandardCharsets.UTF_8);
} catch (final InvalidKeyException | NoSuchAlgorithmException | NoSuchPaddingException | BadPaddingException | IllegalBlockSizeException e) {
logger.log(Level.SEVERE, "Invalid password");
return DECRYPTION_ERROR_TAG;
}
}
use of javax.crypto.BadPaddingException in project jdk8u_jdk by JetBrains.
the class RSAPadding method unpadOAEP.
/**
* PKCS#1 v2.1 OAEP unpadding (MGF1).
*/
private byte[] unpadOAEP(byte[] padded) throws BadPaddingException {
byte[] EM = padded;
boolean bp = false;
int hLen = lHash.length;
if (EM[0] != 0) {
bp = true;
}
int seedStart = 1;
int seedLen = hLen;
int dbStart = hLen + 1;
int dbLen = EM.length - dbStart;
mgf1(EM, dbStart, dbLen, EM, seedStart, seedLen);
mgf1(EM, seedStart, seedLen, EM, dbStart, dbLen);
// verify lHash == lHash'
for (int i = 0; i < hLen; i++) {
if (lHash[i] != EM[dbStart + i]) {
bp = true;
}
}
int padStart = dbStart + hLen;
int onePos = -1;
for (int i = padStart; i < EM.length; i++) {
int value = EM[i];
if (onePos == -1) {
if (value == 0x00) {
// continue;
} else if (value == 0x01) {
onePos = i;
} else {
// Anything other than {0,1} is bad.
bp = true;
}
}
}
// We either ran off the rails or found something other than 0/1.
if (onePos == -1) {
bp = true;
// Don't inadvertently return any data.
onePos = EM.length - 1;
}
int mStart = onePos + 1;
// copy useless padding array for a constant-time method
byte[] tmp = new byte[mStart - padStart];
System.arraycopy(EM, padStart, tmp, 0, tmp.length);
byte[] m = new byte[EM.length - mStart];
System.arraycopy(EM, mStart, m, 0, m.length);
BadPaddingException bpe = new BadPaddingException("Decryption error");
if (bp) {
throw bpe;
} else {
return m;
}
}
use of javax.crypto.BadPaddingException in project jdk8u_jdk by JetBrains.
the class RSAPadding method mgf1.
/**
* Compute MGF1 using mgfMD as the message digest.
* Note that we combine MGF1 with the XOR operation to reduce data
* copying.
*
* We generate maskLen bytes of MGF1 from the seed and XOR it into
* out[] starting at outOfs;
*/
private void mgf1(byte[] seed, int seedOfs, int seedLen, byte[] out, int outOfs, int maskLen) throws BadPaddingException {
// 32 bit counter
byte[] C = new byte[4];
byte[] digest = new byte[mgfMd.getDigestLength()];
while (maskLen > 0) {
mgfMd.update(seed, seedOfs, seedLen);
mgfMd.update(C);
try {
mgfMd.digest(digest, 0, digest.length);
} catch (DigestException e) {
// should never happen
throw new BadPaddingException(e.toString());
}
for (int i = 0; (i < digest.length) && (maskLen > 0); maskLen--) {
out[outOfs++] ^= digest[i++];
}
if (maskLen > 0) {
// increment counter
for (int i = C.length - 1; (++C[i] == 0) && (i > 0); i--) {
// empty
}
}
}
}
Aggregations