Java Code Examples for org.bouncycastle.crypto.InvalidCipherTextException#getMessage()

The following examples show how to use org.bouncycastle.crypto.InvalidCipherTextException#getMessage() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BurstCryptoImpl.java    From burstkit4j with Apache License 2.0 6 votes vote down vote up
@Override
public byte[] aesEncrypt(byte[] plaintext, byte[] signingKey, byte[] nonce) {
    if (signingKey.length != 32) {
        throw new IllegalArgumentException("Key length must be 32 bytes");
    }
    try {
        for (int i = 0; i < 32; i++) {
            signingKey[i] ^= nonce[i];
        }
        byte[] key = getSha256().digest(signingKey);
        byte[] iv = new byte[16];
        secureRandom.nextBytes(iv);
        PaddedBufferedBlockCipher aes = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESEngine()));
        CipherParameters ivAndKey = new ParametersWithIV(new KeyParameter(key), iv);
        aes.init(true, ivAndKey);
        byte[] output = new byte[aes.getOutputSize(plaintext.length)];
        int ciphertextLength = aes.processBytes(plaintext, 0, plaintext.length, output, 0);
        ciphertextLength += aes.doFinal(output, ciphertextLength);
        byte[] result = new byte[iv.length + ciphertextLength];
        System.arraycopy(iv, 0, result, 0, iv.length);
        System.arraycopy(output, 0, result, iv.length, ciphertextLength);
        return result;
    } catch (InvalidCipherTextException e) {
        throw new RuntimeException(e.getMessage(), e);
    }
}
 
Example 2
Source File: BurstCryptoImpl.java    From burstkit4j with Apache License 2.0 6 votes vote down vote up
@Override
public byte[] aesDecrypt(byte[] encrypted, byte[] signingKey, byte[] nonce) {
    if (signingKey.length != 32) {
        throw new IllegalArgumentException("Key length must be 32 bytes");
    }
    try {
        if (encrypted.length < 16 || encrypted.length % 16 != 0) {
            throw new InvalidCipherTextException("invalid ciphertext");
        }
        byte[] iv = Arrays.copyOfRange(encrypted, 0, 16);
        byte[] ciphertext = Arrays.copyOfRange(encrypted, 16, encrypted.length);
        for (int i = 0; i < 32; i++) {
            signingKey[i] ^= nonce[i];
        }
        byte[] key = getSha256().digest(signingKey);
        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);
    }
}
 
Example 3
Source File: SM2.java    From julongchain with Apache License 2.0 5 votes vote down vote up
/**
 * 公钥加密消息
 *
 * @param input
 * @param publicLKey
 * @return
 */
public byte[] encrypt(byte[] input, byte[] publicLKey) throws CspException {
    if (null == input) {
        throw new CspException("input is null");
    }
    if (input.length == 0) {
        throw new CspException("input data length is 0");
    }
    if (null==publicLKey) {
        throw new CspException("publicLKey is null");
    }
    if (publicLKey.length == 0) {
        throw new CspException("publicLKey's length is 0");
    }
    SM2Engine sm2Engine = new SM2Engine();
    ECPublicKeyParameters ecPub = new ECPublicKeyParameters(byte2ECpoint(publicLKey), ecc_bc_spec);
    ParametersWithRandom parametersWithRandom = new ParametersWithRandom(ecPub);
    sm2Engine.init(true, parametersWithRandom);
    byte[] enc = null;
    try {
        enc = sm2Engine.processBlock(input, 0, input.length);
    } catch (InvalidCipherTextException e) {
        log.error(e.getMessage());
        throw new CspException(e.getMessage());
    }
    return enc;
}
 
Example 4
Source File: Encryptor.java    From zeppelin with Apache License 2.0 5 votes vote down vote up
public String encrypt(String inputString) throws IOException {
  byte[] input = inputString.getBytes();
  byte[] result = new byte[encryptCipher.getOutputSize(input.length)];
  int size = encryptCipher.processBytes(input, 0, input.length, result, 0);

  try {
    size += encryptCipher.doFinal(result, size);

    byte[] out = new byte[size];
    System.arraycopy(result, 0, out, 0, size);
    return new String(Base64.encode(out));
  } catch (InvalidCipherTextException e) {
    throw new IOException("Cannot encrypt: " + e.getMessage(), e);
  }
}
 
Example 5
Source File: Encryptor.java    From zeppelin with Apache License 2.0 5 votes vote down vote up
public String decrypt(String base64Input) throws IOException {
  byte[] input = Base64.decode(base64Input);
  byte[] result = new byte[decryptCipher.getOutputSize(input.length)];
  int size = decryptCipher.processBytes(input, 0, input.length, result, 0);

  try {
    size += decryptCipher.doFinal(result, size);

    byte[] out = new byte[size];
    System.arraycopy(result, 0, out, 0, size);
    return new String(out);
  } catch (InvalidCipherTextException e) {
    throw new IOException("Cannot decrypt: " + e.getMessage(), e);
  }
}