Java Encrypt String encryptString(String message)

Here you can find the source of encryptString(String message)

Description

encrypt String

License

Apache License

Declaration

public static String encryptString(String message) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import com.google.common.io.BaseEncoding;

import java.io.UnsupportedEncodingException;

public class Main {
    private static final String DEFAULT_ENCODING = "UTF-8";
    private static BaseEncoding baseEncoding = BaseEncoding.base64();
    private static String ENCRYPT_KEY = "FRAME_ENCRYPT_KEY";

    public static String encryptString(String message) {
        return base64encode(xorMessage(message, ENCRYPT_KEY));
    }/*from  w  ww.  j  av  a  2s. c om*/

    public static String base64encode(String text) {
        try {
            return baseEncoding.encode(text.getBytes(DEFAULT_ENCODING));
        } catch (UnsupportedEncodingException e) {
            return null;
        }
    }

    private static String xorMessage(String message, String key) {
        try {
            if (message == null || key == null)
                return null;

            char[] keys = key.toCharArray();
            char[] mesg = message.toCharArray();

            int ml = mesg.length;
            int kl = keys.length;
            char[] newmsg = new char[ml];

            for (int i = 0; i < ml; i++) {
                newmsg[i] = (char) (mesg[i] ^ keys[i % kl]);
            }

            return new String(newmsg);
        } catch (Exception e) {
            return null;
        }
    }
}

Related

  1. encrypt(String s)
  2. encrypt(String str)
  3. encrypt(String value)