Skip to Main Content

Java Security

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Problem in Cryptography

843810Mar 22 2004 — edited Mar 22 2004
Hi all! I'm new in JCE. I am trying to encrypt and decrypt a file for testing. I've d/l the provider from bouncy castle. The code is copied from a book. I can use it to generate a key pair but what I encrypt a file, it throws exception:
java.lang.SecurityException: Unsupported keysize or algorithm parameters
at javax.crypto.Cipher.init(DashoA6275)
at FileEncryptorRSA.encrypt(FileEncryptorRSA.java:198)
at FileEncryptorRSA.main(FileEncryptorRSA.java:51)
What does this exception mean?? Please help!!

The following is the code for encryption only, I left the other part because it's quite large. If someone wants the whole program, you're welcome to leave a message to tony_turtle@yahoo.com to ask me to send the program. Thx a lot!
private static void encrypt(String fileInput) throws Exception
{
	BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
	System.out.print("Public Key to encrypt with: ");
	String publicKeyFilename = in.readLine();

                     // Load the public key bytes
	FileInputStream fis = new FileInputStream(publicKeyFilename);
	ByteArrayOutputStream baos = new ByteArrayOutputStream();

	int theByte = 0;
	while ((theByte = fis.read()) != -1)
	{
		baos.write(theByte);
	}
	fis.close();
	byte[] keyBytes = baos.toByteArray();
	baos.close();

	// Turn the encoded key into a real RSA public key.
	// Public keys are encoded in X.509.
	X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes);
	KeyFactory keyFactory = KeyFactory.getInstance("RSA");
	PublicKey publicKey = keyFactory.generatePublic(keySpec);

	// Create a cipher using that key to initialize it
	Cipher rsaCipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
	rsaCipher.init(Cipher.ENCRYPT_MODE, publicKey);

	// Now create a new 256 bit Rijndael key to encrypt the file itself.
	// This will be the session key.
	KeyGenerator rijndaelKeyGenerator = KeyGenerator.getInstance("Rijndael", "BC");
	rijndaelKeyGenerator.init(256);
	System.out.println("Generating session key...");
	Key rijndaelKey = rijndaelKeyGenerator.generateKey();
	System.out.println("Done generating key.");

	File target;
	String file_name;

	target = new File(fileInput);
	file_name = target.toString();
	System.out.println("file=" + file_name);

	// Open up an output file for the output of the encryption
	String fileOutput = file_name + ENCRYPTED_FILENAME_SUFFIX;
	DataOutputStream output = new DataOutputStream(new FileOutputStream(fileOutput));

	// Encrypt the Rijndael key with the RSA cipher
	// and write it to the beginning of the file.
	byte[] encodedKeyBytes= rsaCipher.doFinal(rijndaelKey.getEncoded());
	output.writeInt(encodedKeyBytes.length);
	output.write(encodedKeyBytes);

	// Now we need an Initialization Vector for the symmetric cipher in CBC mode
	SecureRandom random = new SecureRandom();
	byte[] iv = new byte[16];
	random.nextBytes(iv);

	// Write the IV out to the file.
	output.write(iv);
	IvParameterSpec spec = new IvParameterSpec(iv);

	// Create the cipher for encrypting the file itself.
	Cipher symmetricCipher = Cipher.getInstance("Rijndael/CBC/PKCS5Padding");
	symmetricCipher.init(Cipher.ENCRYPT_MODE, rijndaelKey, spec);

	CipherOutputStream cos = new CipherOutputStream(output, symmetricCipher);
	FileInputStream input = new FileInputStream(file_name);
	theByte = 0;
	while ((theByte = input.read()) != -1)
	{
		cos.write(theByte);
	}
	input.close();
	cos.close();
	System.out.println("File encrypted.");

	return;
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Apr 19 2004
Added on Mar 22 2004
2 comments
195 views