Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/384.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/217.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 Can';不能让AES密码在Android上工作_Java_Android_Encryption_Aes - Fatal编程技术网

Java Can';不能让AES密码在Android上工作

Java Can';不能让AES密码在Android上工作,java,android,encryption,aes,Java,Android,Encryption,Aes,我无法在Android上通过基本的AES加密/解密往返单元测试。可能是我正在做的事情,但非常感谢您的指导: public class EncryptionManager { private static final byte[] keyBytes = new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11,

我无法在Android上通过基本的AES加密/解密往返单元测试。可能是我正在做的事情,但非常感谢您的指导:

public class EncryptionManager {
    private static final byte[] keyBytes = new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17 };
    private static final byte[] ivBytes = new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f };

    public byte[] encrypt(byte[] plaintext) throws Exception {
        Cipher cipher = getCipher(true);
        return cipher.doFinal(plaintext);
    }

    public byte[] decrypt(byte [] ciphertext) throws Exception {
        Cipher cipher = getCipher(false);
        return cipher.doFinal(ciphertext);
    }

    private static Cipher getCipher(boolean encrypt) throws Exception {
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        SecretKeySpec secretKeySpec = new SecretKeySpec(keyBytes, "AES");
        IvParameterSpec ivParameterSpec = new IvParameterSpec(ivBytes);
        cipher.init(encrypt ? Cipher.ENCRYPT_MODE : Cipher.DECRYPT_MODE,  
                    secretKeySpec, ivParameterSpec);
        return cipher;
    }
}
失败的单元测试如下所示:

public class EncryptionManagerTest extends TestCase {
     public void testShouldPbeRoundtrip() throws Exception {
         String unencryptedStr = "foobargum";
         byte[] unencrypted = unencryptedStr.getBytes();
         EncryptionManager encryptionManager = new EncryptionManager();
         byte[] encrypted = encryptionManager.encrypt(unencrypted);
         String encryptedStr = new String(encrypted);
         byte[] decrypted = encryptionManager.decrypt(encrypted);
         String decryptedStr = new String(encrypted);

         // assert
         assertFalse("encryption not done",      
                unencryptedStr.equalsIgnoreCase(encryptedStr));
         assertEquals("decryption didn't work", unencryptedStr, 
                decryptedStr);
     }
 }

错误:“解密未按预期工作:但是:”

我认为您有一个复制粘贴错误:

String decryptedStr = new String(encrypted);

我认为您有一个复制粘贴错误:

String decryptedStr = new String(encrypted);

String encryptedStr=新字符串(加密)是一个错误。加密生成的字节序列对于给定的字符集可能不是有效的字符串,特别是对于android默认的UTF-8字符集。将字节数组
加密
转换为字符串对象时,无效的字节序列将以不可恢复的方式以有效字符悄悄替换。

字符串加密str=新字符串(加密)是一个错误。加密生成的字节序列对于给定的字符集可能不是有效的字符串,特别是对于android默认的UTF-8字符集。在将字节数组
加密
转换为字符串对象时,无效的字节序列会以不可恢复的方式被有效字符悄悄地替换。

我不敢相信我这么做了,我已经在这方面做了30分钟了:-/我猜这是一部非常糟糕的影片。唉,我真不敢相信我居然这么做了,我已经在这部电影上演了30分钟了:-/我猜是为那部如此粗俗的影片拍的。这只是为了在单元测试中进行类似的比较。但要注意的是,这只是为了在单元测试中逐个比较。但我注意到了这一点,谢谢