当前位置: 首页>>代码示例>>Java>>正文


Java InvalidCipherTextException.getMessage方法代码示例

本文整理汇总了Java中org.spongycastle.crypto.InvalidCipherTextException.getMessage方法的典型用法代码示例。如果您正苦于以下问题:Java InvalidCipherTextException.getMessage方法的具体用法?Java InvalidCipherTextException.getMessage怎么用?Java InvalidCipherTextException.getMessage使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在org.spongycastle.crypto.InvalidCipherTextException的用法示例。


在下文中一共展示了InvalidCipherTextException.getMessage方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: aesEncrypt

import org.spongycastle.crypto.InvalidCipherTextException; //导入方法依赖的package包/类
public static byte[] aesEncrypt(byte[] plaintext, byte[] myPrivateKey, byte[] theirPublicKey, byte[] nonce) {
    try {
        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);
        byte[] iv = new byte[16];
        secureRandom.get().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);
    }
}
 
开发者ID:filipnyquist,项目名称:lbry-android,代码行数:26,代码来源:Crypto.java

示例2: aesDecrypt

import org.spongycastle.crypto.InvalidCipherTextException; //导入方法依赖的package包/类
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);
    }
}
 
开发者ID:filipnyquist,项目名称:lbry-android,代码行数:28,代码来源:Crypto.java

示例3: asymDecrypt

import org.spongycastle.crypto.InvalidCipherTextException; //导入方法依赖的package包/类
/**
 * decrypt data with asymmetric key. create asymmetrically encrypted data:<br>
 * <ul>
 * <li>OAEP padding [42 bytes] (RSA-encrypted)</li>
 * <li>Symmetric key [16 bytes]</li>
 * <li>First part of data [70 bytes]</li>
 * <li>Second part of data [x-70 bytes] (Symmetrically encrypted)</li>
 * </ul>
 * encrypt and store in result
 *
 * @param priv key to use for decryption
 * @param data to be decrypted, needs currently to be at least 70 bytes long
 * @return raw data
 */
public static byte[] asymDecrypt(final RSAPrivateKey priv, final byte[] data) throws TorException {

    if (data == null) {
        throw new NullPointerException("can't encrypt NULL data");
    }
    if (data.length < 70) {
        throw new TorException("input array too short");
    }

    try {
        int encryptedBytes = 0;

        // init OAEP
        final OAEPEncoding oaep = new OAEPEncoding(new RSAEngine());
        oaep.init(false, new RSAKeyParameters(true, priv.getModulus(), priv.getPrivateExponent()));
        // apply RSA+OAEP
        encryptedBytes = oaep.getInputBlockSize();
        final byte[] oaepInput = new byte[encryptedBytes];
        System.arraycopy(data, 0, oaepInput, 0, encryptedBytes);
        final byte[] part1 = oaep.decodeBlock(oaepInput, 0, encryptedBytes);

        // extract symmetric key
        final byte[] symmetricKey = new byte[16];
        System.arraycopy(part1, 0, symmetricKey, 0, 16);
        // init AES
        final AESCounterMode aes = new AESCounterMode(symmetricKey);
        // apply AES
        final byte[] aesInput = new byte[data.length - encryptedBytes];
        System.arraycopy(data, encryptedBytes, aesInput, 0, aesInput.length);
        final byte[] part2 = aes.processStream(aesInput);

        // replace unencrypted data
        final byte[] result = new byte[part1.length - 16 + part2.length];
        System.arraycopy(part1, 16, result, 0, part1.length - 16);
        System.arraycopy(part2, 0, result, part1.length - 16, part2.length);

        return result;

    } catch (final InvalidCipherTextException e) {
        LOG.error("Encryption.asymDecrypt(): can't decrypt cipher text:" + e.getMessage());
        throw new TorException("Encryption.asymDecrypt(): InvalidCipherTextException:" + e.getMessage());
    }
}
 
开发者ID:B4dT0bi,项目名称:silvertunnel-ng,代码行数:58,代码来源:Encryption.java


注:本文中的org.spongycastle.crypto.InvalidCipherTextException.getMessage方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。