All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.alachisoft.ncache.ncactivate.crypto.Impl Maven / Gradle / Ivy

There is a newer version: 5.3.3
Show newest version
/*
 * Impl.java
 *
 * Created on October 7, 2006, 1:41 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */
package com.alachisoft.ncache.ncactivate.crypto;

import com.alachisoft.ncache.ncactivate.utils.StringRef;

/**
 * @author Administrator
 */
public class Impl {

    /**
     * Creates a new instance of Impl
     */
    public Impl() {
    }

    public static boolean is_base64(byte c) {
        if (c >= (char) 'A' && c <= (char) 'Z') {
            return true;
        }

        if (c >= (char) 'a' && c <= (char) 'z') {
            return true;
        }

        if (c >= (char) '0' && c <= (char) '9') {
            return true;
        }

        if (c == (char) '+') {
            return true;
        }

        if (c == (char) '/') {
            return true;
        }
        ;

        if (c == (char) '=') {
            return true;
        }

        return false;
    }

    public static byte decodeb(byte c) {
        if (c >= (char) 'A' && c <= (char) 'Z') {
            return (byte) (c - (char) 'A');
        }
        if (c >= (char) 'a' && c <= (char) 'z') {
            return (byte) (c - (char) 'a' + 26);
        }
        if (c >= (char) '0' && c <= (char) '9') {
            return (byte) (c - (char) '0' + 52);
        }
        if (c == (char) '+') {
            return 62;
        }
        ;
        return 63;
    }

    ;

    public static byte encodeb(byte uc) {
        if (uc < 26) {
            return (byte) ((char) 'A' + uc);
        }
        if (uc < 52) {
            return (byte) ((char) 'a' + (uc - 26));
        }
        if (uc < 62) {
            return (byte) ((char) '0' + (uc - 52));
        }
        if (uc == 62) {
            return (byte) ((char) '+');
        }
        return (char) '/';
    }

    public static char decryptb(char b, int ncount) {
        char ch = 0, ch1 = 0, ch2 = 0;
        ch = b;
        ch1 = (char) (ch & 0xf);
        ch2 = (char) ((ch >> 4) & 0xf);
        ch = (char) ((ch1 << 4) | ch2);
        return (char) (ch ^ (((0x33 + ncount) % 254) + 1));
    }

    public static char encryptb(char b, int ncount) {
        char ch = 0, ch1 = 0, ch2 = 0;
        ch = (char) b;
        ch ^= (((0x33 + ncount) % 254) + 1);
        ch1 = (char) (ch & 0xf);
        ch2 = (char) ((ch >> 4) & 0xf);

        return (char) ((ch1 << 4) | ch2);
    }

    public static int encode(String _str, StringRef strRet) {
        int ncount = _str.length();
        if (ncount < 1) {
            return 0;
        }
        ;

        for (int i = 0; i < ncount; i += 3) {
            char by1 = 0, by2 = 0, by3 = 0;

            by1 = encryptb((char) _str.charAt(i), i);
            if (i + 1 < ncount) {
                by2 = encryptb((char) _str.charAt(i + 1), i + 1);
            }
            ;
            if (i + 2 < ncount) {
                by3 = encryptb((char) _str.charAt(i + 2), i + 2);
            }

            char by4 = 0, by5 = 0, by6 = 0, by7 = 0;
            by4 = (char) (by1 >> 2);
            by5 = (char) (((by1 & 0x3) << 4) | (by2 >> 4));
            by6 = (char) (((by2 & 0xf) << 2) | (by3 >> 6));
            by7 = (char) (by3 & 0x3f);

            strRet.strData += (char) encodeb((byte) by4);
            strRet.strData += (char) encodeb((byte) by5);
            if (i + 1 < ncount) {
                strRet.strData += (char) encodeb((byte) by6);
            } else {
                strRet.strData += (char) '=';
            }
            ;
            if (i + 2 < ncount) {
                strRet.strData += (char) encodeb((byte) by7);
            } else {
                strRet.strData += (char) '=';
            }
        }
        return strRet.strData.length();
    }

    public static int encode(byte[] lpArr, int ncount, String strRet) {
        if (ncount < 1) {
            return 0;
        }
        ;

        for (int i = 0; i < ncount; i += 3) {
            char by1 = 0, by2 = 0, by3 = 0;

            by1 = encryptb((char) lpArr[i], i);
            if (i + 1 < ncount) {
                by2 = encryptb((char) lpArr[i + 1], i + 1);
            }
            ;
            if (i + 2 < ncount) {
                by3 = encryptb((char) lpArr[i + 2], i + 2);
            }

            byte by4 = 0, by5 = 0, by6 = 0, by7 = 0;
            by4 = (byte) (by1 >> 2);
            by5 = (byte) (((by1 & 0x3) << 4) | (by2 >> 4));
            by6 = (byte) (((by2 & 0xf) << 2) | (by3 >> 6));
            by7 = (byte) (by3 & 0x3f);

            strRet += (char) encodeb(by4);
            strRet += (char) encodeb(by5);
            if (i + 1 < ncount) {
                strRet += (char) encodeb(by6);
            } else {
                strRet += (char) ('=');
            }
            ;
            if (i + 2 < ncount) {
                strRet += (char) encodeb(by7);
            } else {
                strRet += (char) ('=');
            }
        }
        return strRet.length();
    }

    public static int decode(String _str, byte[] lpArr) {
        int i;
        int j;
        String str = new String();

        for (j = 0; j < _str.length(); j++) {
            if (is_base64((byte) (_str.charAt(j)))) {
                str += _str.charAt(j);
            }
        }

        if (str.length() == 0) {
            return 0;
        }

        int nlen = str.length();
        j = 0;

        for (i = 0; i < nlen; i += 4) {
            byte c1 = (char) 'A',
                    c2 = (char) 'A',
                    c3 = (char) 'A',
                    c4 = (char) 'A';

            c1 = (byte) str.charAt(i);
            if (i + 1 < str.length()) {
                c2 = (byte) str.charAt(i + 1);
            }
            if (i + 2 < str.length()) {
                c3 = (byte) str.charAt(i + 2);
            }
            if (i + 3 < str.length()) {
                c4 = (byte) str.charAt(i + 3);
            }

            byte by1 = 0, by2 = 0, by3 = 0, by4 = 0;
            by1 = decodeb(c1);
            by2 = decodeb(c2);
            by3 = decodeb(c3);
            by4 = decodeb(c4);

            lpArr[j] += decryptb((char) ((by1 << 2) | (by2 >> 4)), j++);
            if (c3 != '=') {
                lpArr[j] += decryptb((char) (((by2 & 0xf) << 4) | (by3 >> 2)), j++);
            }
            if (c4 != '=') {
                lpArr[j] += decryptb((char) (((by3 & 0x3) << 6) | by4), j++);
            }
            ;
        }
        ;
        return j;
    }

    public static int decode(String _str, StringRef strRet) {
        int i;
        int j;
        String str = new String();

        for (j = 0; j < _str.length(); j++) {
            if (is_base64((byte) _str.charAt(j))) {
                str += _str.charAt(j);
            }
        }

        if (str.length() == 0) {
            return 0;
        }

        int nlen = str.length();
        j = 0;

        for (i = 0; i < nlen; i += 4) {
            byte c1 = (char) 'A',
                    c2 = (char) 'A',
                    c3 = (char) 'A',
                    c4 = (char) 'A';

            c1 = (byte) str.charAt(i);
            if (i + 1 < str.length()) {
                c2 = (byte) str.charAt(i + 1);
            }
            ;
            if (i + 2 < str.length()) {
                c3 = (byte) str.charAt(i + 2);
            }
            ;
            if (i + 3 < str.length()) {
                c4 = (byte) str.charAt(i + 3);
            }
            ;
            byte by1 = 0, by2 = 0, by3 = 0, by4 = 0;
            by1 = decodeb(c1);
            by2 = decodeb(c2);
            by3 = decodeb(c3);
            by4 = decodeb(c4);

            strRet.strData += (char) decryptb((char) ((by1 << 2) | (by2 >> 4)), j++);
            if (c3 != '=') {
                strRet.strData += (char) decryptb((char) (((by2 & 0xf) << 4) | (by3 >> 2)), j++);
            }
            if (c4 != '=') {
                strRet.strData += (char) decryptb((char) (((by3 & 0x3) << 6) | by4), j++);
            }
        }
        return j;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy