当前位置: 动力学知识库 > 问答 > 编程问答 >

java - Hiding The Data

问题描述:

I want to mask few columns based on the criticality of that column, so that once i give these files to my team, they should see masked or encrypted value for development purpose.

I have a file(excel,csv, tsv or anything) which contains 7 columns, columns can increase or decrease based on the file.

name,creditcard,lastname,personal_data,address,city,state

ajay,1234567899999993,jat,1234,pune,pimpri,maharashtra

naresh,9876543210547863,mantri,2674,pune,timpri,maharashtra

sanjay,2452352352525523,hatra,3224,pune,wakad,maharashtra

sarabjit,12235667889768,rango,3114,pune,timpri,maharashtra

i need to mask few column data based on the number of alphabet it has.

Say for example i want to mask creditcard number,personal_data. after masking the output file should contain the same number of columns as per the input file.

But the 2 or 3 or what ever columns are masked , it should contain same no. of charaters but may be in different format like (creditcard number is 16 digits so it should contain exact 16 but can contain alpha numeric in the output file.

Request you to provide a java code or some routine or logic to build this complex situation.

I need to give these kind of multiples files to my members so that they cannot see important information which are masked but still work on those columns for further development purpose.

网友答案:

It sounds like you want to read and parse each line of the input file, obfuscate the contents of some particular field(s), then write the line out to the output file.

To obscure some particular fields, you can hash the fields and replace them with the base-64 encoding of the resulting hashcodes, truncated to the same length as the original field data.

But if all you want to do is just obscure some fields, replacing them with randomly generated characters should be sufficient.

It sounds like the only trick is knowing which fields to obscure.

Addendum

Here is a method that generates a random string of the same length as a source string:

import java.util.*;

public class RandomGenerator
{
    static private long     s_seed =  123;
    static private Random   s_rnd =   new Random(s_seed);

    public static String genRandomString(String src)
    {
        char[] dst = new char[src.length()];

        for (int i = 0;  i < dst.length;  i++)
        {
            char ch = src.charAt(i);

            if (Character.isDigit(ch))
                ch = (char) (s_rnd.nextInt(10) + '0');    // Random char [0-9]
            else if (Character.isAlphabetic(ch))
                ch = (char) (s_rnd.nextInt(26) + 'A');    // Random char [A-Z]
            else
                ch = '?';
            dst[i] = ch;
        }
        return new String(dst);
    }
}

Test program available at: Ideone.

分享给朋友:
您可能感兴趣的文章:
随机阅读: