英文:
Is there any way to get the plain text from signed data using private key?
问题
以下是用 java.security.Signature
进行签名的纯文本部分。下面是用于对纯文本进行签名的代码示例:
public String getSignature(String plainText) throws Exception
{
KeyStore keyStore = loadKeyStore(); // 一个从文件系统中读取密钥库文件的本地方法。
PrivateKey privateKey = (PrivateKey) keyStore.getKey(KEY_ALIAS_IN_KEYSTORE, KEYSTORE_PASSWORD.toCharArray());
Signature privateSignature = Signature.getInstance(SIGNATUREALGO);
privateSignature.initSign(privateKey);
privateSignature.update(plainText.getBytes("UTF-8"));
byte[] signature = privateSignature.sign();
return String.valueOf(signature);
// KEY_ALIAS_IN_KEYSTORE、KEYSTORE_PASSWORD 和 SIGNATUREALGO 都是常量字符串
}
注意 1: 我在网上找到了一种使用公钥验证签名的方法 Java 代码示例 java.security.Signature#verify()。但这不是我所需要的。
注意 2: 我还找到了一种在此处提到的加密和解密方式 Java 中的 RSA 签名和加密。但我手头的用例是从已签名数据中获取原始纯文本。这是否可能?
英文:
The plain text is signed using java.security.Signature
. Below is the code used to sign the plain text
public String getSignature(String plainText) throws Exception
{
KeyStore keyStore = loadKeyStore(); // A local method to read the keystore file from file system.
PrivateKey privateKey = (PrivateKey) keyStore.getKey(KEY_ALIAS_IN_KEYSTORE, KEYSTORE_PASSWORD.toCharArray());
Signature privateSignature = Signature.getInstance(SIGNATUREALGO);
privateSignature.initSign(privateKey);
privateSignature.update(plainText.getBytes("UTF-8"));
byte[] signature = privateSignature.sign();
return String.valueOf(signature);
// KEY_ALIAS_IN_KEYSTORE, KEYSTORE_PASSWORD and SIGNATUREALGO are all constant Strings
}
Note 1: I found online a way to verify the signature using the public key Java Code Examples for java.security.Signature#verify(). But this is not what I require.
Note 2: I also found a ways to encrypt and decrypt as mentioned here RSA Signing and Encryption in Java. But the use case I have in hand is to get the original plain text from a signed data. Is that possible?
答案1
得分: 4
不,仅凭签名无法检索到原始内容。
仅有签名并不包含足够的信息来恢复原始明文,无论您有哪些访问权限的密钥。
签名的基本思想是将其与明文一起发送。这意味着明文将是可见的,但可以使用签名来验证消息是由声称这样做的人编写(或至少签署)的,并且自那时以来尚未被篡改。
签署与加密是不同的。两者经常使用相同或相关的技术,都属于密码学范畴。
英文:
No, you can't retrieve the original content from just the signature.
The signature alone does not contain enough information to restore the original clear text, no matter what keys you have access to.
The basic idea of a signature is to send it together with the clear text. That means the clear text will be visible, but the signature can be used to verify that the message was written (or at least signed) by who claims to have done so and has not been tampered with since then.
Signing something is different from encrypting it. The two often uses the same or related technologies and both fall under cryptography.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论