Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/398.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 android中的AES加密和php中的解密,反之亦然_Java_Php_Android_Android Studio_Aes - Fatal编程技术网

Java android中的AES加密和php中的解密,反之亦然

Java android中的AES加密和php中的解密,反之亦然,java,php,android,android-studio,aes,Java,Php,Android,Android Studio,Aes,我试图通过测试我的代码来学习AES。我以前在Base64.encodeBase64String和Base64.decodeBase64//encode/decodeBase64中出错。所以我以某种方式操纵Base64来解决这个错误。现在在我的应用程序中,文本被正确地加密和解密,我想。但是当我尝试在服务器端加密或解密同一文本时(在aesencryption.net),该站点无法解密我加密的字符串。请帮忙 以下是我的代码: public class MainActivity extends AppC

我试图通过测试我的代码来学习AES。我以前在
Base64.encodeBase64String
Base64.decodeBase64//encode/decodeBase64
中出错。所以我以某种方式操纵Base64来解决这个错误。现在在我的应用程序中,文本被正确地加密和解密,我想。但是当我尝试在服务器端加密或解密同一文本时(在aesencryption.net),该站点无法解密我加密的字符串。请帮忙

以下是我的代码:

public class MainActivity extends AppCompatActivity {

    static final String TAG = "SymmetricAlgorithmAES";
    private static SecretKeySpec secretKey ;
    private static byte[] key ;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // Original text
        // {"type":"Success","httpCode":"200","code":"200","message":{"pin":"11111"},"extra":""}
        String theTestText = "hi";
        TextView tvorig = (TextView)findViewById(R.id.tvorig);
        tvorig.setText("\n[ORIGINAL]:\n" + theTestText + "\n");
        final String strPssword = "android";
        setKey(strPssword);

        // Encode the original data with AES
        byte[] encodedBytes = null;
        try {
            Cipher c = Cipher.getInstance("AES");
            c.init(Cipher.ENCRYPT_MODE,secretKey);
            encodedBytes = c.doFinal(theTestText.getBytes());
        } catch (Exception e) {
            Log.e(TAG, "AES encryption error");
        }

        TextView tvencoded = (TextView)findViewById(R.id.tvencoded);
        tvencoded.setText("[ENCODED]:\n" +
                Base64.encodeToString(encodedBytes, Base64.DEFAULT) + "\n");

        Log.d(TAG, Base64.encodeToString(encodedBytes, Base64.DEFAULT));


        // Decode the encoded data with AES
        byte[] decodedBytes = null;
        try {
            Cipher c = Cipher.getInstance("AES");
            c.init(Cipher.DECRYPT_MODE, secretKey);
            decodedBytes = c.doFinal(encodedBytes);
        } catch (Exception e) {
            Log.e(TAG, "AES decryption error");
        }
        TextView tvdecoded = (TextView)findViewById(R.id.tvdecoded);
        tvdecoded.setText("[DECODED]:\n" + new String(decodedBytes) + "\n");
    }



    public static void setKey(String myKey){
        MessageDigest sha = null;
        try {
            key = myKey.getBytes("UTF-8");
            System.out.println(key.length);
            sha = MessageDigest.getInstance("SHA-1");
            key = sha.digest(key);
            key = Arrays.copyOf(key, 16); // use only first 128 bit
            System.out.println(key.length);
            System.out.println(new String(key,"UTF-8"));
            secretKey = new SecretKeySpec(key, "AES");


        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

    }

}

提前感谢。

PHP代码默认使用CBC模式,而Java代码没有指定相同的模式,并将其留给底层实现。如果我没记错的话,它可能是ECB/PKCS5Padding。您的PHP实现使用的是没有填充的“CBC”(默认情况下,mcrypt不支持填充,但您可以手动执行)

在使用不同的平台时,非常重要的一点是,必须非常具体地说明模式、填充和字符集,否则您将遇到不同的默认设置


尝试按如下方式初始化密码:
cipher.getInstance(“AES/CBC/NoPadding”)

我做了一些像这样的事情,实际上很有效;)

这样称呼这个类:

 try {
            AESCrypter _crypt = new AESCrypter("password");
            String output = "";
            String plainText = "top secret message";
            output = _crypt.encrypt(plainText); //encrypt
            System.out.println("encrypted text=" + output);
            output = _crypt.decrypt(output); //decrypt
            System.out.println("decrypted text=" + output);
        } catch (Exception e) {
            e.printStackTrace();
        }
对于iPhone(代码在这里):https://github.com/Gurpartap/AESCrypt-ObjC


希望这段代码也适用于您:)

AES有不同的加密模式,包括“盐渍”加密模式。在应用程序中克隆网站之前,您必须知道该网站使用的是什么模式。我正在尝试从中解密相同的代码。我也无法解密或加密。AES是一种可以使用的分组密码。因此,如果您的网站(我假设是PHP)使用一种操作模式,而您的应用程序使用另一种操作模式,您将得到不同的结果。除此之外,AES结果取决于您使用的内容。请确保两端的代码都是一样的。@pyromank谢谢您的时间。这个案例实际上在我的脑海中。但问题是,aes网站本身提供了java代码,并提供了一种在线检查相同代码的方法。在我的代码中,我也有一些被操纵的小东西,我不确定这些东西是否会在网站上生效,因为我没有改变模式。因此,在应用程序中,我可以轻松地加密或解密字符串。但当我获取加密字符串并在aes网站上运行时,我无法对其进行解密。请确保以下条件成立并运行另一个测试:1)您加密的字符串仅包含ANSI字符(A-Z、A-Z、-、0-9等);2) “加密密钥”在两种情况下均为空;3) 在这两种情况下,块大小都是256位。谢谢您的时间。我操作了代码,现在它可以正常工作了。:)
 try {
            AESCrypter _crypt = new AESCrypter("password");
            String output = "";
            String plainText = "top secret message";
            output = _crypt.encrypt(plainText); //encrypt
            System.out.println("encrypted text=" + output);
            output = _crypt.decrypt(output); //decrypt
            System.out.println("decrypted text=" + output);
        } catch (Exception e) {
            e.printStackTrace();
        }