Java Tutorial/Security/Digital Signature Algorithm

Материал из Java эксперт
Версия от 05:01, 1 июня 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Create the DSA key factory from a Set of Digital Signature Algorithm (DSA) Parameters

import java.math.BigInteger;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.interfaces.DSAParams;
import java.security.interfaces.DSAPrivateKey;
import java.security.interfaces.DSAPublicKey;
import java.security.spec.DSAPrivateKeySpec;
import java.security.spec.DSAPublicKeySpec;
import java.security.spec.KeySpec;
public class Main {
  public static void main(String[] argv) throws Exception {
    KeyPairGenerator keyGen = KeyPairGenerator.getInstance("DSA");
    keyGen.initialize(1024);
    KeyPair keypair = keyGen.genKeyPair();
    DSAPrivateKey privateKey = (DSAPrivateKey) keypair.getPrivate();
    DSAPublicKey publicKey = (DSAPublicKey) keypair.getPublic();
    DSAParams dsaParams = privateKey.getParams();
    BigInteger p = dsaParams.getP();
    BigInteger q = dsaParams.getQ();
    BigInteger g = dsaParams.getG();
    BigInteger x = privateKey.getX();
    BigInteger y = publicKey.getY();
    KeyFactory keyFactory = KeyFactory.getInstance("DSA");
  }
}





Create the DSA private key from a Set of Digital Signature Algorithm (DSA) Parameters

import java.math.BigInteger;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.interfaces.DSAParams;
import java.security.interfaces.DSAPrivateKey;
import java.security.interfaces.DSAPublicKey;
import java.security.spec.DSAPrivateKeySpec;
import java.security.spec.DSAPublicKeySpec;
import java.security.spec.KeySpec;
public class Main {
  public static void main(String[] argv) throws Exception {
    KeyPairGenerator keyGen = KeyPairGenerator.getInstance("DSA");
    keyGen.initialize(1024);
    KeyPair keypair = keyGen.genKeyPair();
    DSAPrivateKey privateKey = (DSAPrivateKey) keypair.getPrivate();
    DSAPublicKey publicKey = (DSAPublicKey) keypair.getPublic();
    DSAParams dsaParams = privateKey.getParams();
    BigInteger p = dsaParams.getP();
    BigInteger q = dsaParams.getQ();
    BigInteger g = dsaParams.getG();
    BigInteger x = privateKey.getX();
    BigInteger y = publicKey.getY();
    KeyFactory keyFactory = KeyFactory.getInstance("DSA");
    KeySpec privateKeySpec = new DSAPrivateKeySpec(x, p, q, g);
    PrivateKey privateKey1 = keyFactory.generatePrivate(privateKeySpec);
  }
}





Create the DSA public key from a Set of Digital Signature Algorithm (DSA) Parameters

import java.math.BigInteger;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.interfaces.DSAParams;
import java.security.interfaces.DSAPrivateKey;
import java.security.interfaces.DSAPublicKey;
import java.security.spec.DSAPrivateKeySpec;
import java.security.spec.DSAPublicKeySpec;
import java.security.spec.KeySpec;
public class Main {
  public static void main(String[] argv) throws Exception {
    KeyPairGenerator keyGen = KeyPairGenerator.getInstance("DSA");
    keyGen.initialize(1024);
    KeyPair keypair = keyGen.genKeyPair();
    DSAPrivateKey privateKey = (DSAPrivateKey) keypair.getPrivate();
    DSAPublicKey publicKey = (DSAPublicKey) keypair.getPublic();
    DSAParams dsaParams = privateKey.getParams();
    BigInteger p = dsaParams.getP();
    BigInteger q = dsaParams.getQ();
    BigInteger g = dsaParams.getG();
    BigInteger x = privateKey.getX();
    BigInteger y = publicKey.getY();
    KeyFactory keyFactory = KeyFactory.getInstance("DSA");
    KeySpec publicKeySpec = new DSAPublicKeySpec(y, p, q, g);
    PublicKey publicKey1 = keyFactory.generatePublic(publicKeySpec);
  }
}





Creating a Signature

import java.math.BigInteger;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.Signature;
import java.security.interfaces.DSAParams;
import java.security.interfaces.DSAPrivateKey;
import java.security.interfaces.DSAPublicKey;
import java.security.spec.DSAPrivateKeySpec;
import java.security.spec.KeySpec;
public class Main {
  public static void main(String[] argv) throws Exception {
    KeyPairGenerator keyGen = KeyPairGenerator.getInstance("DSA");
    keyGen.initialize(1024);
    KeyPair keypair = keyGen.genKeyPair();
    DSAPrivateKey privateKey = (DSAPrivateKey) keypair.getPrivate();
    DSAPublicKey publicKey = (DSAPublicKey) keypair.getPublic();
    DSAParams dsaParams = privateKey.getParams();
    BigInteger p = dsaParams.getP();
    BigInteger q = dsaParams.getQ();
    BigInteger g = dsaParams.getG();
    BigInteger x = privateKey.getX();
    BigInteger y = publicKey.getY();
    // Create the DSA key factory
    KeyFactory keyFactory = KeyFactory.getInstance("DSA");
    // Create the DSA private key
    KeySpec privateKeySpec = new DSAPrivateKeySpec(x, p, q, g);
    PrivateKey privateKey1 = keyFactory.generatePrivate(privateKeySpec);
    byte[] buffer = new byte[1024];
    Signature sig = Signature.getInstance(privateKey1.getAlgorithm());
    sig.initSign(privateKey1);
    sig.update(buffer, 0, buffer.length);
  }
}





Digital Signature Algorithm Demo

import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.SecureRandom;
import java.security.Security;
import java.security.Signature;
public class MainClass {
  public static void main(String[] args) throws Exception {
    Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
    KeyPairGenerator keyGen = KeyPairGenerator.getInstance("DSA", "BC");
    keyGen.initialize(512, new SecureRandom());
    KeyPair keyPair = keyGen.generateKeyPair();
    Signature signature = Signature.getInstance("DSA", "BC");
    signature.initSign(keyPair.getPrivate(), new SecureRandom());
    byte[] message = "abc".getBytes();
    signature.update(message);
    byte[] sigBytes = signature.sign();
    signature.initVerify(keyPair.getPublic());
    signature.update(message);
    System.out.println(signature.verify(sigBytes));
  }
}





DSA Key Translator

import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.spec.DSAPrivateKeySpec;
import java.security.spec.DSAPublicKeySpec;
import java.security.spec.InvalidKeySpecException;
public class MainClass {
  public static void main(String[] args) throws NoSuchAlgorithmException, InvalidKeySpecException {
    KeyPairGenerator kpg = KeyPairGenerator.getInstance("DSA");
    kpg.initialize(512);
    KeyPair keys = kpg.genKeyPair();
    PrivateKey priKey = keys.getPrivate();
    PublicKey pubKey = keys.getPublic();
    KeyFactory kf = KeyFactory.getInstance("DSA");
    DSAPrivateKeySpec dsaPriKeySpec = (DSAPrivateKeySpec) kf.getKeySpec(priKey,
        DSAPrivateKeySpec.class);
    DSAPublicKeySpec dsaPubKeySpec = (DSAPublicKeySpec) kf.getKeySpec(pubKey,
        DSAPublicKeySpec.class);
    System.out.println("\nDSA Private Key");
    System.out.println("\nx = " + dsaPriKeySpec.getX());
    System.out.println("\nDSA Public Key");
    System.out.println("\ng = " + dsaPubKeySpec.getG());
    System.out.println("\np = " + dsaPubKeySpec.getP());
    System.out.println("\nq = " + dsaPubKeySpec.getQ());
    System.out.println("\ny = " + dsaPubKeySpec.getY());
  }
}
/*
DSA Private Key
x = 776400661570001590971791637592968309673321751461
DSA Public Key
g = 5421644057436475141609648488325705128047428394380474376834667300766108262613900542681289080713724597310673074119355136085795982097390670890367185141189796
p = 13232376895198612407547930718267435757728527029623408872245156039757713029036368719146452186041204237350521785240337048752071462798273003935646236777459223
q = 857393771208094202104259627990318636601332086981
y = 9079896982621092847112483863863391775338648287464668946120962630349123906761002084264031103470728516533966483834610830067548970604189069706612392762346323
*/





DSA with Elliptic Curve

import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.SecureRandom;
import java.security.Security;
import java.security.Signature;
import java.security.spec.ECGenParameterSpec;
public class MainClass {
  public static void main(String[] args) throws Exception {
    Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
    KeyPairGenerator keyGen = KeyPairGenerator.getInstance("ECDSA", "BC");
    ECGenParameterSpec ecSpec = new ECGenParameterSpec("prime192v1");
    keyGen.initialize(ecSpec, new SecureRandom());
    KeyPair keyPair = keyGen.generateKeyPair();
    Signature signature = Signature.getInstance("ECDSA", "BC");
    signature.initSign(keyPair.getPrivate(), new SecureRandom());
    byte[] message = "abc".getBytes();
    signature.update(message);
    byte[] sigBytes = signature.sign();
    signature.initVerify(keyPair.getPublic());
    signature.update(message);
    System.out.println(signature.verify(sigBytes));
  }
}





Export DSAPrivateKeySpec

import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.SecureRandom;
import java.security.spec.DSAPrivateKeySpec;
public class MainClass {
  public static void main(String args[]) throws Exception {
    KeyPairGenerator kpg = KeyPairGenerator.getInstance("DSA");
    kpg.initialize(512, new SecureRandom());
    KeyPair kp = kpg.generateKeyPair();
    Class spec = Class.forName("java.security.spec.DSAPrivateKeySpec");
    KeyFactory kf = KeyFactory.getInstance("DSA");
    DSAPrivateKeySpec ks = (DSAPrivateKeySpec) kf.getKeySpec(kp.getPrivate(), spec);
    FileOutputStream fos = new FileOutputStream("exportedKey");
    ObjectOutputStream oos = new ObjectOutputStream(fos);
    oos.writeObject(ks.getX());
    oos.writeObject(ks.getP());
    oos.writeObject(ks.getQ());
    oos.writeObject(ks.getG());
  }
}





Getting the Digital Signature Algorithm (DSA) Parameters of a Key Pair

import java.math.BigInteger;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.interfaces.DSAParams;
import java.security.interfaces.DSAPrivateKey;
import java.security.interfaces.DSAPublicKey;
public class Main {
  public static void main(String[] argv) throws Exception {
    KeyPairGenerator keyGen = KeyPairGenerator.getInstance("DSA");
    keyGen.initialize(1024);
    KeyPair keypair = keyGen.genKeyPair();
    DSAPrivateKey privateKey = (DSAPrivateKey) keypair.getPrivate();
    DSAPublicKey publicKey = (DSAPublicKey) keypair.getPublic();
    DSAParams dsaParams = privateKey.getParams();
    BigInteger p = dsaParams.getP();
    BigInteger q = dsaParams.getQ();
    BigInteger g = dsaParams.getG();
    BigInteger x = privateKey.getX();
    BigInteger y = publicKey.getY();
  }
}





Signed Object

import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.Signature;
import java.security.SignedObject;
import java.util.Vector;
public class MainClass {
  public static void main(String[] args) throws Exception {
    String alg = "DSA";
    KeyPairGenerator kg = KeyPairGenerator.getInstance(alg);
    KeyPair keyPair = kg.genKeyPair();
    Vector v = new Vector();
    v.add("This is a test!");
    Signature sign = Signature.getInstance(alg);
    SignedObject so = new SignedObject(v, keyPair.getPrivate(), sign);
    System.out.println(so.verify(keyPair.getPublic(), sign));
  }
}





Signing a Java Object

import java.io.Serializable;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.Signature;
import java.security.SignedObject;
public class Main {
  public static void main(String[] argv) throws Exception {
    KeyPairGenerator keyGen = KeyPairGenerator.getInstance("DSA");
    keyGen.initialize(1024);
    KeyPair keypair = keyGen.genKeyPair();
    PrivateKey privateKey = keypair.getPrivate();
    PublicKey publicKey = keypair.getPublic();
    Serializable o = new MyClass();
    Signature sig = Signature.getInstance(privateKey.getAlgorithm());
    SignedObject so = new SignedObject(o, privateKey, sig);
    
    sig = Signature.getInstance(publicKey.getAlgorithm());
    boolean b = so.verify(publicKey, sig);
    o = (MyClass) so.getObject();
  }
}
class MyClass implements Serializable {
  String s = "my string";
  int i = 123;
}





Use DSAPrivateKeySpec

import java.io.FileInputStream;
import java.io.ObjectInputStream;
import java.math.BigInteger;
import java.security.KeyFactory;
import java.security.PrivateKey;
import java.security.spec.DSAPrivateKeySpec;
public class MainClass {
  public static void main(String args[]) throws Exception {
    FileInputStream fis = new FileInputStream("exportedKey");
    ObjectInputStream ois = new ObjectInputStream(fis);
    DSAPrivateKeySpec ks = new DSAPrivateKeySpec((BigInteger) ois.readObject(), (BigInteger) ois
        .readObject(), (BigInteger) ois.readObject(), (BigInteger) ois.readObject());
    KeyFactory kf = KeyFactory.getInstance("DSA");
    PrivateKey pk = kf.generatePrivate(ks);
    System.out.println("Got private key");
  }
}





Use DSA to Sign

import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.Signature;
public class MainClass {
  public static void main(String[] args) throws Exception {
    String alg = "DSA";
    KeyPairGenerator kg = KeyPairGenerator.getInstance(alg);
    KeyPair keyPair = kg.genKeyPair();
    byte[] signature = performSigning("test", alg, keyPair);
    performVerification(args[0], alg, signature, keyPair.getPublic());
  }
  static byte[] performSigning(String s, String alg, KeyPair keyPair) throws Exception {
    Signature sign = Signature.getInstance(alg);
    PrivateKey privateKey = keyPair.getPrivate();
    PublicKey publicKey = keyPair.getPublic();
    sign.initSign(privateKey);
    sign.update(s.getBytes());
    return sign.sign();
  }
  static void performVerification(String s, String alg, byte[] signature, PublicKey publicKey)
      throws Exception {
    Signature sign = Signature.getInstance(alg);
    sign.initVerify(publicKey);
    sign.update(s.getBytes());
    System.out.println(sign.verify(signature));
  }
}





Verifies the signature for the given buffer of bytes using the public key.

import java.math.BigInteger;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.Signature;
import java.security.interfaces.DSAParams;
import java.security.interfaces.DSAPrivateKey;
import java.security.interfaces.DSAPublicKey;
import java.security.spec.DSAPrivateKeySpec;
import java.security.spec.DSAPublicKeySpec;
import java.security.spec.KeySpec;
public class Main {
  public static void main(String[] argv) throws Exception {
    KeyPairGenerator keyGen = KeyPairGenerator.getInstance("DSA");
    keyGen.initialize(1024);
    KeyPair keypair = keyGen.genKeyPair();
    DSAPrivateKey privateKey = (DSAPrivateKey) keypair.getPrivate();
    DSAPublicKey publicKey = (DSAPublicKey) keypair.getPublic();
    DSAParams dsaParams = privateKey.getParams();
    BigInteger p = dsaParams.getP();
    BigInteger q = dsaParams.getQ();
    BigInteger g = dsaParams.getG();
    BigInteger x = privateKey.getX();
    BigInteger y = publicKey.getY();
    KeyFactory keyFactory = KeyFactory.getInstance("DSA");
    KeySpec publicKeySpec = new DSAPublicKeySpec(y, p, q, g);
    PublicKey publicKey1 = keyFactory.generatePublic(publicKeySpec);
    KeySpec privateKeySpec = new DSAPrivateKeySpec(x, p, q, g);
    PrivateKey privateKey1 = keyFactory.generatePrivate(privateKeySpec);
    byte[] buffer = new byte[1024];
    Signature sig = Signature.getInstance(privateKey1.getAlgorithm());
    sig.initSign(privateKey1);
    sig.update(buffer, 0, buffer.length);
    byte[] signature = sig.sign();
    sig = Signature.getInstance(publicKey1.getAlgorithm());
    sig.initVerify(publicKey1);
    sig.update(buffer, 0, buffer.length);
    sig.verify(signature);
  }
}