Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/209.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
Android 使用密钥字符串加密另一个字符串中的字符串,编译问题_Android_String_Encryption - Fatal编程技术网

Android 使用密钥字符串加密另一个字符串中的字符串,编译问题

Android 使用密钥字符串加密另一个字符串中的字符串,编译问题,android,string,encryption,Android,String,Encryption,我需要两种简单的加密和解密方法,使用另一个字符串密钥作为加密和解密密钥,在加密的字符串b中加密字符串a 我发现这段代码似乎在做我需要的事情 public class DesEncrypter { Cipher ecipher; Cipher dcipher; DesEncrypter(SecretKey key) { try { ecipher = Cipher.getInstance("DES"); dc

我需要两种简单的加密和解密方法,使用另一个
字符串密钥
作为加密和解密密钥,在加密的
字符串b
中加密
字符串a

我发现这段代码似乎在做我需要的事情

public class DesEncrypter {
    Cipher ecipher;
    Cipher dcipher;

    DesEncrypter(SecretKey key) {
        try {
            ecipher = Cipher.getInstance("DES");
            dcipher = Cipher.getInstance("DES");
            ecipher.init(Cipher.ENCRYPT_MODE, key);
            dcipher.init(Cipher.DECRYPT_MODE, key);

        } catch (javax.crypto.NoSuchPaddingException e) {
        } catch (java.security.NoSuchAlgorithmException e) {
        } catch (java.security.InvalidKeyException e) {
        }
    }

    public String encrypt(String str) {
        try {
            // Encode the string into bytes using utf-8
            byte[] utf8 = str.getBytes("UTF8");

            // Encrypt
            byte[] enc = ecipher.doFinal(utf8);

            // Encode bytes to base64 to get a string
            return new sun.misc.BASE64Encoder().encode(enc);
        } catch (javax.crypto.BadPaddingException e) {
        } catch (IllegalBlockSizeException e) {
        } catch (UnsupportedEncodingException e) {
        } catch (java.io.IOException e) {
        }
        return null;
    }

    public String decrypt(String str) {
        try {
            // Decode base64 to get bytes
            byte[] dec = new sun.misc.BASE64Decoder().decodeBuffer(str);

            // Decrypt
            byte[] utf8 = dcipher.doFinal(dec);

            // Decode using utf-8
            return new String(utf8, "UTF8");
        } catch (javax.crypto.BadPaddingException e) {
        } catch (IllegalBlockSizeException e) {
        } catch (UnsupportedEncodingException e) {
        } catch (java.io.IOException e) {
        }
        return null;
    }
}
不幸的是,我们需要一个解决方案, 我犯了一个错误

sun.misc.BASE64Encoder()
而且

sun.misc.BASE64Decoder()
这要求我在sun.misc包中创建base64编码器和解码器


如何修复?

Android Java实现中不存在sun.misc.Base64编码器和
sun.misc.Base64解码器
。它们仅在Oracle的JDK中是内部的,尽管您确实有访问权限,但它们并不保证存在

您可以使用android.util.Base64类对android中的Base64进行编码和解码

要编码,请更改:

return new sun.misc.BASE64Encoder().encode(enc);
byte[] dec = new sun.misc.BASE64Decoder().decodeBuffer(str);
致:

要解码,请更改:

return new sun.misc.BASE64Encoder().encode(enc);
byte[] dec = new sun.misc.BASE64Decoder().decodeBuffer(str);
致: