我如何开始使用BouncyCastle?

Jef*_*tin 27 cryptography bouncycastle

因此,在CodingHorror加密和捶打评论的乐趣之后,我们正在重新考虑进行自己的加密.

在这种情况下,我们需要将识别用户的一些信息传递给第三方服务,然后第三方服务将使用信息和散列回调到我们网站上的服务.

第二个服务查找该用户的信息,然后将其传递回第三方服务.

我们想要加密进入第三方服务的这些用户信息,并在它出来后对其进行解密.所以它不是一个长期存在的加密.

在编码恐怖文章中,Coda Hale推荐了BouncyCastle和库中的高级抽象,以根据特定需求进行加密.

我的问题是BouncyCastle命名空间很大,而且文档不存在.谁能指点我这个高级抽象库?(或者除了BouncyCastle之外的其他选择?)

Ada*_*ter 12

高级抽象?我想Bouncy Castle图书馆中最高级别的抽象包括:

我大多熟悉Java的Java版本.也许这段代码片段会为您提供足够高的抽象(例如使用AES-256加密):

public byte[] encryptAES256(byte[] input, byte[] key) throws InvalidCipherTextException {
    assert key.length == 32; // 32 bytes == 256 bits
    CipherParameters cipherParameters = new KeyParameter(key);

    /*
     * A full list of BlockCiphers can be found at http://www.bouncycastle.org/docs/docs1.6/org/bouncycastle/crypto/BlockCipher.html
     */
    BlockCipher blockCipher = new AESEngine();

    /*
     * Paddings available (http://www.bouncycastle.org/docs/docs1.6/org/bouncycastle/crypto/paddings/BlockCipherPadding.html):
     *   - ISO10126d2Padding
     *   - ISO7816d4Padding
     *   - PKCS7Padding
     *   - TBCPadding
     *   - X923Padding
     *   - ZeroBytePadding
     */
    BlockCipherPadding blockCipherPadding = new ZeroBytePadding();

    BufferedBlockCipher bufferedBlockCipher = new PaddedBufferedBlockCipher(blockCipher, blockCipherPadding);

    return encrypt(input, bufferedBlockCipher, cipherParameters);
}

public byte[] encrypt(byte[] input, BufferedBlockCipher bufferedBlockCipher, CipherParameters cipherParameters) throws InvalidCipherTextException {
    boolean forEncryption = true;
    return process(input, bufferedBlockCipher, cipherParameters, forEncryption);
}

public byte[] decrypt(byte[] input, BufferedBlockCipher bufferedBlockCipher, CipherParameters cipherParameters) throws InvalidCipherTextException {
    boolean forEncryption = false;
    return process(input, bufferedBlockCipher, cipherParameters, forEncryption);
}

public byte[] process(byte[] input, BufferedBlockCipher bufferedBlockCipher, CipherParameters cipherParameters, boolean forEncryption) throws InvalidCipherTextException {
    bufferedBlockCipher.init(forEncryption, cipherParameters);

    int inputOffset = 0;
    int inputLength = input.length;

    int maximumOutputLength = bufferedBlockCipher.getOutputSize(inputLength);
    byte[] output = new byte[maximumOutputLength];
    int outputOffset = 0;
    int outputLength = 0;

    int bytesProcessed;

    bytesProcessed = bufferedBlockCipher.processBytes(
            input, inputOffset, inputLength,
            output, outputOffset
        );
    outputOffset += bytesProcessed;
    outputLength += bytesProcessed;

    bytesProcessed = bufferedBlockCipher.doFinal(output, outputOffset);
    outputOffset += bytesProcessed;
    outputLength += bytesProcessed;

    if (outputLength == output.length) {
        return output;
    } else {
        byte[] truncatedOutput = new byte[outputLength];
        System.arraycopy(
                output, 0,
                truncatedOutput, 0,
                outputLength
            );
        return truncatedOutput;
    }
}
Run Code Online (Sandbox Code Playgroud)

编辑:哎呀,我刚读了你链接的文章.听起来他说的是比我想象的更高级别的抽象(例如,"发送机密信息").我担心我不太了解他的目标.