Доброго времени суток)
Подскажите, в чем может быть косяк? Результат валидатора - полный абсурд.
Спасибо)
package com.javarush.task.task18.task1826;
/*
Шифровка
*/
import java.io.*;
import java.security.Key;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
public class Solution {
public static void main(String[] args) throws Exception {
InputStream is = null;
OutputStream os = null;
byte[] inputBytes, outputBytes;
if (args.length != 3 || !("-e".equals(args[0]) || "-d".equals(args[0]))) {
System.out.println(getUsageMessage());
return;
}
try {
is = new FileInputStream(args[1]);
os = new FileOutputStream(args[2]);
inputBytes = readFileByBytes(is);
if ("-e".equals(args[0])) {
outputBytes = CustomCryptoApi.encrypt(inputBytes);
} else {
outputBytes = CustomCryptoApi.decrypt(inputBytes);
}
writeFileByBytes(os, outputBytes);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (is != null) {
try {
is.close();
} catch (IOException e) {
/* ignore */
} finally {
if (os != null) {
try {
os.close();
} catch (IOException e) {
/* ignore */
}
}
}
}
}
}
private static byte[] readFileByBytes(InputStream is) throws IOException {
int readBytes;
byte[] buffer = new byte[8192];
ByteArrayOutputStream os = new ByteArrayOutputStream();
while ((readBytes = is.read(buffer)) != -1)
os.write(buffer, 0, readBytes);
return os.toByteArray();
}
private static void writeFileByBytes(OutputStream os, byte[] bytes) throws IOException {
os.write(bytes);
}
private static String getUsageMessage() {
return "Usage (-[e|d] inputFileName outputFileName)";
}
public static class CustomCryptoApi {
private static final String ALG = "AES";
private static final Key key = generateKey();
public static byte[] encrypt(byte[] plainBytes) throws Exception {
byte[] cipherBytes;
Cipher cipherInstance = Cipher.getInstance(ALG);
cipherInstance.init(Cipher.ENCRYPT_MODE, key);
cipherBytes = cipherInstance.doFinal(plainBytes);
return cipherBytes;
}
public static byte[] decrypt(byte[] cipherBytes) throws Exception {
byte[] decryptBytes;
Cipher cipherInstance = Cipher.getInstance(ALG);
cipherInstance.init(Cipher.DECRYPT_MODE, key);
decryptBytes = cipherInstance.doFinal(cipherBytes);
return decryptBytes;
}
private static Key generateKey() {
byte[] key = new byte[] {'J', 'a', 'v', 'a', 'R', 'a', 's', 'h',
'F', 'o', 'r', 'e', 'v', 'e', 'r', '!'};
return new SecretKeySpec(key, ALG);
}
}
}