Ai galera, com a ajuda do Oyama conseguir resolver cmo criptografar uma string por sockets!
vc deve fazer os seguintes passos no seu console:
-keytool -genkey
-keytool -genkey -alias[nome do alias] -keypass [keypass] -kelalg[alg]
-copiar o arquivo .keystore pra ond está o seu projeto!!!
segue abaixo o codigo:
//class servidor
import java.io.*;
import java.net.*;
import java.security.Key;
import java.security.KeyPairGenerator;
import java.security.KeyPair;
import java.security.KeyStore;
import java.security.PublicKey;
import java.security.PrivateKey;
import java.security.Security;
import java.security.cert.Certificate;
import javax.crypto.Cipher;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
public class Servidor {
private static byte[] encrypt(byte[] inpBytes, PublicKey key, String xform) throws Exception {
Cipher cipher = Cipher.getInstance(xform);
cipher.init(Cipher.ENCRYPT_MODE, key);
return cipher.doFinal(inpBytes);
}
private static byte[] decrypt(byte[] inpBytes, PrivateKey key, String xform) throws Exception{
Cipher cipher = Cipher.getInstance(xform);
cipher.init(Cipher.DECRYPT_MODE, key);
return cipher.doFinal(inpBytes);
}
public static void main(String args[]) throws Exception
{
Security.addProvider(new BouncyCastleProvider());
String xform = "RSA/NONE/PKCS1Padding";
FileInputStream fis = new FileInputStream( ".keystore" );
KeyStore kpg = KeyStore.getInstance("JKS", "SUN");
kpg.load(fis, "keystore_passwd".toCharArray());
PrivateKey prvk = (PrivateKey)kpg.getKey("marcelo", "key_passwd".toCharArray());
ServerSocket server = null;
Socket cliente = null;
try
{
server = new ServerSocket(7000);
System.out.println("Aguardando um cliente conectar...");
cliente = server.accept();
System.out.println("Conexão efetuada");
System.out.println("");
//recebe dados do cliente
DataInputStream entrada = new DataInputStream(cliente.getInputStream());
int size = entrada.readInt();
System.out.println("Recebido size: " + size);
byte[] recvBytes = new byte[size];
entrada.read(recvBytes);
System.out.println("Recebido bytes: " + new String(recvBytes));
byte[] decBytes = decrypt(recvBytes, prvk, xform);
System.out.println(new String(decBytes));
/*//envia dados ao cliente
DataOutputStream output = new DataOutputStream(cliente.getOutputStream());
byte[] sentBytes = ("reply: " + new String(recvBytes)).getBytes();
output.writeInt(sentBytes.length);
output.write(sentBytes);
*/
cliente.close();
}
catch(IOException e)
{
System.out.println("Algum problema ocorreu para criar ou receber o socket.");
}
finally
{
try
{
server.close();
}
catch(IOException e)
{
}
}
}
}
//class cliente
import java.io.*;
import java.net.*;
//import java.security.KeyPairGenerator;
//import java.security.KeyPair;
import java.security.KeyStore;
import java.security.PublicKey;
import java.security.PrivateKey;
import java.security.Security;
import java.security.cert.Certificate;
import javax.crypto.Cipher;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
public class Cliente {
private static byte[] encrypt(byte[] inpBytes, PublicKey key, String xform) throws Exception {
Cipher cipher = Cipher.getInstance(xform);
cipher.init(Cipher.ENCRYPT_MODE, key);
return cipher.doFinal(inpBytes);
}
private static byte[] decrypt(byte[] inpBytes, PrivateKey key, String xform) throws Exception{
Cipher cipher = Cipher.getInstance(xform);
cipher.init(Cipher.DECRYPT_MODE, key);
return cipher.doFinal(inpBytes);
}
public static void main(String args[]) throws Exception
{
Security.addProvider(new BouncyCastleProvider());
String xform = "RSA/NONE/PKCS1Padding";
FileInputStream fis = new FileInputStream( ".keystore" );
KeyStore kpg = KeyStore.getInstance("JKS", "SUN");
kpg.load(fis, "keystore_passwd".toCharArray());
Certificate cert = kpg.getCertificate("marcelo");
PublicKey pubk = cert.getPublicKey();
Socket cliente = null;
try
{
cliente = new Socket("127.0.0.1",7000);
//envia dados para o servidor
DataOutputStream output = new DataOutputStream(cliente.getOutputStream());
byte[] sentBytes = "oi".getBytes();
byte[] encBytes = encrypt(sentBytes, pubk, xform);
output.writeInt(encBytes.length);
System.out.println("Enviado size: " + encBytes.length);
output.write(encBytes);
System.out.println("Enviado bytes: " + new String (encBytes));
System.out.println("");
/* //recebe dados do servidor
DataInputStream entrada = new DataInputStream(cliente.getInputStream());
int size = entrada.readInt();
byte[] recvBytes = new byte[size];
entrada.read(recvBytes);
System.out.println( "Recebido do servidor: " + new String(recvBytes) );
*/
}
catch(IOException e)
{
System.out.println("Algum problema ocorreu ao criar ou enviar dados pelo socket.");
}
finally
{
try
{
cliente.close();
}
catch(IOException e){}
}
}
}
vlw galera
[color=darkred]Editado pelo moderador para incluir as tags Code[/color]