我的加密码:
public class Encryptor{
private static String key="asdfghjklqwe@321" ;
private static String initVector = "RandomInitVector";
public static void encryptFile(File inputFile, String outputFolderPath) {
try {
Key secretKey = new SecretKeySpec(key.getBytes("UTF-8"),"AES");
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, secretKey,new IvParameterSpec(initVector.getBytes("UTF-8")));
FileInputStream inputStream = new FileInputStream(inputFile);
byte[] inputBytes = new byte[(int) inputFile.length()];
inputStream.read(inputBytes);
byte[] outputBytes = cipher.doFinal(inputBytes);
File encryptedFile = new File(outputFolderPath,inputFile.getName());
encryptedFile.getParentFile().mkdirs();
encryptedFile.createNewFile();
FileOutputStream outputStream = new FileOutputStream(encryptedFile);
outputStream.write(outputBytes);
inputStream.close();
outputStream.close();
System.out.println("File encryption completed successfully.");
inputFile.delete();
System.out.println("File deleted from the original location.");
} catch (Exception e) {
e.printStackTrace();
}
}
}
我有一个调用此方法的JAR文件(实现了一个Timer).JAR中的类文件代码
public class encryptFiles extends TimerTask {
public static void main(String[] args) throws IOException {
try {
encryptFiles mainObj = new encryptFiles();
Timer timer = new Timer();
timer.scheduleAtFixedRate(mainObj, new Date(), 3600000L);
} catch (Exception ae) {
ae.printStackTrace();
}
}
public void run(){
File ipath = new File(INPUT);
File[] listOfFilesClient = ipath.listFiles();
for (File fileToBeProcessed : listOfFilesClient)
{
try
{
Encryptor.encryptFiles(fileToBeProcessed,OUTPUT);
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
}
但我得到了这个例外:
Exception in thread "Timer-0" java.lang.NoSuchFieldError: address
at java.net.InetAddress.init(Native Method)
at java.net.InetAddress.<clinit>(InetAddress.java:271)
at javax.crypto.JarVerifier.getSystemEntropy(JarVerifier.java:783)
at javax.crypto.JarVerifier.testSignatures(JarVerifier.java:706)
at javax.crypto.JarVerifier.access$400(JarVerifier.java:34)
at javax.crypto.JarVerifier$1.run(JarVerifier.java:183)
at javax.crypto.JarVerifier$1.run(JarVerifier.java:149)
at java.security.AccessController.doPrivileged(Native Method)
at javax.crypto.JarVerifier.<clinit>(JarVerifier.java:148)
at javax.crypto.JceSecurity.loadPolicies(JceSecurity.java:318)
at javax.crypto.JceSecurity.setupJurisdictionPolicies(JceSecurity.java:263)
at javax.crypto.JceSecurity.access$000(JceSecurity.java:48)
at javax.crypto.JceSecurity$1.run(JceSecurity.java:78)
at java.security.AccessController.doPrivileged(Native Method)
at javax.crypto.JceSecurity.<clinit>(JceSecurity.java:76)
at javax.crypto.Cipher.getInstance(Cipher.java:499)
at org.excel.encryptor.Encryptor.encryptFile(Encryptor.java:42)
两个类文件都保存在不同包中的不同JAR文件中。如果在Windows机器上运行,一切正常(Java 1.7_70),但在Solaris(Java 1.7_72 32)中失败。那么,这个例外可能是什么原因呢?为什么只在Solaris中发生?任何帮助,将不胜感激。提前谢谢。