Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/232.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/1/list/4.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 Studio中的高级加密标准_Java_Android_Android Studio_Aes - Fatal编程技术网

Java Android Studio中的高级加密标准

Java Android Studio中的高级加密标准,java,android,android-studio,aes,Java,Android,Android Studio,Aes,这是我将普通字符串转换为AES加密字符串的代码: public class AES2 { private static AES2 aes = new AES2( ); private AES2() { } public static AES2 getInstance( ) { return aes; } private static SecretKeySpec secretKey ; private static byte

这是我将普通字符串转换为AES加密字符串的代码:

public class AES2

{

    private static AES2 aes = new AES2( );

    private AES2() { }

    public static AES2 getInstance( ) {
        return aes;
    }

    private static SecretKeySpec secretKey ;
    private static byte[] key ;

    private static String decryptedString;
    private static String encryptedString;

    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) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }



    }

    public static String encrypt(String strToEncrypt)
    {
        try
        {
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
            cipher.init(Cipher.ENCRYPT_MODE, secretKey);
            encryptedString =     (Base64.encodeBase64String(cipher.doFinal(strToEncrypt.getBytes("UTF-8"))));
        }
        catch (Exception e)
        {

            System.out.println("Error while encrypting: "+e.toString());
        }
        return null;
    }
    public static String decrypt(String strToDecrypt)
    {
        try
        {
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5PADDING");

            cipher.init(Cipher.DECRYPT_MODE, secretKey);
            byte[] bytes = cipher.doFinal(Base64.decodeBase64(strToDecrypt));
            decryptedString = bytes.toString();

        }
        catch (Exception e)
        {

            System.out.println("Error while decrypting: "+e.toString());
        }
        return null;
    }
}
我已将commons-codec-1.10.jar包含在/libs文件夹中,并将其添加为库

我在编译时遇到编译错误:

1) 错误:找不到的符号方法encodeBase64String(字节[])

2) 错误:不兼容的类型:无法将字符串转换为字节[],例如

encryptedString =     (Base64.encodeBase64String(cipher.doFinal(strToEncrypt.getBytes("UTF-8"))));
byte[] bytes = cipher.doFinal(Base64.decodeBase64(strToDecrypt));
我已经检查过,函数调用完全匹配,但我不知道为什么会出现这些错误

我做错了什么


PS:代码来自:

检查您的导入类是否有base64,或者使用导入更新您的问题代码。在PHP服务器上,我使用的是org.apache.commons.codec.binary.base64,所以这不会引起问题吗?可能会解决。尝试将其更改为android.util.Base64并执行,这样会更快地了解它。处理它!!没用!!!检查导入类中的base64,或者使用导入更新问题代码。在PHP服务器上,我使用的是org.apache.commons.codec.binary.base64,所以这不会引起问题吗?可能会解决。尝试将其更改为android.util.Base64并执行,这样会更快地了解它。处理它!!没用!!!