2 Replies - 1405 Views - Last Post: 18 November 2012 - 10:38 AM Rate Topic: -----

#1 bladevetren  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 18-August 12

file encryption fault

Posted 18 November 2012 - 06:49 AM

hello, i am attempting to create a file encryptor by converting the file input stream into a byte array and then encrypting that via RSA; then finally encrypting the file name and writing the contents and name to a file output stream. the problem which is occurring is that whenever i run the program it throws the exception:

java.lang.NumberFormatException: Zero length BigInteger
at java.math.BigInteger.<init>(Unknown Source)
at file_encryption.encrypt1(file_encryption.java:167)
at file_encryption.main(file_encryption.java:202)
Exception in thread "main" java.lang.NullPointerException
at java.io.FileOutputStream.write(Unknown Source)
at file_encryption.main(file_encryption.java:205)

in conclusion please may you tell me why this problem is occurring, and the necessary steps for this programs completion.

here is the code:
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigInteger;
import java.security.SecureRandom;
import java.util.HashSet;
import java.util.Random;
import java.util.Set;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;


public class file_encryption {
	 private static BigInteger N;
     private static BigInteger phiN;
     private static BigInteger p;
     private static BigInteger q;
     private static BigInteger e;
     private static BigInteger d;
     
    
    
	  
	    
	    
	    
	    
	    
	    	 public file_encryption(int keySize)
			    {
			        if (keySize < 1)
			            throw new IllegalArgumentException("Key size too small.");
			        SecureRandom rand = new SecureRandom();
			        generatePQ(keySize / 2, rand);
			        N = p.multiply(q);
			        phiN = (p.subtract(BigInteger.ONE)).multiply((q.subtract(BigInteger.ONE)));
			        generateExponents(invertibleSet());
			    }
			 
			

			    private void generatePQ(int bitLength, Random rand)
			    {
			        while (true)
			        {
			            p = generateOddPrime(bitLength, rand);
			        q = generateOddPrime(bitLength, rand);
			            if (!p.equals(q))
			            return;
			        }
			}
			 
			   
			    private BigInteger generateOddPrime(int bitLength, Random rand)
			    {
			        BigInteger two = new BigInteger("2");
			        while (true)
			        {
			            BigInteger prime = BigInteger.probablePrime(bitLength, rand);
			            if (!prime.mod(two).equals(BigInteger.ZERO))
			                return prime;
			    }
			    }
			 
			  
			    private void generateExponents(BigInteger[] invertibleSet)
			    {
			        Random rand = new Random();
			        while (true)
			        {
			            BigInteger invertible = invertibleSet[rand
			                    .nextInt(invertibleSet.length)];
			            BigInteger inverse = invertible.modInverse(phiN);
			            if (invertible.multiply(inverse).mod(phiN)
			                    .equals(BigInteger.ONE.mod(phiN)))
			            {
			                e = invertible;
			                //input the alternative value of the privateKey here
			                d = inverse;
			                return;
			            }
			   }
			    }
			 
			   
			    private BigInteger[] invertibleSet()
			    {
			        final int maxSize = 100000;
			        Set<BigInteger> invertibles = new HashSet<BigInteger>();
			        BigInteger end = N.subtract(BigInteger.ONE);
			        for (BigInteger i = new BigInteger("5"); i.compareTo(end) < 0; i = i
			                .add(BigInteger.ONE))
			        {
			            if (i.gcd(phiN).equals(BigInteger.ONE))
			            {
			            invertibles.add(i);
			                if (invertibles.size() == maxSize)
			                    break;
			        }
			        }
			        return invertibles.toArray(new BigInteger[invertibles.size()]);
			    }
			 
			 
			   

			     
			     
			     

			 
			
			  
			    public BigInteger getModulus()
			    {
			    return N;
			    }
			 
			   
			    public BigInteger getPublicKeyExponent()
			    {
			        return e;
			    }
			 
			  
			    public BigInteger getPrivateKeyExponent()
			    {
			    return d;
			    }
		    
	    
	    	 private String encrypt(String plainText)
	 	    {
	 	    	
	 	    	
	 	        BigInteger msg = new BigInteger(plainText.getBytes());
	 	        byte[] encrypted = msg.modPow(e,N).toByteArray();
	 	        return toHex(encrypted);
	 	    }
	 	    private String toHex(byte[] bytes)
	 	    {
	 	        BigInteger bi = new BigInteger(1, bytes);
	 	        return String.format("%0" + (bytes.length << 1) + "X", bi);
	 	    }
	 	   
	      
	 	 private byte[] encrypt1(String plainText)
	 	    {
	 	 
	 	    	try{
	 	    	InputStream data2 = new FileInputStream(plainText);
	 	    	
	 	    	byte [] str = getBytes(data2);
	 	    	
	 	    	
	 	        BigInteger msg = new BigInteger(str);
	 	        byte[] encrypted = msg.modPow(e,N).toByteArray();
	 	      
	 	     data2.close();
	 	       return encrypted;
	 	    	 } catch (Exception e1) {
	 	 			e1.printStackTrace();
	 	 		  }
	 			return null;
	 	    }
	 	 
	 	    public static byte[] getBytes(InputStream is) throws IOException {

	 	        int len;
	 	        int size = 1024;
	 	        byte[] buf;

	 	        if (is instanceof ByteArrayInputStream) {
	 	          size = is.available();
	 	          buf = new byte[size];
	 	          len = is.read(buf, 0, size);
	 	        } else {
	 	          ByteArrayOutputStream bos = new ByteArrayOutputStream();
	 	          buf = new byte[size];
	 	          while ((len = is.read(buf, 0, size)) != -1)
	 	            bos.write(buf, 0, len);
	 	          buf = bos.toByteArray();
	 	        }
	 	        return buf;
	 	      }
	 	    
	 	   public static void main(String[] args){
	 		  file_encryption cipher = new file_encryption(1000);
	 	    	 String filename="test.txt";
	 	    	 String name =cipher.encrypt(filename);
	 	    	 byte[] data=cipher.encrypt1(filename);
	 	    	 try {
	 				FileOutputStream n = new FileOutputStream(name);
	 				n.write(data);
	 				n.close();
	 			} catch (FileNotFoundException e) {
	 				// TODO Auto-generated catch block
	 				e.printStackTrace();
	 			} catch (IOException e) {
	 				// TODO Auto-generated catch block
	 				e.printStackTrace();
	 			}
	 	    	 
	 	     }
}





Is This A Good Question/Topic? 0
  • +

Replies To: file encryption fault

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: file encryption fault

Posted 18 November 2012 - 10:13 AM

java.lang.NumberFormatException: Zero length BigInteger
at java.math.BigInteger.<init>(Unknown Source)
at file_encryption.encrypt1(file_encryption.java:167)

What is at line 167 ?

162              InputStream data2 = new FileInputStream(plainText);  
163                    
164              byte [] str = getBytes(data2);  
165                    
166                    
167              BigInteger msg = new BigInteger(str);  


Zero length Big Integer: I'll bet that str[] has a lenght of 0
Was This Post Helpful? 0
  • +
  • -

#3 bladevetren  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 18-August 12

Re: file encryption fault

Posted 18 November 2012 - 10:38 AM

i see, i appoligise for my ignorance, i am not yet that familiar with errors such as this
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1