Search code examples
javafernet

Get encoded String using Fernet


So I decided to encode a String using Fernet

The code is pretty straight forward:

private static final Key key = new Key("myrandomkey");

public static Token encrypt(String message){
    return Token.generate(key, message);
}

How would I go about obtaining the encoded String? Since the token provides timestamp and iv, but not the encoded String

I tried getting the cipherText of the token, but that just gives me a weird String (Ex: "[B@6f7122f9"), when the encrypted String should be something like: 78APXA4zMBNX1REjh21AXzAx1YXor4ozq8RxABCZ4uo= Also, cipherText is private/protected

PS: I want to send the encodedstring over requests (Spring Framework)


Solution

  • After a bit of testing, the solution was to .serialise()

    That method will return the encoded String

    So:

    public static String encrypt(String message){
        return Token.generate(key, message).serialise();
    }