Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Encode and decode string without forward or backward slash

Tags:

java

base64

aes

I have the code that encode and decode string.

When I input "9" encrypt method return "9iCOC73F/683bf5WRJDnKQ=="

The problem is that when I encode String sometimes it will return encoded string with (/ or \ ) and I want to remove that (/ or \ ) from String.

So how can i achieve with my encrypt and 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 am using Java.

like image 256
user3441151 Avatar asked Jul 28 '16 10:07

user3441151


People also ask

Does Base64 have forward slashes?

Base64 encoding takes three bytes of binary data and encodes it into four bytes of printable ASCII, which includes the letters A through Z (both upper- and lowercase), the numbers 0 through 9, and the plus sign and forward slash characters.

How do you decode a string in Java?

decode(encodedString); String actualString= new String(actualByte); Explanation: In above code we called Base64. Decoder using getDecoder() and then decoded the string passed in decode() method as parameter then convert return value to string.

What is Base64 decode Java?

It decodes a Base64 encoded String into a newly-allocated byte array using the Base64 encoding scheme. public int decode(byte[] src, byte[] dst) It decodes all bytes from the input byte array using the Base64 encoding scheme, writing the results into the given output byte array, starting at offset 0.

What is encoder and decoder in Java?

An encoder takes a Java object and produces a representation that can be transmitted as a WebSocket message; for example, encoders typically produce JSON, XML, or binary representations. A decoder performs the reverse function; it reads a WebSocket message and creates a Java object.


1 Answers

Use Base64 "URL-safe" encoding as described in IETF RFC 4648 Section 5. This replaces the + and / characters with - and _ respectively. Instantiate those encoder/decoders as follows:

java.util.Base64.Encoder encoder = java.util.Base64.getUrlEncoder();
java.util.Base64.Decoder decoder = java.util.Base64.getUrlDecoder();
like image 105
Andy Avatar answered Oct 13 '22 11:10

Andy