Android - decodeBase64 crashes App

后端 未结 3 1153
后悔当初
后悔当初 2021-01-22 22:51

I have to encrypt a String but the app doesn\'t reach the encrypt method, it crashes on load.
I\'m using Apache Commons Codec library.

private EditText txt         


        
相关标签:
3条回答
  • 2021-01-22 23:25

    Simply create an object of Base64 and use it to encode or decode in Android, when using org.apache.commons.codec.binary.Base64 library

    To Encode

    Base64 ed=new Base64();

    String encoded=new String(ed.encode("Hello".getBytes()));

    Replace "Hello" with the text to be encoded in String Format.

    To Decode

    Base64 ed=new Base64();

    String decoded=new String(ed.decode(encoded.getBytes()));

    Here encoded is the String variable to be decoded

    0 讨论(0)
  • 2021-01-22 23:27

    Try this:

    // decode data from base 64
        private static byte[] decodeBase64(String dataToDecode)
            {
                byte[] dataDecoded = Base64.decode(dataToDecode, Base64.DEFAULT);
                return dataDecoded;
            }
    
    //enconde data in base 64
            private static byte[] encodeBase64(byte[] dataToEncode)
            {
                byte[] dataEncoded = Base64.encode(dataToEncode, Base64.DEFAULT);
                return dataEncoded;
            }
    
    0 讨论(0)
  • 2021-01-22 23:38

    you are giving a string as an input you should give key.getBytes() to the decode methos

    0 讨论(0)
提交回复
热议问题