base64 - Java Encode and decode string without forward or backward slash -
i have code encode , decode string.
when input "9" encrypt method return "9icoc73f/683bf5wrjdnkq=="
the problem when encode string return encoded string (/ or \ ) , want remove (/ or \ ) string.
so how can achieve encrypt , decrypt both method.
import java.security.key; import javax.crypto.cipher; import javax.crypto.spec.secretkeyspec; import sun.misc.base64decoder; import sun.misc.base64encoder; public class encryptdecryptaesalgo { private static final string algo = "aes"; private static final byte[] keyvalue = new byte[] { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p' }; public string encrypt(string data) throws exception { string encryptedvalue = ""; try { key key = generatekey(); cipher c = cipher.getinstance(algo); c.init(cipher.encrypt_mode, key); byte[] encval = c.dofinal(data.getbytes()); encryptedvalue = new base64encoder().encode(encval); return encryptedvalue; } catch (exception e) { } return encryptedvalue; } public string decrypt(string encrypteddata) throws exception { string decryptedvalue = ""; try { key key = generatekey(); cipher c = cipher.getinstance(algo); c.init(cipher.decrypt_mode, key); byte[] decordedvalue = new base64decoder().decodebuffer(encrypteddata); byte[] decvalue = c.dofinal(decordedvalue); decryptedvalue = new string(decvalue); return decryptedvalue; } catch (exception e) { } return decryptedvalue; } private key generatekey() throws exception { key key = new secretkeyspec(keyvalue, algo); return key; } }
i using java.
use base64 "url-safe" encoding described in ietf rfc 4648 section 5. replaces +
, /
characters -
, _
respectively. instantiate encoder/decoders follows:
java.util.base64.encoder encoder = java.util.base64.geturlencoder(); java.util.base64.decoder decoder = java.util.base64.geturldecoder();
Comments
Post a Comment