gpt4 book ai didi

java - 使用 RSA 私钥加密消息是否安全

转载 作者:行者123 更新时间:2023-12-01 11:09:49 24 4
gpt4 key购买 nike

我想分发加密消息,只有我可以加密,但每个人都可以解密。我正在使用以下代码来执行任务。它有效,但我想知道它是否存在任何安全问题?

我知道正常用法是使用 RSA 公钥进行加密,使用私钥进行解密(但我的用例是相反的)。另外我不想在这里使用 java.security.Signature。

用例:我想通过电子邮件将配置文件从服务器发送到客户端,而收件人无法以明文形式读取配置。在安装过程中拥有服务器公钥的应用程序将能够在将配置文件导入配置目录后对其进行解密。

public static final String ALGORITHM = "RSA/ECB/PKCS1Padding";

public Cipher createCipher(final int encryptionMode, final Key key) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException {
final Cipher cipher = Cipher.getInstance(ALGORITHM);
cipher.init(encryptionMode, key);
return cipher;
}

public byte[] encryptString(final String text, final PrivateKey privateKey) throws GeneralSecurityException, IOException {
return createCipher(Cipher.ENCRYPT_MODE, privateKey).doFinal(text.getBytes("UTF-8"));
}

public String decryptString(final byte[] msg, final PublicKey publicKey) throws GeneralSecurityException, IOException {
final byte[] decrypted = createCipher(Cipher.DECRYPT_MODE, publicKey).doFinal(msg);
return new String(decrypted, "UTF-8");
}

// me
final PrivateKey privateKey = ... read from file ...
final byte[] msg = encryptString("my-secret-text-that-everybody-can-read-but-only-I-can-generate", privateKey);

// other person
final PublicKey publicKey = ... read from file ...
final String text = decryptString(msg, publicKey));

最佳答案

感谢@Maarten Bodewes 的评论回答了我的问题。

I'm voting to close this question as off-topic because although the question contains code this really is a conceptual question about cryptography, where it has been asked before – Maarten Bodewes

来自link根据他提供的信息,我的问题的答案是:“不,你通常不能交换公钥和私钥。”

在公共(public)指数中使用小素数只会危害 RSA 的安全性。详细解答如下:

Your public key consists of a public exponent and a modulus. The modulus should be known to the person doing the encryption. The public exponent - in general - is a relatively small prime such as 3 or 65537 (Fourth number of Fermat). So given the modulus all an attacker has to do is guess the public key. It's very easy to factor pretty small exponents (I've written software to generate public keys given a private key). The exponent of the private key on the other hand consists of a number that is about as large as the modulus. It cannot be easily factored; the security of RSA relies on this fact.

关于java - 使用 RSA 私钥加密消息是否安全,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32500680/

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