Jump to content



Photo

latest problem with RSA encryption


  • Please log in to reply
1 reply to this topic

#1 Original Poster

Original Poster

    C++ n00b

  • Tech Issues Solved: 1
  • Joined: 15-July 08
  • Location: my room
  • OS: windows 7, backtrack 5, OSx 10.6

Posted 26 November 2012 - 01:01

i am having issues with decrypting sent messages .... here is the encryption code from person A ...

	RSAPublicKeySpec keySpec = new RSAPublicKeySpec(m, l);
	   KeyFactory fact = KeyFactory.getInstance("RSA");
	   pubKey = fact.generatePublic(keySpec);
	byte[] src = outputLine.getBytes();
	Cipher cipher = Cipher.getInstance("RSA");
	cipher.init(Cipher.ENCRYPT_MODE, pubKey);
	byte[] cipherData = cipher.doFinal(src);
	out.printf(new String(cipherData));
	


and here is the decryption code from person B

	  byte[] src = inputLine.getBytes();
	  System.out.println(src);
	  Cipher cipher = Cipher.getInstance("RSA");
	  cipher.init(Cipher.DECRYPT_MODE, jim);
	  byte[] cipherData = cipher.doFinal(src);
	  System.out.println("Decrypted: " + new String(cipherData));

all are in classes, tried and caught ... when decrypting the the sent string with private key ..i get the error "javax.crypto.BadPaddingException: Data must start with zero"

I have tried this in a seprate file from my work and all results are fine....


below is the test script that works fine.... any ideas ? ( i know not all imports are used in test code )

import java.awt.*;
import java.awt.RenderingHints.Key;
import java.net.*;		
import java.awt.event.*;  
import javax.swing.*;	  
import javax.swing.event.*;
import java.io.*;
import javax.crypto.*;
import javax.crypto.spec.*;
import java.security.InvalidKeyException;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.spec.*;
import java.util.UUID;

public class test
{
public static void main(String arg[]) {

  KeyPairGenerator kpg;
  try {
   kpg = KeyPairGenerator.getInstance("RSA");
  kpg.initialize(2048);
  KeyPair kp = kpg.genKeyPair();
  PublicKey publicKey = kp.getPublic();
  PrivateKey privateKey = kp.getPrivate();

  KeyFactory fact = KeyFactory.getInstance("RSA");
  RSAPublicKeySpec pub = fact.getKeySpec(kp.getPublic(),
	RSAPublicKeySpec.class);
  RSAPrivateKeySpec priv = fact.getKeySpec(kp.getPrivate(),
	RSAPrivateKeySpec.class);

  String bob = "hi";
  byte[] src = bob.getBytes();
   Cipher cipher = Cipher.getInstance("RSA");
	cipher.init(Cipher.ENCRYPT_MODE, publicKey);
	byte[] cipherData = cipher.doFinal(src);
	System.out.println(new String(cipherData));
  
  
	Cipher cipher1 = Cipher.getInstance("RSA");
	 cipher1.init(Cipher.DECRYPT_MODE, privateKey);
	 byte[] cipherData1 = cipher1.doFinal(cipherData);
	 System.out.println(new String(cipherData1));
  
  } catch (NoSuchAlgorithmException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (InvalidKeySpecException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (NoSuchPaddingException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (InvalidKeyException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (IllegalBlockSizeException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (BadPaddingException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }

   }
}



#2 OP Original Poster

Original Poster

    C++ n00b

  • Tech Issues Solved: 1
  • Joined: 15-July 08
  • Location: my room
  • OS: windows 7, backtrack 5, OSx 10.6

Posted 26 November 2012 - 03:32

if anyone is interested its something to do with the sent format of the string ..... it decrypted something once ... which means it happened to send a good string once haha