Java Tutorial/Security/Key

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

DES Key Agreement

import java.math.BigInteger;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import javax.crypto.KeyAgreement;
import javax.crypto.SecretKey;
import javax.crypto.interfaces.DHPrivateKey;
import javax.crypto.interfaces.DHPublicKey;
import javax.crypto.spec.DHParameterSpec;
public class MainClass {
  public static void main(String[] args) throws Exception {
    String s = "F488FD584E49DBCD20B49DE49107366B336C380D451D0F7C88"
        + "11111111111111111111111111111111111111111111111111"
        + "11111111111111111111111111111111111111111111111111"
        + "11111111111111111111111111111111111111111111111111"
        + "11111111111111111111111111111111111111111111111111" + "2F78C7";
    BigInteger base = BigInteger.valueOf(2);
    BigInteger modulous = new BigInteger(s, 16);
    DHParameterSpec skipParameterSpec = new DHParameterSpec(modulous, base);
    KeyPairGenerator kpg1 = KeyPairGenerator.getInstance("DH");
    kpg1.initialize(skipParameterSpec);
    KeyPair kp1 = kpg1.generateKeyPair();
    KeyAgreement ka1 = KeyAgreement.getInstance("DH");
    DHPrivateKey privateKey1 = (DHPrivateKey) kp1.getPrivate();
    DHPublicKey publicKey1 = (DHPublicKey) kp1.getPublic();
    ka1.init(privateKey1);
    System.out.println("1 is using " + publicKey1.getY() + " for its public key");
    KeyPairGenerator kpg2 = KeyPairGenerator.getInstance("DH");
    kpg2.initialize(skipParameterSpec);
    KeyPair kp2 = kpg2.generateKeyPair();
    KeyAgreement ka2 = KeyAgreement.getInstance("DH");
    DHPrivateKey privateKey2 = (DHPrivateKey) kp2.getPrivate();
    DHPublicKey publicKey2 = (DHPublicKey) kp2.getPublic();
    ka2.init(privateKey2);
    System.out.println("2 is using " + publicKey2.getY() + "for its public key");
    // Use the KeyAgreement object of 1 to generate its shared key
    ka1.doPhase(publicKey2, true);
    SecretKey sharedKey1 = ka1.generateSecret("DES");
    System.out.println("1 is using " + new String(sharedKey1.getEncoded())
        + " as its DES session key");
    // Use the KeyAgreement object of 2 to generate its shared key
    ka2.doPhase(publicKey1, true);
    SecretKey sharedKey2 = ka2.generateSecret("DES");
    System.out.println("2 is using " + new String(sharedKey2.getEncoded())
        + "as its DES session key");
  }
}





implements Key, PublicKey, PrivateKey

import java.security.Key;
import java.security.PrivateKey;
import java.security.PublicKey;
public class XYZKey implements Key, PublicKey, PrivateKey {
  int rotValue;
  public String getAlgorithm() {
    return "XYZ";
  }
  public String getFormat() {
    return "XYZ Special Format";
  }
  public byte[] getEncoded() {
    byte b[] = new byte[4];
    b[3] = (byte) ((rotValue << 24) & 0xff);
    b[2] = (byte) ((rotValue << 16) & 0xff);
    b[1] = (byte) ((rotValue << 8) & 0xff);
    b[0] = (byte) ((rotValue << 0) & 0xff);
    return b;
  }
}





Key Agreement

import java.math.BigInteger;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import javax.crypto.KeyAgreement;
import javax.crypto.interfaces.DHPrivateKey;
import javax.crypto.interfaces.DHPublicKey;
import javax.crypto.spec.DHParameterSpec;
public class MainClass {
  public static void main(String[] args) throws Exception {
    String s = "F488FD584E49DBCD20B49DE49107366B336C380D451D0F7C88"
        + "11111111111111111111111111111111111111111111111111"
        + "11111111111111111111111111111111111111111111111111"
        + "11111111111111111111111111111111111111111111111111"
        + "11111111111111111111111111111111111111111111111111" + "2F78C7";
    BigInteger base = BigInteger.valueOf(2);
    BigInteger modulus = new BigInteger(s, 16);
    DHParameterSpec skipParameterSpec = new DHParameterSpec(modulus, base);
    KeyPairGenerator kpg1 = KeyPairGenerator.getInstance("DH");
    kpg1.initialize(skipParameterSpec);
    KeyPair kp1 = kpg1.generateKeyPair();
    KeyAgreement ka1 = KeyAgreement.getInstance("DH");
    DHPrivateKey privateKey1 = (DHPrivateKey) kp1.getPrivate();
    DHPublicKey publicKey1 = (DHPublicKey) kp1.getPublic();
    ka1.init(privateKey1);
    System.out.println("1 is using " + publicKey1.getY() + " for its public key");
    KeyPairGenerator kpg2 = KeyPairGenerator.getInstance("DH");
    kpg2.initialize(skipParameterSpec);
    KeyPair kp2 = kpg2.generateKeyPair();
    KeyAgreement ka2 = KeyAgreement.getInstance("DH");
    DHPrivateKey privateKey2 = (DHPrivateKey) kp2.getPrivate();
    DHPublicKey publicKey2 = (DHPublicKey) kp2.getPublic();
    ka2.init(privateKey2);
    System.out.println("2 is using " + publicKey2.getY() + " for its public key");
    ka1.doPhase(publicKey2, true);
    byte[] sharedKey1 = ka1.generateSecret();
    System.out.println("1 is using " + new BigInteger(1, sharedKey1) + " for its shared key");
    ka2.doPhase(publicKey1, true);
    byte[] sharedKey2 = ka2.generateSecret();
    System.out.println("2 is using " + new BigInteger(1, sharedKey2) + " for its shared key");
  }
}





PBE key spec

import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.PBEKeySpec;
public class MainClass {
  public static void main(String[] args) throws Exception {
    PBEKeySpec keySpec = new PBEKeySpec("charArray".toCharArray());
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
    SecretKey key = keyFactory.generateSecret(keySpec);
    System.out.println(new String(key.getEncoded()));
  }
}





Rich DES Key

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.security.SecureRandom;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
public class MainClass {
  public static void main(String args[]) throws Exception {
    KeyGenerator keyGen = KeyGenerator.getInstance("DES");
    SecureRandom random = SecureRandom.getInstance("SHA1PRNG", "SUN");
    random.setSeed(101L);
    keyGen.init(56, random);
    SecretKey sKey = keyGen.generateKey();
    SecretKeyFactory kfactory = SecretKeyFactory.getInstance("DES");
    DESKeySpec kspec = (DESKeySpec) kfactory.getKeySpec(sKey, DESKeySpec.class);
    System.out.println(sKey);
    FileOutputStream fos = new FileOutputStream("secretKeys");
    ObjectOutputStream oos = new ObjectOutputStream(fos);
    oos.writeObject(kspec.getKey());
    FileInputStream fin = new FileInputStream("secretKeys");
    ObjectInputStream ois = new ObjectInputStream(fin);
    byte[] kMaterial = (byte[]) ois.readObject();
    DESKeySpec keyspec = new DESKeySpec(kMaterial);
    SecretKey newKey = kfactory.generateSecret(keyspec);
    System.out.println(newKey);
    System.out.println("Do the keys equal :" + newKey.equals(sKey));
  }
}





Use DSAKey

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.math.BigInteger;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PublicKey;
import java.security.SecureRandom;
import java.security.spec.DSAPublicKeySpec;
public class MainClass {
  public static void main(String args[]) throws Exception {
    KeyPairGenerator keyGen = KeyPairGenerator.getInstance("DSA");
    SecureRandom random = SecureRandom.getInstance("SHA1PRNG", "SUN");
    random.setSeed(101L);
    keyGen.initialize(1024, random);
    KeyPair keypair = keyGen.generateKeyPair();
    KeyFactory kfactory = KeyFactory.getInstance("DSA");
    DSAPublicKeySpec kspec = (DSAPublicKeySpec) kfactory.getKeySpec(keypair.getPublic(),
        DSAPublicKeySpec.class);
    System.out.println(keypair.getPublic());
    FileOutputStream fos = new FileOutputStream("publicKeys");
    ObjectOutputStream oos = new ObjectOutputStream(fos);
    oos.writeObject(kspec.getY());
    oos.writeObject(kspec.getP());
    oos.writeObject(kspec.getQ());
    oos.writeObject(kspec.getG());
    FileInputStream fin = new FileInputStream("publicKeys");
    ObjectInputStream ois = new ObjectInputStream(fin);
    BigInteger Y = (BigInteger) ois.readObject();
    BigInteger P = (BigInteger) ois.readObject();
    BigInteger Q = (BigInteger) ois.readObject();
    BigInteger G = (BigInteger) ois.readObject();
    DSAPublicKeySpec keyspec = new DSAPublicKeySpec(Y, P, Q, G);
    PublicKey pkey = kfactory.generatePublic(keyspec);
    System.out.println(pkey);
  }
}
/*Sun DSA Public Key
    Parameters:
    p:
    fd7f5381 1d751229 52df4a9c 2eece4e7 f611b752 3cef4400 c31e3f80 b6512669
    455d4022 51fb593d 8d58fabf c5f5ba30 f6cb9b55 6cd7813b 801d346f f26660b7
    6b9950a5 a49f9fe8 047b1022 c24fbba9 d7feb7c6 1bf83b57 e7c6a8a6 150f04fb
    83f6d3c5 1ec30235 54135a16 9132f675 f3ae2b61 d72aeff2 2203199d d14801c7
    q:
    9760508f 15230bcc b292b982 a2eb840b f0581cf5
    g:
    f7e1a085 d69b3dde cbbcab5c 36b857b9 7994afbb fa3aea82 f9574c0b 3d078267
    5159578e bad4594f e6710710 8180b449 167123e8 4c281613 b7cf0932 8cc8a6e1
    3c167a8b 547c8d28 e0a3ae1e 2bb3a675 916ea37f 0bfa2135 62f1fb62 7a01243b
    cca4f1be a8519089 a883dfe1 5ae59f06 928b665e 807b5525 64014c3b fecf492a
  y:
    ab67aa43 9e8ea5c8 904b1afe 89ae185a 4ef595cf ca9b9114 f05373dc 193cddd3
    baefb0f8 8bd858ea d78632c2 6481c9e0 a4f56878 8f4b0f10 d505ee57 4b1c7d5d
    1196ddf0 1003578f 16272cb6 94f92796 57efd826 50287f9d b6f7e512 75fa4316
    5961aef6 ba663ab8 81c57606 554e4fb6 830b9a7b ce32d5a3 a708d09b 3b6aa8ff
Sun DSA Public Key
    Parameters:
    p:
    fd7f5381 1d751229 52df4a9c 2eece4e7 f611b752 3cef4400 c31e3f80 b6512669
    455d4022 51fb593d 8d58fabf c5f5ba30 f6cb9b55 6cd7813b 801d346f f26660b7
    6b9950a5 a49f9fe8 047b1022 c24fbba9 d7feb7c6 1bf83b57 e7c6a8a6 150f04fb
    83f6d3c5 1ec30235 54135a16 9132f675 f3ae2b61 d72aeff2 2203199d d14801c7
    q:
    9760508f 15230bcc b292b982 a2eb840b f0581cf5
    g:
    f7e1a085 d69b3dde cbbcab5c 36b857b9 7994afbb fa3aea82 f9574c0b 3d078267
    5159578e bad4594f e6710710 8180b449 167123e8 4c281613 b7cf0932 8cc8a6e1
    3c167a8b 547c8d28 e0a3ae1e 2bb3a675 916ea37f 0bfa2135 62f1fb62 7a01243b
    cca4f1be a8519089 a883dfe1 5ae59f06 928b665e 807b5525 64014c3b fecf492a
  y:
    ab67aa43 9e8ea5c8 904b1afe 89ae185a 4ef595cf ca9b9114 f05373dc 193cddd3
    baefb0f8 8bd858ea d78632c2 6481c9e0 a4f56878 8f4b0f10 d505ee57 4b1c7d5d
    1196ddf0 1003578f 16272cb6 94f92796 57efd826 50287f9d b6f7e512 75fa4316
    5961aef6 ba663ab8 81c57606 554e4fb6 830b9a7b ce32d5a3 a708d09b 3b6aa8ff
*/