· 6 years ago · Mar 04, 2019, 10:04 PM
1Exception in thread "main" java.lang.ClassCastException: javax.crypto.CipherInputStream cannot be cast to javax.imageio.stream.ImageOutputStream
2at encypt.com.trial.main(trial.java:82)
3
4public class trial {
5 public static void main(String[] arg) throws Exception {
6
7 // Scanner to read the user's password. The Java cryptography
8 // architecture points out that strong passwords in strings is a
9 // bad idea, but we'll let it go for this assignment.
10 Scanner scanner = new Scanner(System.in);
11 // Arbitrary salt data, used to make guessing attacks against the
12 // password more difficult to pull off.
13 byte[] salt = { (byte) 0xc7, (byte) 0x73, (byte) 0x21, (byte) 0x8c,
14 (byte) 0x7e, (byte) 0xc8, (byte) 0xee, (byte) 0x99 };
15
16 {
17 File inputFile = new File("sheep.png");
18 BufferedImage input = ImageIO.read(inputFile);
19 Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
20 SecretKeyFactory keyFac = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
21 // Get a password from the user.
22 System.out.print("Password: ");
23 System.out.flush();
24 PBEKeySpec pbeKeySpec = new PBEKeySpec(scanner.nextLine().toCharArray());
25 // Set up other parameters to be used by the password-based
26 // encryption.
27 PBEParameterSpec pbeParamSpec = new PBEParameterSpec(salt, 20);
28 SecretKey pbeKey = keyFac.generateSecret(pbeKeySpec);
29 // Make a PBE Cyhper object and initialize it to encrypt using
30 // the given password.
31 Cipher pbeCipher = Cipher.getInstance("PBEWithMD5AndDES");
32 pbeCipher.init(Cipher.ENCRYPT_MODE, pbeKey, pbeParamSpec);
33 FileOutputStream output = new FileOutputStream("sheepTest.png");
34 CipherOutputStream cos = new CipherOutputStream(
35 output, pbeCipher);
36 //File outputFile = new File("image.png");
37 ImageIO.write(input,"PNG",cos);
38 cos.close();
39
40 }
41 // Now, create a Cipher object to decrypt for us. We are repeating
42 // some of the same code here to illustrate how java applications on
43 // two different hosts could set up compatible encryption/decryption
44 // mechanisms.
45 {
46 File inputFile = new File("sheepTest.png");
47 BufferedImage input = ImageIO.read(inputFile);
48 // Get another (hopefully the same) password from the user.
49 System.out.print("Decryption Password: ");
50 System.out.flush();
51 PBEKeySpec pbeKeySpec = new PBEKeySpec(scanner.next().toCharArray());
52 // Set up other parameters to be used by the password-based
53 // encryption.
54 PBEParameterSpec pbeParamSpec = new PBEParameterSpec(salt, 20);
55 SecretKeyFactory keyFac = SecretKeyFactory
56 .getInstance("PBEWithMD5AndDES");
57 SecretKey pbeKey = keyFac.generateSecret(pbeKeySpec);
58 // Make a PBE Cyper object and initialize it to decrypt using
59 // the given password.
60 Cipher pbeCipher = Cipher.getInstance("PBEWithMD5AndDES");
61 pbeCipher.init(Cipher.DECRYPT_MODE, pbeKey, pbeParamSpec);
62 // Decrypt the ciphertext and then print it out.
63 /*byte[] cleartext = pbeCipher.doFinal(ciphertext);
64 System.out.println(new String(cleartext));*/
65 FileInputStream output = new FileInputStream("sheepTest.png");
66 CipherInputStream cos = new CipherInputStream(
67 output, pbeCipher);
68 ImageIO.write(input,"PNG",(ImageOutputStream) cos);
69 cos.close();
70
71 }
72 }
73}
74
75ImageIO.write(input,"PNG",(ImageOutputStream) cos);
76
77FileOutputStream output = new FileOutputStream("sheepTest.png");
78 CipherOutputStream cos = new CipherOutputStream(output, pbeCipher);
79 ImageIO.write(input, "PNG", cos);
80 cos.close();