Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. Copyright 1997-2010 Oracle and/or its affiliates. All rights reserved. Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their respective owners. The contents of this file are subject to the terms of either the GNU General Public License Version 2 only ("GPL") or the Common Development and Distribution License("CDDL") (collectively, the "License"). You may not use this file except in compliance with the License. You can obtain a copy of the License at http://www.netbeans.org/cddl-gplv2.html or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the specific language governing permissions and limitations under the License. When distributing the software, include this License Header Notice in each file and include the License file at nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this particular file as subject to the "Classpath" exception as provided by Sun in the GPL Version 2 section of the License file that accompanied this code. If applicable, add the following below the License Header, with the fields enclosed by brackets [] replaced by your own identifying information: "Portions Copyrighted [year] [name of copyright owner]" Contributor(s): The Original Software is the CVS Client Library. The Initial Developer of the Original Software is Robert Greig. Portions created by Robert Greig are Copyright (C) 2000. All Rights Reserved. If you wish your version of this file to be governed by only the CDDL or only the GPL Version 2, indicate your decision by adding "[Contributor] elects to include this software in this distribution under the [CDDL or GPL Version 2] license." If you do not indicate a single choice of license, a recipient has the option to distribute your version of this file under either the CDDL, the GPL Version 2 or to extend the choice of license to its licensees as provided above. However, if you add GPL Version 2 code and therefore, elected the GPL Version 2 license, then the option applies only if the new code is made subject to such option by the copyright holder. Contributor(s): Robert Greig. /
 
 package org.netbeans.modules.bugtracking.util;
 
Scrambles text (the password) using the standard scheme described in the CVS protocol version 1.10. This encoding is trivial and should not be used for security, but rather as a mechanism for avoiding inadvertant compromise.

Author(s):
Robert Greig, Tomas Stupka
 
 class Scrambler {
 
     private static final char [] characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".toCharArray();

    
The mapping array
 
     private int[] shifts;

    
The single instance of this class (Singleton design pattern)
 
     public static Scrambler instance;

    
Do not instantiate the scrambler directly. Use the getInstance() method
 
     private Scrambler() {
         int i;
          = new int[256];
 
         for (i = 0; i < 32; ++i) {
             [i] = i;
         }
 
         [i++] = 114;
         [i++] = 120;
         [i++] = 53;
         [i++] = 79;
         [i++] = 96;
         [i++] = 109;
         [i++] = 72;
         [i++] = 108;
         [i++] = 70;
         [i++] = 64;
         [i++] = 76;
         [i++] = 67;
         [i++] = 116;
         [i++] = 74;
         [i++] = 68;
         [i++] = 87;
         [i++] = 111;
        [i++] = 52;
        [i++] = 75;
        [i++] = 119;
        [i++] = 49;
        [i++] = 34;
        [i++] = 82;
        [i++] = 81;
        [i++] = 95;
        [i++] = 65;
        [i++] = 112;
        [i++] = 86;
        [i++] = 118;
        [i++] = 110;
        [i++] = 122;
        [i++] = 105;
        [i++] = 41;
        [i++] = 57;
        [i++] = 83;
        [i++] = 43;
        [i++] = 46;
        [i++] = 102;
        [i++] = 40;
        [i++] = 89;
        [i++] = 38;
        [i++] = 103;
        [i++] = 45;
        [i++] = 50;
        [i++] = 42;
        [i++] = 123;
        [i++] = 91;
        [i++] = 35;
        [i++] = 125;
        [i++] = 55;
        [i++] = 54;
        [i++] = 66;
        [i++] = 124;
        [i++] = 126;
        [i++] = 59;
        [i++] = 47;
        [i++] = 92;
        [i++] = 71;
        [i++] = 115;
        [i++] = 78;
        [i++] = 88;
        [i++] = 107;
        [i++] = 106;
        [i++] = 56;
        [i++] = 36;
        [i++] = 121;
        [i++] = 117;
        [i++] = 104;
        [i++] = 101;
        [i++] = 100;
        [i++] = 69;
        [i++] = 73;
        [i++] = 99;
        [i++] = 63;
        [i++] = 94;
        [i++] = 93;
        [i++] = 39;
        [i++] = 37;
        [i++] = 61;
        [i++] = 48;
        [i++] = 58;
        [i++] = 113;
        [i++] = 32;
        [i++] = 90;
        [i++] = 44;
        [i++] = 98;
        [i++] = 60;
        [i++] = 51;
        [i++] = 33;
        [i++] = 97;
        [i++] = 62;
        [i++] = 77;
        [i++] = 84;
        [i++] = 80;
        [i++] = 85;
        [i++] = 223;
        [i++] = 225;
        [i++] = 216;
        [i++] = 187;
        [i++] = 166;
        [i++] = 229;
        [i++] = 189;
        [i++] = 222;
        [i++] = 188;
        [i++] = 141;
        [i++] = 249;
        [i++] = 148;
        [i++] = 200;
        [i++] = 184;
        [i++] = 136;
        [i++] = 248;
        [i++] = 190;
        [i++] = 199;
        [i++] = 170;
        [i++] = 181;
        [i++] = 204;
        [i++] = 138;
        [i++] = 232;
        [i++] = 218;
        [i++] = 183;
        [i++] = 255;
        [i++] = 234;
        [i++] = 220;
        [i++] = 247;
        [i++] = 213;
        [i++] = 203;
        [i++] = 226;
        [i++] = 193;
        [i++] = 174;
        [i++] = 172;
        [i++] = 228;
        [i++] = 252;
        [i++] = 217;
        [i++] = 201;
        [i++] = 131;
        [i++] = 230;
        [i++] = 197;
        [i++] = 211;
        [i++] = 145;
        [i++] = 238;
        [i++] = 161;
        [i++] = 179;
        [i++] = 160;
        [i++] = 212;
        [i++] = 207;
        [i++] = 221;
        [i++] = 254;
        [i++] = 173;
        [i++] = 202;
        [i++] = 146;
        [i++] = 224;
        [i++] = 151;
        [i++] = 140;
        [i++] = 196;
        [i++] = 205;
        [i++] = 130;
        [i++] = 135;
        [i++] = 133;
        [i++] = 143;
        [i++] = 246;
        [i++] = 192;
        [i++] = 159;
        [i++] = 244;
        [i++] = 239;
        [i++] = 185;
        [i++] = 168;
        [i++] = 215;
        [i++] = 144;
        [i++] = 139;
        [i++] = 165;
        [i++] = 180;
        [i++] = 157;
        [i++] = 147;
        [i++] = 186;
        [i++] = 214;
        [i++] = 176;
        [i++] = 227;
        [i++] = 231;
        [i++] = 219;
        [i++] = 169;
        [i++] = 175;
        [i++] = 156;
        [i++] = 206;
        [i++] = 198;
        [i++] = 129;
        [i++] = 164;
        [i++] = 150;
        [i++] = 210;
        [i++] = 154;
        [i++] = 177;
        [i++] = 134;
        [i++] = 127;
        [i++] = 182;
        [i++] = 128;
        [i++] = 158;
        [i++] = 208;
        [i++] = 162;
        [i++] = 132;
        [i++] = 167;
        [i++] = 209;
        [i++] = 149;
        [i++] = 241;
        [i++] = 153;
        [i++] = 251;
        [i++] = 237;
        [i++] = 236;
        [i++] = 171;
        [i++] = 195;
        [i++] = 243;
        [i++] = 233;
        [i++] = 253;
        [i++] = 240;
        [i++] = 194;
        [i++] = 250;
        [i++] = 191;
        [i++] = 155;
        [i++] = 142;
        [i++] = 137;
        [i++] = 245;
        [i++] = 235;
        [i++] = 163;
        [i++] = 242;
        [i++] = 178;
        [i++] = 152;
    }

    
Get an instance of the standard scrambler
    public static Scrambler getInstance() {
        if ( == null) {
             = new Scrambler();
        }
        return ;
    }

    
Scramble text, turning it into a String of scrambled data

Returns:
a String of scrambled data
    public String scramble(String text) {
        StringBuffer buf = new StringBuffer("A"); //NOI18N
        if (text != null) {
            for (int i = 0; i < text.length(); ++i) {
                buf.append(scramble(text.charAt(i)));
            }
        }        
        return new String(encode(buf.toString().getBytes()));
    }
    
    public String descramble(String scrambledText) {        
        StringBuffer buf = new StringBuffer(); 
        if (scrambledText != null) {            
            byte[] decoded = decode(scrambledText); 
            for (int i = 1; i < decoded.length; ++i) {                
                buf.append(scramble((char)decoded[i]));
            }
        }
        return buf.toString();
    }    
    
    private char scramble(char ch) {               
        int i = ( [ (((intch) &  0xFF) & 255] & 255 ) & 
                ( [ (((intch) &  0x00FF) & 255] & 255 );
        return Character.toChars(i)[0];
    }     
    
    private byte[] decode(String str) {        
        return decode64(str);
    }
    
    private byte[] encode(byte[] encode) {        
        return encode64(encode).getBytes();
    }
    private static String encode64(byte [] data) {
        return encode64(datafalse);
    }
    private static String encode64(byte [] databoolean useNewlines) {
        int length = data.length;
        StringBuffer sb = new StringBuffer(data.length * 3 / 2);
        int end = length - 3;
        int i = 0;
        int lineCount = 0;
        while (i <= end) {
            int d = ((((intdata[i]) & 0xFF) << 16) | ((((intdata[i + 1]) & 0xFF) << 8) | (((intdata[i + 2]) & 0xFF);
            sb.append([(d >> 18) & 0x3F]);
            sb.append([(d >> 12) & 0x3F]);
            sb.append([(d >> 6) & 0x3F]);
            sb.append([d & 0x3F]);
            i += 3;
            if (useNewlines && lineCount++ >= 14) {
                lineCount = 0;
                sb.append(System.getProperty("line.separator"));
            }
        }
        if (i == length - 2) {
            int d = ((((intdata[i]) & 0xFF) << 16) | ((((intdata[i + 1]) & 0xFF) << 8);
            sb.append([(d >> 18) & 0x3F]);
            sb.append([(d >> 12) & 0x3F]);
            sb.append([(d >> 6) & 0x3F]);
            sb.append("=");
        } else if (i == length - 1) {
            int d = (((intdata[i]) & 0xFF) << 16;
            sb.append([(d >> 18) & 0x3F]);
            sb.append([(d >> 12) & 0x3F]);
            sb.append("==");
        }
        return sb.toString();
    }
    private static byte [] decode64(String s) {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        decode64(sbos);
        return bos.toByteArray();
    private static void decode64(String sByteArrayOutputStream bos) {
        int i = 0;
        int len = s.length();
        for (;;) {
            while (i < len && s.charAt(i) <= ' 'i++;
            if (i == lenbreak;
            int tri = (decode64(s.charAt(i)) << 18) + (decode64(s.charAt(i+1)) << 12) + (decode64(s.charAt(i+2)) << 6) + (decode64 (s.charAt(i+3)));
            bos.write((tri >> 16) & 255);
            if (s.charAt(i+2) == '='break;
            bos.write((tri >> 8) & 255);
            if (s.charAt(i+3) == '='break;
            bos.write(tri & 255);
            i += 4;
        }
    }
    private static int decode64(char c) {
        if (c >= 'A' && c <= 'Z') {
            return ((intc) - 65;
        } else if (c >= 'a' && c <= 'z') {
            return ((intc) - 97 + 26;
        } else if (c >= '0' && c <= '9') {
            return ((intc) - 48 + 26 + 26;
        } else switch (c) {
            case '+':
                return 62;
            case '/':
                return 63;
            case '=':
                return 0;
            default:
                throw new RuntimeException("Base64: unexpected code: " + c);
        }
    }
New to GrepCode? Check out our FAQ X