Use JDK DES - Java Security

Java examples for Security:DES

Description

Use JDK DES

Demo Code


import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.spec.X509EncodedKeySpec;
import java.util.Objects;

import javax.crypto.Cipher;
import javax.crypto.KeyAgreement;
import javax.crypto.SecretKey;
import javax.crypto.interfaces.DHPublicKey;
import javax.crypto.spec.DHParameterSpec;

import org.apache.commons.codec.binary.Base64;

public class ImoocDH {

    private static String src = "imooc security dh";

    public static void main(String[] args) {
        jdkDH();/*from w  ww .  j a  v a 2s . co  m*/
    }

    public static void jdkDH() {
        try {
            KeyPairGenerator senderKeyPairGenerator = KeyPairGenerator
                    .getInstance("DH");
            senderKeyPairGenerator.initialize(512);
            KeyPair senderKeyPair = senderKeyPairGenerator
                    .generateKeyPair();
            byte[] senderPublicKeyEnc = senderKeyPair.getPublic()
                    .getEncoded();

            KeyFactory receiverKeyFactory = KeyFactory.getInstance("DH");
            X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(
                    senderPublicKeyEnc);
            PublicKey receiverPublicKey = receiverKeyFactory
                    .generatePublic(x509EncodedKeySpec);
            DHParameterSpec dhParameterSpec = ((DHPublicKey) receiverPublicKey)
                    .getParams();
            KeyPairGenerator receiverKeyPairGenerator = KeyPairGenerator
                    .getInstance("DH");
            receiverKeyPairGenerator.initialize(dhParameterSpec);
            KeyPair receiverKeypair = receiverKeyPairGenerator
                    .generateKeyPair();
            PrivateKey receiverPrivateKey = receiverKeypair.getPrivate();
            byte[] receiverPublicKeyEnc = receiverKeypair.getPublic()
                    .getEncoded();

            KeyAgreement receiverKeyAgreement = KeyAgreement
                    .getInstance("DH");
            receiverKeyAgreement.init(receiverPrivateKey);
            receiverKeyAgreement.doPhase(receiverPublicKey, true);
            SecretKey receiverDesKey = receiverKeyAgreement
                    .generateSecret("DES");

            KeyFactory senderKeyFactory = KeyFactory.getInstance("DH");
            x509EncodedKeySpec = new X509EncodedKeySpec(
                    receiverPublicKeyEnc);
            PublicKey senderPublicKey = senderKeyFactory
                    .generatePublic(x509EncodedKeySpec);
            KeyAgreement senderKeyAgreement = KeyAgreement
                    .getInstance("DH");
            senderKeyAgreement.init(senderKeyPair.getPrivate());
            senderKeyAgreement.doPhase(senderPublicKey, true);
            SecretKey senderDesKey = senderKeyAgreement
                    .generateSecret("DES");
            if (Objects.equals(receiverDesKey, senderDesKey)) {
                System.out.println("");
            }

            Cipher cipher = Cipher.getInstance("DES");
            cipher.init(Cipher.ENCRYPT_MODE, senderDesKey);
            byte[] result = cipher.doFinal(src.getBytes());
            System.out.println("jdk dh encrypt : "
                    + Base64.encodeBase64String(result));

            cipher.init(Cipher.DECRYPT_MODE, receiverDesKey);
            result = cipher.doFinal(result);
            System.out.println("jdk dh decrypt : " + new String(result));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

Related Tutorials