集册 Java实例教程 从客户端接收RSA非对称公钥,生成AES对称密钥

从客户端接收RSA非对称公钥,生成AES对称密钥

欢马劈雪     最近更新时间:2020-01-02 10:19:05

599
  • Receives a RSA asymmetric public key from the Client
  • generates a AES symmetric key
  • encrypts the AES symmetric key with the RSA asymmetric public key
  • sends the encrypted data to the Client


import org.apache.logging.log4j.LogManager;

import org.apache.logging.log4j.Logger;

import sun.misc.IOUtils;/*时 代 J a v a - nowjava.com 提 供*/


import javax.crypto.Cipher;

import javax.crypto.KeyGenerator;

import javax.crypto.NoSuchPaddingException;

import javax.crypto.SecretKey;

import javax.crypto.spec.IvParameterSpec;

import java.io.*;

import java.net.ServerSocket;

import java.net.Socket;

import java.security.NoSuchAlgorithmException;

import java.security.NoSuchProviderException;

import java.security.PublicKey;

import java.security.SecureRandom;
/** n o w    j a v a  . c o m 提 供 **/

public class SecureServer {


    private static final Logger logger = LogManager.getLogger(SecureServer.class);


    public static void main(String[] args) throws NoSuchPaddingException, NoSuchAlgorithmException, NoSuchProviderException {

        if (args.length != 1) {

            System.err.println("Usage: java SecureServer <port number>");

            System.exit(1);

        }


        int portNumber = Integer.parseInt(args[0]);

        byte[] message = null;


        logger.trace("Sever started");


        try {

            ServerSocket serverSocket =

                    new ServerSocket(Integer.parseInt(args[0]));

            Socket clientSocket = serverSocket.accept();

            ObjectInputStream in = new ObjectInputStream(

                    clientSocket.getInputStream());


            // receives RSA asymmetric public key from Client

            PublicKey publicKey = (PublicKey) in.readObject();


            // generates AES symmetric key

            KeyGenerator keyGen = KeyGenerator.getInstance("AES");

            keyGen.init(128);

            SecretKey secretKey = keyGen.generateKey();


            logger.trace("generated AES symmetric key: " + secretKey.toString());

            logger.trace("received public key: " + publicKey.toString());



            // uses asymmetric public RSA key from client to encrypt symmetric AES key

            Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");

            cipher.init(Cipher.ENCRYPT_MODE, publicKey);

            byte[] encryptedData = cipher.doFinal(secretKey.getEncoded());


            OutputStream outputStream = clientSocket.getOutputStream();

            DataOutputStream dataOutputStream = new DataOutputStream(outputStream);

            dataOutputStream.writeInt(encryptedData.length);

            dataOutputStream.write(encryptedData, 0, encryptedData.length);

            logger.trace("sent AES symmetric key");



            // read IV

            DataInputStream dataInputStream = new DataInputStream(

                    clientSocket.getInputStream());

            byte[] iv = new byte[16];

            dataInputStream.read(iv);

            IvParameterSpec ivParameterSpec = new IvParameterSpec(iv);


            // read encrypted message

            int length = dataInputStream.readInt(); // read length of incoming message

            if (length > 0) {

                message = new 
展开阅读全文