Today I tried to write secure communication between client and server
i want to use ssl way
step by step I make example
and you just this process if you want secure communication
## Client ##
1. making publicKey and privateKey using RSA
2, making publicKey to Base64 String
3. give it(publicKey) to Server
## Server ##
4. receive publicKey from client and decode Bas64String to byte[]
5. get object publicKey
6. making symmetric-key (Random 16 number)
7. encryting publickey symmetric-key and give it to client
.. and
you can make encrypted message by using des or any with symmetric-key
The point is symmetric-key is exposed to only one client and one server
/**
* (c)Copyright 2010-2010, BaruSoft Co., Ltd. All rights reserved <br/>
*
* @description <br/>
*
* @create 2013. 8. 21.
* @author jjhangu
*/
package secure;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.X509EncodedKeySpec;
import java.util.Formatter;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
public class RSACrypt {
public static void main(String[] args) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException, InvalidKeySpecException, NoSuchProviderException, IOException {
final KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA");
final KeyPair keyPair = generator.generateKeyPair();
final PublicKey publicKey = keyPair.getPublic();
final PrivateKey privateKey = keyPair.getPrivate();
/**
* 시나리오 1
*
* Client
* 1. make RSA keys (publicKey, priateKey)
* 2. give publicKey to Server
*
* Server
* 3. make Symmetric-key using publicKey given by client
* 4. give it to client
*
* Client and Server Communicate Using Symmetric-key using DES Algorithm
*
*/
// Client
// 1 making public and private key
final BASE64Encoder encoder = new BASE64Encoder();
final String pubKeyStr = encoder.encode(publicKey.getEncoded());
System.out.println("#######################################");
System.out.println("Client[publicKey] BASE64 ");
System.out.println("#######################################");
System.out.println(pubKeyStr);
System.out.println();
System.out.println();
final String priKeyStr = encoder.encode(privateKey.getEncoded());
System.out.println("#######################################");
System.out.println("Client[privateKey] BASE64 ");
System.out.println("#######################################");
System.out.println(priKeyStr);
System.out.println();
System.out.println();
// give it to public key to server
// Server
// 3. Get Public Key obejct
final BASE64Decoder decoder = new BASE64Decoder();
final byte[] sigBytes2 = decoder.decodeBuffer(pubKeyStr);
// PublicKey from client
final PublicKey publicServerKey = convertKeytoKeyObject(sigBytes2);
final String pubServerKeyStr = encoder.encode(publicServerKey.getEncoded());
System.out.println("#######################################");
System.out.println("Server [publicKey] BASE64 ");
System.out.println("#######################################");
System.out.println(pubServerKeyStr);
System.out.println();
System.out.println();
// 3. make Symmetric-key using publicKey given by client
final Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, publicServerKey);
final String symmetricKey = "0123456789ABCDEF";
System.out.println("#######################################");
System.out.println("Server [symmetricKey] ");
System.out.println("#######################################");
System.out.println(symmetricKey);
System.out.println();
System.out.println();
final byte[] b0 = cipher.doFinal(symmetricKey.getBytes());
// give symmetricKey to Client
// client decode
cipher.init(Cipher.DECRYPT_MODE, privateKey);
final byte[] b1 = cipher.doFinal(b0);
System.out.println("Client Finally Decode SymmetricKey : " + new String(b1));
// only one client and server will know symmetricKey
// and Server Client will Have Same symmetricKey and Communucating message using des
// algorithm with this symmetricKey
}
public static String bytesToHexString(byte[] bytes) {
final StringBuilder sb = new StringBuilder(bytes.length * 2);
final Formatter formatter = new Formatter(sb);
for (final byte b : bytes) {
formatter.format("%02x", b);
}
return sb.toString();
}
private static PublicKey convertKeytoKeyObject(byte[] publicKey) throws NoSuchAlgorithmException, InvalidKeySpecException, NoSuchProviderException {
final X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(publicKey);
final KeyFactory keyFact = KeyFactory.getInstance("RSA");
return keyFact.generatePublic(x509KeySpec);
}
}
No comments:
Post a Comment