[英]Decrypt digital sign bouncycastle
I'm using code from this answer there is an example how to sign and verify signature, but how could i decrypt this kind of signature using Bouncycastle? 我使用的是此答案中的代码,这里有一个示例,说明如何签名和验证签名,但是如何使用Bouncycastle解密这种签名? There is no such method in
java.security.Signature
class. java.security.Signature
类中没有这样的方法。
I think that you mean that you are looking for encryption/decryption sample with bouncycastle instead of a signature/verification sample which you are referencing in your question. 我认为您的意思是您正在寻找带有bouncycastle的加密/解密样本,而不是您在问题中引用的签名/验证样本。 In order to do it you can use
javax.crypto.Cipher
class instead of java.security.Signature
I give you a simple example using AES algorithm in ECB mode (please note that there are many cipher algorithms, operation modes etc. this sample is only to show the basics): 为了做到这一点,您可以使用
javax.crypto.Cipher
类代替java.security.Signature
我给您一个使用ECB模式下的AES算法的简单示例(请注意,有很多密码算法,操作模式等。此示例是仅显示基础知识):
import java.security.Key;
import java.security.Security;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
public class CipherBasicSample
{
public static void main(String args[]) throws Exception
{
Security.addProvider(new BouncyCastleProvider());
// text to cipher
String secret = "secret";
// create the key to cipher an decipher
KeyGenerator kg = KeyGenerator.getInstance("AES","BC");
kg.init(128);
SecretKey sk = kg.generateKey();
Key key = new SecretKeySpec(sk.getEncoded(), "AES");
// get a cipher instance
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding", "BC");
// init to encrypt mode
cipher.init(Cipher.ENCRYPT_MODE, key);
// encrypt the text
cipher.update(secret.getBytes());
byte[] secretEncrypt = cipher.doFinal();
System.out.println("Encrypt text: " + new String(secretEncrypt));
// get a cipher instance
Cipher decipher = Cipher.getInstance("AES/ECB/PKCS5Padding", "BC");
// init to decrypt mode
decipher.init(Cipher.DECRYPT_MODE, key);
// decrypt the text
decipher.update(secretEncrypt);
byte[] secretDecrypt = decipher.doFinal();
System.out.println("Encrypt text: " + new String(secretDecrypt));
}
}
Furthermore you can check the bc.prov source code where there are some test classes to test different cipher implementations: src code or on gitHub 此外,您可以检查bc.prov源代码,其中有一些测试类可以测试不同的密码实现: src代码或gitHub上
Hope this helps, 希望这可以帮助,
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.