使用 RSA Cipher 的线程安全
Thread safety with RSA Cipher
正如标题所说,我想知道如何在给定 RSA 算法的多线程环境中最好地使用 Cipher 实例。
我已经阅读了关于该主题的几个问题,并且从我收集到的内容中:
Cipher 不是线程安全的,因为它保持内部状态,而 encrypting/decrypting
如果使用 AES,则 doFinal()
会将初始化向量重置为最后一个已知值,因此每次都应生成新的密码实例
我的问题
如果选择的算法是 RSA,只调用一次 cipher.init()
可以吗?这与下面代码中所示的第二个项目符号相反。我相信是这样,因为没有初始化向量。此外,密码将仅用于解密数据。
我应该只在 cipher.doFinal()
调用时同步吗?
- 处理请求加密服务的多个线程的常用方法是什么,我是否应该在某些代理后面使用密码池作为阻塞queue?
代码示例:
public class RsaPrototype {
private static PrivateKey privKey;
private static Cipher cipher;
private static final String PRIVATE_KEY_PATH ="./privK.pem";
/*
* ALGORITHM/BLOCKING_MODE/PADDING_SCHEMA
*/
private static final String CIPHER_SPECIFICATION = "RSA/None/NoPadding";
private static final String RSA_PROVIDER ="BC";
static {
Security.addProvider(new BouncyCastleProvider());
importPrivateKey();
cipher = getCipher();
}
/**
* Initializes cipher with RSA algorithm, without blocking mode and padding.
* Implementation provider is bouncy castle.
*
* @return cipher instance.
*/
private static Cipher getCipher() {
try
{
Cipher cipher = Cipher.getInstance(CIPHER_SPECIFICATION, RSA_PROVIDER);
cipher.init(Cipher.DECRYPT_MODE, privKey);
return cipher;
} catch (NoSuchAlgorithmException | NoSuchPaddingException | NoSuchProviderException e) {
throw new RuntimeException(e.getMessage());
}
}
/**
* Imports public key from the given .PEM file into application cache.
*/
private static void importPrivateKey() {
try (BufferedReader reader =
new BufferedReader(new FileReader(PRIVATE_KEY_PATH));
PEMParser pemParser =
new PEMParser(reader);) {
privKey = new JcaPEMKeyConverter().getPrivateKey((PrivateKeyInfo) pemParser.readObject());
}
catch (IOException ignorable) {
// not handled
}
}
public static String decrypt(byte[] encryptedText) {
byte[] plainText;
synchronized (cipher) {
plainText = cipher.doFinal(encryptedText);
}
return new String(plainText, StandardCharsets.UTF_8);
}
}
加密(和写入消息)本质上是同步的。使用多线程不是我的方法。考虑一条消息 aabb。使用多个线程可能会变成 abba 或 baba 或 abab 或 bbaa。请注意,密码的内部状态也是以这种方式同步的...要取出 aabb,您必须发送 aabb。
如果其他人读到这篇文章,则不需要为每次使用 RSA 的调用重新初始化密码。虽然,密码池可用于提高性能。
我已经编写了快速负载测试来验证这一点。
看来在cipher.doInit()
上同步,用单个Cipher实例解密就够了
private static Queue<String> results = new ConcurrentLinkedQueue<String>();
@Test
public void test() throws InterruptedException {
String plainText = "some text to verify data integrity";
String encryptedText = Encrypt.encrypt(plainText);
for (int i = 0; i < 5000; i++) {
new Thread( () -> { results.add(Decrypt.decrypt(encryptedText)); })
.start();;
}
Thread.sleep(5000);
assertTrue(results.size() == 5000);
while(!results.isEmpty()) {
assertTrue(results.poll().equals(plainText));
}
}
正如标题所说,我想知道如何在给定 RSA 算法的多线程环境中最好地使用 Cipher 实例。
我已经阅读了关于该主题的几个问题,并且从我收集到的内容中:
Cipher 不是线程安全的,因为它保持内部状态,而 encrypting/decrypting
如果使用 AES,则
doFinal()
会将初始化向量重置为最后一个已知值,因此每次都应生成新的密码实例
我的问题
如果选择的算法是 RSA,只调用一次
cipher.init()
可以吗?这与下面代码中所示的第二个项目符号相反。我相信是这样,因为没有初始化向量。此外,密码将仅用于解密数据。我应该只在
cipher.doFinal()
调用时同步吗?- 处理请求加密服务的多个线程的常用方法是什么,我是否应该在某些代理后面使用密码池作为阻塞queue?
代码示例:
public class RsaPrototype {
private static PrivateKey privKey;
private static Cipher cipher;
private static final String PRIVATE_KEY_PATH ="./privK.pem";
/*
* ALGORITHM/BLOCKING_MODE/PADDING_SCHEMA
*/
private static final String CIPHER_SPECIFICATION = "RSA/None/NoPadding";
private static final String RSA_PROVIDER ="BC";
static {
Security.addProvider(new BouncyCastleProvider());
importPrivateKey();
cipher = getCipher();
}
/**
* Initializes cipher with RSA algorithm, without blocking mode and padding.
* Implementation provider is bouncy castle.
*
* @return cipher instance.
*/
private static Cipher getCipher() {
try
{
Cipher cipher = Cipher.getInstance(CIPHER_SPECIFICATION, RSA_PROVIDER);
cipher.init(Cipher.DECRYPT_MODE, privKey);
return cipher;
} catch (NoSuchAlgorithmException | NoSuchPaddingException | NoSuchProviderException e) {
throw new RuntimeException(e.getMessage());
}
}
/**
* Imports public key from the given .PEM file into application cache.
*/
private static void importPrivateKey() {
try (BufferedReader reader =
new BufferedReader(new FileReader(PRIVATE_KEY_PATH));
PEMParser pemParser =
new PEMParser(reader);) {
privKey = new JcaPEMKeyConverter().getPrivateKey((PrivateKeyInfo) pemParser.readObject());
}
catch (IOException ignorable) {
// not handled
}
}
public static String decrypt(byte[] encryptedText) {
byte[] plainText;
synchronized (cipher) {
plainText = cipher.doFinal(encryptedText);
}
return new String(plainText, StandardCharsets.UTF_8);
}
}
加密(和写入消息)本质上是同步的。使用多线程不是我的方法。考虑一条消息 aabb。使用多个线程可能会变成 abba 或 baba 或 abab 或 bbaa。请注意,密码的内部状态也是以这种方式同步的...要取出 aabb,您必须发送 aabb。
如果其他人读到这篇文章,则不需要为每次使用 RSA 的调用重新初始化密码。虽然,密码池可用于提高性能。
我已经编写了快速负载测试来验证这一点。
看来在cipher.doInit()
上同步,用单个Cipher实例解密就够了
private static Queue<String> results = new ConcurrentLinkedQueue<String>();
@Test
public void test() throws InterruptedException {
String plainText = "some text to verify data integrity";
String encryptedText = Encrypt.encrypt(plainText);
for (int i = 0; i < 5000; i++) {
new Thread( () -> { results.add(Decrypt.decrypt(encryptedText)); })
.start();;
}
Thread.sleep(5000);
assertTrue(results.size() == 5000);
while(!results.isEmpty()) {
assertTrue(results.poll().equals(plainText));
}
}