gpt4 book ai didi

cryptography - 如何开始使用 BouncyCaSTLe?

转载 作者:行者123 更新时间:2023-12-03 06:21:01 29 4
gpt4 key购买 nike

所以在CodingHorror's fun with encryption之后和激烈的评论,我们正在重新考虑进行我们自己的加密。

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

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

我们希望对进入第 3 方服务的用户信息进行加密,并在其出来后对其进行解密。所以它不是一种长期存在的加密。

在编码恐怖文章中,Coda Hale 推荐了 BouncyCaSTLe 和库中的高级抽象来针对特定需求进行加密。

我的问题是 BouncyCaSTLe 命名空间很大并且文档不存在。谁能指出我这个高级抽象库? (或者除了 BouncyCaSTLe 之外还有其他选择吗?)

最佳答案

高级抽象?我想 Bouncy CaSTLe 库中的最高级别的抽象将包括:

我最熟悉该库的 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;
}
}

编辑:哎呀,我刚刚读了您链接到的文章。听起来他正在谈论比我想象的更高层次的抽象(例如,“发送 secret 消息”)。恐怕我不太明白他的意思。

关于cryptography - 如何开始使用 BouncyCaSTLe?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/885485/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com