Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/318.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
如何对消息进行加密,然后在另一台设备上使用AES使用javax.crypto.cipher对其进行解密_Java_Encryption - Fatal编程技术网

如何对消息进行加密,然后在另一台设备上使用AES使用javax.crypto.cipher对其进行解密

如何对消息进行加密,然后在另一台设备上使用AES使用javax.crypto.cipher对其进行解密,java,encryption,Java,Encryption,我的代码有问题。我想使用由密码短语生成的密钥加密消息。然后我想以字符串形式(加密的形式)发送它(或在某处使用它,无论什么),然后我只想通过知道密码来解密它 我写了下面的代码,但是我收到了IllegalBlockSizeException或BadPaddingException。我原以为巴丁会用填充物来处理的 这是我的密码: 构造函数和初始化: public class AES_Cipher { private String keyString; private byte[] b

我的代码有问题。我想使用由密码短语生成的密钥加密消息。然后我想以字符串形式(加密的形式)发送它(或在某处使用它,无论什么),然后我只想通过知道密码来解密它

我写了下面的代码,但是我收到了
IllegalBlockSizeException
BadPaddingException
。我原以为巴丁会用填充物来处理的

这是我的密码:

构造函数和初始化:

public class AES_Cipher {

    private String keyString;
    private byte[] byteKey;
    private SecretKey key;
    Cipher c;

    public AES_Cipher(String keyString){
        this.keyString = keyString.toString();
    }

    public void init() throws InitializtionFailedException{
        try{
            c = Cipher.getInstance("AES/ECB/PKCS5Padding");
            byteKey = keyString.getBytes("UTF-8");
            MessageDigest sha = MessageDigest.getInstance("SHA-1");
            byteKey = sha.digest(byteKey);
            byteKey = Arrays.copyOf(byteKey, 16);
            key = new SecretKeySpec(byteKey, "AES");

        }catch(NoSuchAlgorithmException e){
            throw new InitializtionFailedException();
        }
}
public String encrypt(String text) throws EncryptionException{
    try{
            c.init(Cipher.ENCRYPT_MODE, key);
            byte[] tmp = c.doFinal( text.getBytes("UTF-8")); 

            return new String(tmp, "UTF-8");
        }catch(IllegalBlockSizeException e){
            throw new EncryptionException();
        }
}
public String decrypt(String text) throws DecryptionException{
        try{

        //byte[] decordedValue = new BASE64Decoder().decodeBuffer(text);
        // tried too use it but with poor outcome
        //c = Cipher.getInstance("AES/ECB/PKCS5Padding");
        c.init(Cipher.DECRYPT_MODE, key);
        byte[] tmp1 = text.getBytes("UTF-8");
        //byte[] tmp = c.doFinal("aaaaaaaaaaaaaaaa".getBytes());
        //this returns BadPaddingException
        byte[] tmp = c.doFinal(text.getBytes());
        return new String(tmp, "UTF-8");

    }catch(IllegalBlockSizeException e){
    throw new DecryptionException();
    }
}
加密:

public class AES_Cipher {

    private String keyString;
    private byte[] byteKey;
    private SecretKey key;
    Cipher c;

    public AES_Cipher(String keyString){
        this.keyString = keyString.toString();
    }

    public void init() throws InitializtionFailedException{
        try{
            c = Cipher.getInstance("AES/ECB/PKCS5Padding");
            byteKey = keyString.getBytes("UTF-8");
            MessageDigest sha = MessageDigest.getInstance("SHA-1");
            byteKey = sha.digest(byteKey);
            byteKey = Arrays.copyOf(byteKey, 16);
            key = new SecretKeySpec(byteKey, "AES");

        }catch(NoSuchAlgorithmException e){
            throw new InitializtionFailedException();
        }
}
public String encrypt(String text) throws EncryptionException{
    try{
            c.init(Cipher.ENCRYPT_MODE, key);
            byte[] tmp = c.doFinal( text.getBytes("UTF-8")); 

            return new String(tmp, "UTF-8");
        }catch(IllegalBlockSizeException e){
            throw new EncryptionException();
        }
}
public String decrypt(String text) throws DecryptionException{
        try{

        //byte[] decordedValue = new BASE64Decoder().decodeBuffer(text);
        // tried too use it but with poor outcome
        //c = Cipher.getInstance("AES/ECB/PKCS5Padding");
        c.init(Cipher.DECRYPT_MODE, key);
        byte[] tmp1 = text.getBytes("UTF-8");
        //byte[] tmp = c.doFinal("aaaaaaaaaaaaaaaa".getBytes());
        //this returns BadPaddingException
        byte[] tmp = c.doFinal(text.getBytes());
        return new String(tmp, "UTF-8");

    }catch(IllegalBlockSizeException e){
    throw new DecryptionException();
    }
}
解密:

public class AES_Cipher {

    private String keyString;
    private byte[] byteKey;
    private SecretKey key;
    Cipher c;

    public AES_Cipher(String keyString){
        this.keyString = keyString.toString();
    }

    public void init() throws InitializtionFailedException{
        try{
            c = Cipher.getInstance("AES/ECB/PKCS5Padding");
            byteKey = keyString.getBytes("UTF-8");
            MessageDigest sha = MessageDigest.getInstance("SHA-1");
            byteKey = sha.digest(byteKey);
            byteKey = Arrays.copyOf(byteKey, 16);
            key = new SecretKeySpec(byteKey, "AES");

        }catch(NoSuchAlgorithmException e){
            throw new InitializtionFailedException();
        }
}
public String encrypt(String text) throws EncryptionException{
    try{
            c.init(Cipher.ENCRYPT_MODE, key);
            byte[] tmp = c.doFinal( text.getBytes("UTF-8")); 

            return new String(tmp, "UTF-8");
        }catch(IllegalBlockSizeException e){
            throw new EncryptionException();
        }
}
public String decrypt(String text) throws DecryptionException{
        try{

        //byte[] decordedValue = new BASE64Decoder().decodeBuffer(text);
        // tried too use it but with poor outcome
        //c = Cipher.getInstance("AES/ECB/PKCS5Padding");
        c.init(Cipher.DECRYPT_MODE, key);
        byte[] tmp1 = text.getBytes("UTF-8");
        //byte[] tmp = c.doFinal("aaaaaaaaaaaaaaaa".getBytes());
        //this returns BadPaddingException
        byte[] tmp = c.doFinal(text.getBytes());
        return new String(tmp, "UTF-8");

    }catch(IllegalBlockSizeException e){
    throw new DecryptionException();
    }
}
当然还有一些例外

另外,我想稍后在android上使用这段代码,如果有其他一些密码可能效率较低但麻烦较少,那么它不需要是
AES
。但不是简单的异或,如果不是太多的话


提前谢谢。

好的。多亏了Artjom B

建造商和非专业化: 公开类AES_密码{

private String keyString;
private byte[] byteKey;
private SecretKey key;
Cipher c;

public AES_Cipher(String keyString){
    this.keyString = keyString.toString();
}

public void init() throws InitializtionFailedException{
    try{
        c = Cipher.getInstance("AES/ECB/PKCS5Padding");
        byteKey = keyString.getBytes("UTF-8");
        MessageDigest sha = MessageDigest.getInstance("SHA-1");
        byteKey = sha.digest(byteKey);
        byteKey = Arrays.copyOf(byteKey, 16);
        key = new SecretKeySpec(byteKey, "AES");

    }catch(NoSuchAlgorithmException e){
        throw new InitializtionFailedException();
    }
 }
加密:

 public String encrypt(String text) throws EncryptionException{
    try{
        c.init(Cipher.ENCRYPT_MODE, key);
        byte[] textByte = text.getBytes("UTF-8"); 
        byte[] tmp = c.doFinal(textByte);

        String return1 = Base64.encodeToString(tmp, Base64.DEFAULT);


        return return1;
}   // of course there is some exceptions catched down there
解密:

public String decrypt(String text) throws DecryptionException{
    try{

        c.init(Cipher.DECRYPT_MODE, key);
        byte[] textByte = Base64.decode(text, Base64.DEFAULT);

        byte[] tmp = c.doFinal(textByte);

        return new String(tmp, "UTF-8");

    }catch(IllegalBlockSizeException e){
        Log.d("Exception", "IllegalBlockSizeException");
        throw new DecryptionException();

    }  // wrote DecryptionException myself. 
       // also there is more Exceptions below
}

这与AES无关。每种现代密码都会出现这种情况。问题是
新字符串(tmp,“UTF-8”)
可能重复的Never use。它是确定性的,因此在语义上不安全。您至少应该使用随机模式,如或。最好对密文进行身份验证,这样就不可能进行像a这样的攻击。这可以通过身份验证模式(如GCM或EAX)或方案来完成。@ArtjomB。您可以explain?如果不是这样,我应该如何解析字符串?我想如果我一直使用相同的字符集,它应该可以工作。好的。所以我尝试使用BASE64编码器和BASE64解码器来处理它。感谢您指出错误:)。现在我试着在android上使用它,但我不能,因为没有BASE64编码器或解码器。它只有BASE64,但需要res byte[]已经存在,因此它迫使我使用字符串(tmp,“UTF-8”)将加密字节数组转换为字符串,以便我可以进一步发送它。而且我在logcat中没有看到任何异常,因此我有点盲目。我所知道的是它在doFinal上崩溃。有什么想法可以帮助我吗?