当前位置: 首页 > 知识库问答 >
问题:

解码签名字节时出错:" Java . security . signature exception:解码签名字节时出错"

慕承允
2023-03-14

我在尝试验证类中的ECDSA签名时遇到此错误。错误代码为:

java.security.SignatureException: error decoding signature bytes.
at org.bouncycastle.jcajce.provider.asymmetric.util.DSABase.engineVerify(Unknown Source)
at java.security.Signature$Delegate.engineVerify(Signature.java:1172)
at java.security.Signature.verify(Signature.java:623)
at SDSGeneration.Signing.verify_signature(Signing.java:88)
at com.sdsweb.modele.VerificationBox.checkSignature(VerificationBox.java:121)
at com.sdsweb.modele.VerificationBox.verifieur(VerificationBox.java:84)
at com.sdsweb.servlet.Authentification.doGet(Authentification.java:55)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:620)......

我在VerificationBox中调用这个方法。java类:

public Map<Integer, String> checkSignature(String data_digest, Signature SignObject, PublicKey publicKey, byte[] Signaturebyte) throws InvalidKeyException, SignatureException, NoSuchProviderException, NoSuchAlgorithmException{

    Map<Integer, String> erreur_signature = new HashMap<Integer, String>();

    Signing SignVerifier = new Signing();

    SignVerifier.setEcdsa_signature();

    SignVerifier.verify_signature(publicKey, SignVerifier.getEcdsa_signature(), data_digest, Signaturebyte);

    if (SignVerifier.getVerify_result()){
        erreur_signature.put(j, "SIGNATURE ÉRONNÉ, CODE SDS INVALIDE");
        j++;
    }
    return null;

}

这是SDSGeneration.Signing:

package SDSGeneration;

import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.PublicKey;
import java.security.Signature;
import java.security.KeyPair;
import java.security.SignatureException;

//This class provide an instance of all object which are needed to sign data
public class Signing {

    public Signing() {

    }

    public Signing(KeyPair keygen) {

    }

    //ECDSA Signature Object
    private Signature ecdsa_signature;
    //KeyPair object
    private KeyPair keygen;
    //Final data signature
    private byte[] signature;
    //Boolean variable telling if the signing proccess was succesfful or not
    private Boolean verify_result;

    public Boolean getVerify_result() {
        return verify_result;
    }

    public byte[] getSignature() {
        return signature;
    }


    public KeyPair getKeygen() {
        return keygen;
    }

    public Signature getEcdsa_signature() {
        return ecdsa_signature;
    }

    public void setEcdsa_signature() throws NoSuchAlgorithmException, NoSuchProviderException {

        this.ecdsa_signature = java.security.Signature.getInstance("ECDSA", "BC");
    }

    public void setKeygen(KeyPair keygen) {
        this.keygen = keygen;
    }



    //This method is use to sign data. A call to a  sign  method resets the signature object to the state it was in when previously initialized for signing via a call to  initSign. 
    //That is, the object is reset and available to generate another signature with the same private key, if desired, via new calls to  update  and  sign .
    public void generate_signature(KeyPair keygen, Signature ecdsa_signature, String data) throws InvalidKeyException, SignatureException{

        //inserting private key in signature object
        ecdsa_signature.initSign(keygen.getPrivate());

        //inserting data to sign
        ecdsa_signature.update(data.getBytes());

        //signing
        this.signature = ecdsa_signature.sign();
    }

    //This method is use to sign data. A call to the  verify  method resets the signature object to its state when it was initialized for verification via a call to initVerify . 
    //That is, the object is reset and available to verify another signature from the identity whose public key was specified in the call to  initVerify .
    public void verify_signature(PublicKey keygen, Signature ecdsa_signature, String data, byte[] signature) throws InvalidKeyException, SignatureException{

        //generating the signature
        ecdsa_signature.initVerify(keygen);

        //inserting data to verify
        ecdsa_signature.update(data.getBytes());

        //verifying
        this.verify_result = ecdsa_signature.verify(signature);
    }
}

我写了一个测试类来测试SDSGeneration.Signing代码和一切运行良好,签名和验证数据,但是在我的项目中导入和使用它时,会出现下面的错误。这是测试类代码:

    package SDSGeneration;

import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.SignatureException;

public class TestSigning {

    public TestSigning() {
        // TODO Auto-generated constructor stub
    }

    public static void main(String[] args) throws NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException, InvalidKeyException, SignatureException {
        // TODO Auto-generated method stub
        GenerateKeyPair new_keypair = new GenerateKeyPair();

        Signing testSign = new Signing();
        testSign.setKeygen(new_keypair.getKeygen());

        testSign.setEcdsa_signature();

        testSign.generate_signature(testSign.getKeygen(), testSign.getEcdsa_signature(), "Fanfe Yvon");

        testSign.verify_signature(testSign.getKeygen().getPublic(), testSign.getEcdsa_signature(), "Fanfe Yvon", testSign.getSignature());

        System.out.println("Signature result : " + testSign.getVerify_result() + " ; signature of data : " + testSign.getSignature());
    }

}

共有1个答案

卜昂熙
2023-03-14

问题是我使用了错误的字符集将签名编码到存储中。

在存储之前,我必须将其编码为Base64并以UTF-8编码,如下所示:

String signTostring = DatatypeConverter.printBase64Binary(signature);
signTostring = URLEncoder.encode(signTostring, "UTF-8");

当我想使用它时,我会进行反向操作以获得原始签名

String st = URLDecoder.decode(code.getSignature(), "UTF-8");
byte[] sign_byte = DatatypeConverter.parseBase64Binary(st); 
 类似资料:
  • 问题内容: 是否可以从由类源代码形成的字节码中提取出一个类名? 情况是这样的:我从某个地方远程获得了一个字节码类,不管它来自何处。为了使用类加载器有效地加载该类,我还需要具有类名称…对吗? 问题答案: 如果只需要类名,则可能自己解析类文件的开头,而不是仅仅为此目的而添加用于处理类代码的第三方库。您只需要常量池中的类和字符串,跳过访问标志,然后将/替换为即可。在班级名称中。如果有字节数组,则可以使用

  • 项目清理和重建工作成功,但如果我想运行应用程序,我得到这样的错误。 错误:将字节码转换为Dex时出错:原因:Dex无法解析版本52字节码。这是由使用Java8或更高版本编译的库依赖项造成的。如果在库子模块中使用'java'gradle插件,请将targetCompatibility='1.7'sourceCompatibility='1.7'添加到该子模块的build.gradle文件中。 错误:

  • 我尝试在google play上上传我的apk,但遇到一条错误消息:“您上传了一个可调试的apk。出于安全考虑,您需要禁用调试,然后才能在google play中发布它。了解有关可调试apk的更多信息。” 然后我在清单中写入并再次尝试。我遇到了同样的错误,所以我将模块中的构建变体设置为释放,并再次尝试生成一个apk,但这次,生成了这个错误:

  • 问题内容: 我遇到了一些有关JVM / JIT活动的参考,其中似乎在编译字节码和解释字节码之间有区别。该特定注释声明的字节码在前10000次运行时进行解释,然后进行编译。 “编译”和“解释”字节码之间有什么区别? 问题答案: 解释字节码基本上是逐行读取字节码,不进行任何优化或任何操作,然后对其进行解析并实时执行。由于许多原因,这种方法效率低下,其中包括Java字节码设计得不能快速解释的问题。 编译

  • 我用Java生成了一个ECDSA签名,我想从中获得R和S值。我的理解是我生成的签名是DER编码的。有人能给我提供一些Java代码(也许用Bouncy Castle)来检索作为大整数的R和S值吗? 注意:如果有帮助,我通过JCE的Signature类使用内置提供程序生成签名,并且我的P_256EC密钥对的签名长度通常徘徊在70到72字节之间。

  • 清理和生成项目 将“sourceCompatibility=”1.7“targetCompatibility=”1.7“”添加到分级文件。 谁能建议一下如何解决这个问题。