【问题标题】:Decrypt digital sign bouncycastle解密数字签名 bouncycastle
【发布时间】:2014-06-18 06:16:31
【问题描述】:

我正在使用来自 answer 的代码,有一个如何签名和验证签名的示例,但我如何使用 Bouncycastle 解密这种签名? java.security.Signature类中没有这样的方法。

【问题讨论】:

    标签: java encryption bouncycastle


    【解决方案1】:

    我认为您的意思是您正在寻找带有 bouncycastle 的加密/解密示例,而不是您在问题中引用的签名/验证示例。为了做到这一点,你可以使用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));
        }
    }
    

    此外,您可以查看 bc.prov 源代码,其中有一些测试类来测试不同的密码实现:src codeon gitHub

    希望对你有帮助,

    【讨论】:

      猜你喜欢
      • 2018-12-24
      • 2012-01-30
      • 1970-01-01
      • 2012-06-02
      • 2018-08-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-14
      • 2013-10-10
      相关资源
      最近更新 更多