This article illustrates how Java uses a DSA key pair to generate XML signatures. Share to everyone for your reference. The implementation methods are as follows:
The Mainclass.java code is as follows:
Copy Code code as follows:
Import Java.security.KeyPair;
Import Java.security.KeyPairGenerator;
Import Java.security.SecureRandom;
Import java.util.Collections;
Import Javax.xml.crypto.dsig.CanonicalizationMethod;
Import Javax.xml.crypto.dsig.DigestMethod;
Import javax.xml.crypto.dsig.Reference;
Import Javax.xml.crypto.dsig.SignatureMethod;
Import Javax.xml.crypto.dsig.SignedInfo;
Import Javax.xml.crypto.dsig.XMLSignature;
Import Javax.xml.crypto.dsig.XMLSignatureFactory;
Import Javax.xml.crypto.dsig.keyinfo.KeyInfo;
Import Javax.xml.crypto.dsig.keyinfo.KeyInfoFactory;
Import Javax.xml.crypto.dsig.keyinfo.KeyValue;
Import Javax.xml.crypto.dsig.spec.C14NMethodParameterSpec;
public class MainClass {
public static void Main (string[] args) throws Exception {
Keypairgenerator KPG = keypairgenerator.getinstance ("DSA");
Kpg.initialize (1024, New SecureRandom ());
KeyPair Dsakeypair = Kpg.generatekeypair ();
Xmlsignaturefactory sigfactory = Xmlsignaturefactory.getinstance ();
Reference ref = Sigfactory.newreference ("#Body", Sigfactory.newdigestmethod (DIGESTMETHOD.SHA1, null));
SignedInfo SignedInfo = Sigfactory.newsignedinfo (Sigfactory.newcanonicalizationmethod (
Canonicalizationmethod.inclusive_with_comments, (C14NMETHODPARAMETERSPEC) null), Sigfactory.newsignaturemethod ( SIGNATUREMETHOD.DSA_SHA1, NULL), Collections.singletonlist (ref));
Keyinfofactory kif = Sigfactory.getkeyinfofactory ();
KeyValue kv = Kif.newkeyvalue (Dsakeypair.getpublic ());
KeyInfo KeyInfo = Kif.newkeyinfo (Collections.singletonlist (KV));
Xmlsignature Xmlsig = Sigfactory.newxmlsignature (SignedInfo, keyInfo);
}
}
I hope this article will help you with your Java programming.