October 3rd, 2012 03:03 PM
#1
Need help creating an exception for wrong password in decrypt method
The following code works but I am trying to prevent decrypting a file if you have the wrong key.
// Example code showing how to use Java's Password-Based encryption. This
// example is a simplified form of the code in the documentation for the
// java cryptography architecture.
import java.awt.image.BufferedImage;
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.io.OutputStream;
import java.security.GeneralSecurityException;
//import java.security.NoSuchAlgorithmException;
import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.CipherOutputStream;
//import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.imageio.ImageIO;
import javax.crypto.spec.DESKeySpec;
public class sample
{
static String key = "squirrel123";
static String key1 = "squirrel1234";
public static void main(String[] arg) throws Exception
{
encryptFile(key, "c:\\users\\test\\rose.jpg",
"c:\\users\\test\\encrypt.jpg","JPG");
decryptFile(key1, "c:\\users\\test\\encrypt.jpg",
"c:\\users\\test\\decrypted_output.jpg","JPG");
}
public static void encryptFile(String key, String nonEncryptedFile, String EncryptedFile, String typeFile) throws GeneralSecurityException, FileNotFoundException
{
DESKeySpec dks = new DESKeySpec(key.getBytes());
SecretKeyFactory skf = SecretKeyFactory.getInstance("DES");
SecretKey desKey = skf.generateSecret(dks);
Cipher pbeCipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
if((typeFile.equals("JPG"))||
(typeFile.equals("GIF"))||
(typeFile.equals("PNG"))||
(typeFile.equals("BMP")))
{
File inputFile = new File(nonEncryptedFile);
BufferedImage input = null;
pbeCipher.init(Cipher.ENCRYPT_MODE, desKey);
try
{
input = ImageIO.read(inputFile);
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
FileOutputStream output = null;
try
{
output = new FileOutputStream(EncryptedFile);
}
catch
(FileNotFoundException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
CipherOutputStream cos = new CipherOutputStream(output, pbeCipher);
try
{
ImageIO.write(input,typeFile,cos);
}
catch (IOException e1)
{
// TODO Auto-generated catch block
e1.printStackTrace();
}
try
{
cos.close();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void doCopy(InputStream is, OutputStream os) throws IOException
{
byte[] bytes = new byte[64];
int numBytes;
while ((numBytes = is.read(bytes)) != -1) {
os.write(bytes, 0, numBytes);
}
os.flush();
os.close();
is.close();
}
public static void decryptFile(String key, String encryptedFile, String decryptedFile, String typeFile)throws GeneralSecurityException, FileNotFoundException
{
DESKeySpec dks = new DESKeySpec(key1.getBytes());
SecretKeyFactory skf = SecretKeyFactory.getInstance("DES");
SecretKey desKey = skf.generateSecret(dks);
Cipher pbeCipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
try
{
pbeCipher.init(Cipher.DECRYPT_MODE, desKey);
System.out.println("Decrypt mode!");
// Decrypt the ciphertext and then print it out.
FileInputStream output = null;
if((typeFile.equals("JPG"))||
(typeFile.equals("GIF"))||
(typeFile.equals("PNG"))||
(typeFile.equals("BMP")))
{
try {
output = new FileInputStream(encryptedFile);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
CipherInputStream cis = new CipherInputStream(output, pbeCipher);
BufferedImage input = null;
try {
input = ImageIO.read(cis);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
FileOutputStream out = null;
try {
out = new FileOutputStream(decryptedFile);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
ImageIO.write(input,typeFile, out);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
cis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} catch (IOException e ){
System.out.println("invalid");
}
}
}
Last edited by liftthis; October 3rd, 2012 at 03:05 PM .
October 4th, 2012 06:00 AM
#2
Re: Need help creating an exception for wrong password in decrypt method
Please use code tags when posting code.
The following code works but I am trying to prevent decrypting a file if you have the wrong key
What does your code do now if you enter the wrong key - how can it decrypt the file if the Key is incorrect?
Posting Permissions
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
Forum Rules
Click Here to Expand Forum to Full Width
Bookmarks