Results 1 to 2 of 2
- 08-08-2011, 08:15 PM #1
Member
- Join Date
- Aug 2011
- Posts
- 1
- Rep Power
- 0
problem usding RSA encode/decode while sending data via sockets
hi
i need to encode data by public key and send them to server via sockets and decode them using private key there
i generated pairkeys and i think i'v done every thing
but in server side , when i am trying to decode , i get -> Exception in thread "main" javax.crypto.BadPaddingException: Data must start with zero
the RSAHandler which i'm using is not written by me , i just found it in internet
here is my code
PublicKey readKeyFromFileC(String keyFileName) throws IOException {
InputStream in = new FileInputStream(keyFileName);
ObjectInputStream oin = new ObjectInputStream(new BufferedInputStream(in));
try {
BigInteger m = (BigInteger) oin.readObject();
BigInteger e = (BigInteger) oin.readObject();
RSAPublicKeySpec keySpec = new RSAPublicKeySpec(m, e);
KeyFactory fact = KeyFactory.getInstance("RSA");
PublicKey pubKey = fact.generatePublic(keySpec);
return pubKey;
} catch (Exception e) {
throw new RuntimeException("Spurious serialisation error", e);
} finally {
oin.close();
}
}
PrivateKey readKeyFromFileS(String keyFileName) throws IOException {
InputStream in = new FileInputStream(keyFileName);
ObjectInputStream oin = new ObjectInputStream(new BufferedInputStream(in));
try {
BigInteger m = (BigInteger) oin.readObject();
BigInteger e = (BigInteger) oin.readObject();
RSAPrivateKeySpec keySpec = new RSAPrivateKeySpec(m, e);
KeyFactory fact = KeyFactory.getInstance("RSA");
PrivateKey privKey = fact.generatePrivate(keySpec);
return privKey;
} catch (Exception e) {
throw new RuntimeException("Spurious serialisation error", e);
} finally {
oin.close();
}
}
public String rsaEncrypt(byte[] data) throws Exception {
PublicKey pubKey = (PublicKey) readKeyFromFileC("/public.key");
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, pubKey);
byte[] cipherData = cipher.doFinal(data);
return cipherData.toString();
}
public String rsaDecrypt(byte[] data) throws Exception {
PrivateKey privKey = (PrivateKey) readKeyFromFileS("/private.key");
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.DECRYPT_MODE, privKey);
byte[] deCipherData = cipher.doFinal(data);
return deCipherData.toString();
}
--------------
here is the client side code
public void sendToServer(String sendText) throws Exception {
Socket clientSocket = new Socket("localhost", 5000);
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter (clientSocket.getOutputStream())), true);
out.println(new String(sendText));
}
tcpClient.sendToServer(rsaHandler.rsaEncrypt(login Info.getBytes()));
String res = tcpClient.RecieveFromServer();
-------------------------------
and server side code
RSAHandler rsaHandler = new RSAHandler();
Message = rsaHandler.rsaDecrypt(fromclient.getBytes()).toCha rArray();
please help me , its long time that i'm dealing with this code
i think maybe the problem is in converting string to byte / byte to string in encoding/decoding and sending process , but i can't fix it
thanks in advance if somebody can solve my problem
the italic line , is where i get that exception javax.crypto.BadPaddingException: Data must start with zero
- 08-09-2011, 05:39 AM #2
Don't double post the same question and don't hijack another poster's thread. Also, don't post to old, long dead threads.
Your post in Padding Problem as been removed.
db
Similar Threads
-
Encode data to torrent?
By greatmajestics in forum AWT / SwingReplies: 0Last Post: 04-28-2010, 04:12 PM -
Windows Service sending data to a java app through Sockets?
By JavaInLove in forum NetworkingReplies: 1Last Post: 01-09-2010, 02:16 PM -
Getting problem in UTF-8 Encode/Decode with Java
By sagarsway in forum Advanced JavaReplies: 2Last Post: 12-22-2008, 07:01 PM -
Sending files over sockets!
By rameshraj in forum NetworkingReplies: 2Last Post: 05-30-2008, 10:18 PM -
Sending Mail Using Sockets
By Java Tip in forum java.netReplies: 0Last Post: 04-07-2008, 08:05 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks