Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/357.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java JAR文件在Linux上正常工作,在Windows上抛出异常_Java_Linux_Windows_Exception_Jar - Fatal编程技术网

Java JAR文件在Linux上正常工作,在Windows上抛出异常

Java JAR文件在Linux上正常工作,在Windows上抛出异常,java,linux,windows,exception,jar,Java,Linux,Windows,Exception,Jar,我已经用Java编写了一个基本的文本加密程序。(是的,我知道它使用ECB…) 我的程序在Linux上运行良好。我将它编译成一个JAR文件,在Linux上也可以正常工作。问题是,当我在Windows上运行文件时,它将抛出异常,同时使用在Ubuntu上工作的相同密钥对相同文本进行解密 我不知道从哪里开始调试,甚至不知道在谷歌上使用什么搜索词。我完全不知所措。我认为Java是跨平台的 import java.io.IOException; 导入java.security.InvalidKeyExcep

我已经用Java编写了一个基本的文本加密程序。(是的,我知道它使用ECB…)

我的程序在Linux上运行良好。我将它编译成一个
JAR
文件,在Linux上也可以正常工作。问题是,当我在Windows上运行文件时,它将抛出异常,同时使用在Ubuntu上工作的相同密钥对相同文本进行解密

我不知道从哪里开始调试,甚至不知道在谷歌上使用什么搜索词。我完全不知所措。我认为Java是跨平台的

import java.io.IOException;
导入java.security.InvalidKeyException;
导入java.security.Key;
导入java.security.NoSuchAlgorithmException;
导入java.util.Scanner;
导入javax.crypto.BadPaddingException;
导入javax.crypto.Cipher;
导入javax.crypto.IllegalBlockSizeException;
导入javax.crypto.NoSuchPaddingException;
导入javax.crypto.spec.SecretKeySpec;
导入sun.misc.base64编码器;
导入sun.misc.base64解码器;
公共类应用程序
{
公共静态void main(字符串[]args)
{
扫描仪输入=新扫描仪(System.in);
字符串textToEncrypt=“Hello World”;
字符串textToDecrypt;
字符串textToDecryptAscii;
字符串结果;
int操作;
密码=空;
试一试{
cipher=cipher.getInstance(“AES”);
}捕获(无算法异常e1){
//TODO自动生成的捕捉块
e1.printStackTrace();
}捕获(无填充异常e1){
//TODO自动生成的捕捉块
e1.printStackTrace();
}
//String key=“Bar12345Bar12345”//128位密钥
字符串键=null;
BASE64Encoder AscienceOrder=新的BASE64Encoder();
base64解码器asciiDecoder=新的base64解码器();
System.out.printf(“输入:\n1进行加密\n2进行解密\n\n选择:”;
操作=输入.nextInt();
input.nextLine();
如果(操作==1)
{
尝试
{
System.out.print(“输入用于加密的128位密钥:”;
key=input.nextLine();
如果(key.length()!=16)
{
while(key.length()!=16)
{
System.out.print(“您需要输入一个*128位*键:”);
key=input.nextLine();
}
}
System.out.printf(“\n-------------\n\n要加密的文本:”;
textToEncrypt=input.nextLine();
//创建密钥和密码
Key aesKey=新的SecretKeySpec(Key.getBytes(),“AES”);
//Cipher Cipher=Cipher.getInstance(“AES”);
//加密文本
cipher.init(cipher.ENCRYPT_模式,aesKey);
byte[]encrypted=cipher.doFinal(textToEncrypt.getBytes());
StringBuilder sb=新的StringBuilder();
for(字节b:加密)
{
sb.追加((char)b);
}
//加密字符串
字符串enc=sb.toString();
//System.out.println(“加密:+enc”);
字符串ascienceodedencryptedresult=ascienceoder.encodeBuffer(enc.getBytes());
ascienceodedencryptedresult=ascienceodedencryptedresult.replace(“\n”和“).replace(“\r”和“);
System.out.println(“加密文本:+ascienceodedencryptedresult”);
//System.out.printf(“\n--------------------------------------\n加密文本:“+AscienceNodeEncryptedResult+”\n-------------------\n\n”);
}
捕获(例外e)
{
e、 printStackTrace();
}
}
else if(操作==2)
{
System.out.printf(“\n------\n\n要解密的文本:”);
textToDecryptAscii=input.nextLine();
System.out.print(“输入128位解密密钥:”);
key=input.nextLine();
如果(key.length()!=16)
{
while(key.length()!=16)
{
System.out.print(“您需要输入一个*128位*键:”);
key=input.nextLine();
}
}
字节[]decodedBytes=null;
尝试
{
decodedBytes=asciiDecoder.decodeBuffer(texttodecryptscii);
}捕获(IOE1异常){
//TODO自动生成的捕捉块
e1.printStackTrace();
}
//System.out.println(“decodedBytes”+新字符串(decodedBytes));
textToDecrypt=新字符串(解码字节);
//将字符串转换为字节数组
//用于解密
byte[]bb=新字节[textToDecrypt.length()];

对于(int i=0;i我在进行加密之前已经看到了这一点,因为Linux/Unix和Windows之间的CRLF不同。这两个操作系统对回车换行和换行的看法完全不同

您可能需要使用Ant和fixcrlf步骤将代码编译成jar文件:

    <fixcrlf 
        srcdir="${dir.prj.work}"
        eol="dos"
        includes="**/*"
    />

底线是,BASE64编码器/解码器中有“某种东西”导致解密失败…它以某种方式更改了按位字符,而我在使用System.out.println()时看不到这种方式…也许十六进制输出可能会显示什么…

您会遇到什么异常?一个可能的问题是将字符串转换为字节[].String.getBytes()
将在系统属性中使用
file.encoding
,此值在不同的环境中可能不同。@beckyang-
javax.crypto.BadPaddingException:给定的最后一个块填充不正确
不要将原始字节附加到字符串中。这会给您带来各种编码问题。使用
=Ascienceo
    <fixcrlf 
        srcdir="${dir.prj.work}"
        eol="dos"
        includes="**/*"
    />
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import java.util.Scanner;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;

import sun.misc.BASE64Encoder;
import sun.misc.BASE64Decoder;

public class Application { 

    public Application() {
        // TODO Auto-generated constructor stub
    }

    public static void main ( String[] args ) {
        Scanner input = new Scanner(System.in);
        String textToEncrypt = "Hello World";
        String textToDecrypt;
        String textToDecryptAscii;
        String result;
        int operation;
        Cipher cipher = null;
        try {
            cipher = Cipher.getInstance("AES");
        } catch (NoSuchAlgorithmException e1) {
            e1.printStackTrace();
        } catch (NoSuchPaddingException e1) {
            e1.printStackTrace();
        }

        //String key = "Bar12345Bar12345"; // 128 bit key
        String key = null;
        //byte[] key = null;

        //BASE64Encoder asciiEncoder = new BASE64Encoder();
        //BASE64Decoder asciiDecoder = new BASE64Decoder();

        //System.out.printf("Enter:\n1 for encryption\n2 for decryption\n\nChoice: ");
        //operation = input.nextInt();
        //input.nextLine();

        try { 
            System.out.print("Enter a 128-bit key to be used for encryption: ");
            key = input.nextLine();

            if ( key.length() != 16 ) {
                while ( key.length() != 16 ) {
                    System.out.print("You need to enter a *128-bit* key: ");
                    key = input.nextLine();
                }
            }
            System.out.println ( "128-bit encryption key.......................["+key+"] length ["+key.length ()+"]");

            System.out.printf ( "Text to encrypt..............................[");
            //System.out.printf("\n---------\n\nText to encrypt: ");
            textToEncrypt = input.nextLine();
            System.out.println ( "Text to encrypt..............................["+textToEncrypt+"] length ["+textToEncrypt.length ()+"]");

            //Create key and cipher
            Key aesKey = new SecretKeySpec(key.trim().getBytes(), "AES");
            //Cipher cipher = Cipher.getInstance("AES");

            //encrypt the text
            cipher.init(Cipher.ENCRYPT_MODE, aesKey);
            byte[] encrypted = cipher.doFinal(textToEncrypt.getBytes ());

            StringBuilder sb = new StringBuilder();
            for (byte b: encrypted) {
                sb.append((char)b);
            }

            // the encrypted String
            String enc = sb.toString();
            System.out.println ( "Encrypted text...............................["+enc+"] length ["+enc.length ()+"]");
            //System.out.println("encrypted:" + enc);

            //String asciiEncodedEncryptedResult = asciiEncoder.encodeBuffer(enc.getBytes()).trim ();
            String asciiEncodedEncryptedResult = enc.trim ();
            System.out.println ( "Encoded text.................................["+asciiEncodedEncryptedResult+"] length ["+asciiEncodedEncryptedResult.length ()+"]");

            //asciiEncodedEncryptedResult = asciiEncodedEncryptedResult.replace("\n", "").replace("\r", "");
            asciiEncodedEncryptedResult = asciiEncodedEncryptedResult.trim ();

            System.out.println ( "Encrypted text...............................["+asciiEncodedEncryptedResult+"] length ["+asciiEncodedEncryptedResult.length ()+"]");


            //byte[] decodedBytes = null;
            //try {
            //    decodedBytes = asciiDecoder.decodeBuffer(asciiEncodedEncryptedResult);
            //} 
            //catch (IOException e1) {
            //    e1.printStackTrace();
            //}
            //System.out.println ( "Decoded Bytes................................["+decodedBytes+"] length ["+decodedBytes.length+"]");

            //textToDecrypt = new String(decodedBytes);
            textToDecrypt = asciiEncodedEncryptedResult;

            System.out.println ( "Text to Decrypt..............................["+textToDecrypt+"] length ["+textToDecrypt.length()+"]");

            //Convert the string to byte array
            //for decryption
            byte[] bb = new byte[textToDecrypt.length()];
            for ( int i=0; i<textToDecrypt.length(); i++ ) {
                bb[i] = (byte) textToDecrypt.charAt(i);
            }

            //decrypt the text
            //Key aesKey = null;
            String decrypted = null;
            try {
                //aesKey = new SecretKeySpec(key.trim ().getBytes (), "AES");
                cipher.init(Cipher.DECRYPT_MODE, aesKey);
                decrypted = new String(cipher.doFinal(bb));
            }
            catch (InvalidKeyException e) {
                e.printStackTrace();
            }
            catch (IllegalBlockSizeException e) {
                e.printStackTrace();
            }
            catch (BadPaddingException e) {
                e.printStackTrace();
            }
            catch ( Exception ltheXcp ) { 
                ltheXcp.printStackTrace ();
            }

            if ( decrypted != null ) {
                System.out.println ( "Decrypted text...............................["+decrypted+"] length ["+decrypted.length ()+"]");
            }
            else { 
                System.out.println ( "Decrypted text...............................["+decrypted+"] length []");
            }

        }
        catch ( Exception ltheXcp ) { 
            ltheXcp.printStackTrace ();
        }

    }
}
Enter a 128-bit key to be used for encryption: aaaaaaaaaaaaaaaa
128-bit encryption key.......................[aaaaaaaaaaaaaaaa] length [16]
Text to encrypt..............................[adymlincoln
Text to encrypt..............................[adymlincoln] length [11]
Encrypted text...............................[\_i8`???R????] length [16]
Encoded text.................................[\_i8`???R????] length [16]
Encrypted text...............................[\_i8`???R????] length [16]
Text to Decrypt..............................[\_i8`???R????] length [16]
Decrypted text...............................[adymlincoln] length [11]