Java String Encrypt encryptIfNotEncrypted(String s)

Here you can find the source of encryptIfNotEncrypted(String s)

Description

encrypt If Not Encrypted

License

Apache License

Declaration

public static String encryptIfNotEncrypted(String s) 

Method Source Code

//package com.java2s;
/**// w  w w  .  j  a va 2 s .c o  m
 * Copyright ? 2013 - 2017 WaveMaker, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

public class Main {
    private static final byte[] KEY = { 12, 7, 28, 127, 97, 69, 77, 122, 11 };
    private static final String ENCRYPTED_PREFIX = "wm_-+";
    private static final String ENCRYPTED_SUFFIX = "==wm-_";

    public static String encryptIfNotEncrypted(String s) {
        return isEncrypted(s) ? s : encrypt(s);
    }

    public static boolean isEncrypted(String s) {
        if (s == null) {
            return false;
        }
        try {
            String d = decrypt(s, false);
            return hasMarkers(d);
        } catch (RuntimeException ex) {
            return false;
        }
    }

    public static String encrypt(String s) {
        s = ENCRYPTED_PREFIX + s + ENCRYPTED_SUFFIX;
        StringBuilder rtn = new StringBuilder(s.length());
        byte[] bytes = s.getBytes();
        for (int i = 0; i < bytes.length; i++) {
            rtn.append(encrypt(bytes[i], KEY[i % KEY.length]));
        }
        return rtn.toString();
    }

    private static String encrypt(byte b, byte key) {
        b = (byte) (b ^ key);
        String rtn = Integer.toHexString(b);
        if (rtn.length() == 1) {
            rtn = "0" + rtn;
        }
        return rtn;
    }

    public static String decrypt(String s) {
        return decrypt(s, true);
    }

    private static String decrypt(String s, boolean removeMarkers) {
        byte[] rtn = new byte[s.length() / 2];
        int j = 0;
        for (int i = 0; i < s.length(); i += 2) {
            String hex = s.substring(i, i + 2);
            rtn[j] = (byte) (Integer.parseInt(hex, 16) ^ KEY[j % KEY.length]);
            j++;
        }
        String d = new String(rtn);
        if (removeMarkers && hasMarkers(d)) {
            return d.substring(ENCRYPTED_PREFIX.length(), d.length() - ENCRYPTED_SUFFIX.length());
        }
        return d;
    }

    private static boolean hasMarkers(String s) {
        return s.startsWith(ENCRYPTED_PREFIX) && s.endsWith(ENCRYPTED_SUFFIX);
    }
}

Related

  1. encryptBankCardNoLast4Bits(String cardNo)
  2. encryptBytes(final byte[] b, final int i)
  3. encryptChar(String caracter, int variable, int indice)
  4. EncryptDecrypt(String value)
  5. encryptEmailId(String emailId)
  6. encryption(String str, int k)
  7. encryptPlayerChat(byte[] is, int i_25_, int i_26_, int i_27_, byte[] is_28_)
  8. encryptPwd(String src)
  9. encryptStrBuff(byte[] resultBytes)