ai galera…estou tentando fzer um socket usando dados criptografados(utilizo o bouncy castle! soh q tah dando erro da hora d decriptografar e naum to sabendo resolver! alguem ai ajuda???
vo dxa os codigos cliente e servidor ok!?!??!
//class cliente
import <a href="http://java.io">java.io</a>.<em>;
import <a href="http://java.net">java.net</a>.</em>;
import java.security.KeyPairGenerator;
import java.security.KeyPair;
import java.security.PublicKey;
import java.security.PrivateKey;
import java.security.Security;
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";
KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA", "BC");
kpg.initialize(512);
KeyPair kp = kpg.generateKeyPair();
PublicKey pubk = kp.getPublic();
PrivateKey prvk = kp.getPrivate();
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));
//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){}
}
}
}
//class servidor
import <a href="http://java.io">java.io</a>.<em>;
import <a href="http://java.net">java.net</a>.</em>;
import java.security.KeyPairGenerator;
import java.security.KeyPair;
import java.security.PublicKey;
import java.security.PrivateKey;
import java.security.Security;
import javax.crypto.Cipher;
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";
KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA", "BC");
kpg.initialize(512);
KeyPair kp = kpg.generateKeyPair();
PublicKey pubk = kp.getPublic();
PrivateKey prvk = kp.getPrivate();
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);
entrada.read(decBytes);
System.out.println("Recebido bytes: " + new String(decBytes));
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)
{
}
}
}
}
vlw