Java RC4加密生成随机不反复邀请码

1 篇文章 0 订阅
1 篇文章 0 订阅

要批量生成推广码,首先我们知道推广码的特点:

1:不可反复
2:不能够被猜测出

关于这两点,我们的思路大体分为例如以下几类:
1:每次生成一个随机码后查数据库是否有同样的,有则又一次生成(每次都要访问数据库。导致效率极低,不推荐)
2:根据数据库的主键作为唯一键,进行打乱或插入操作,如主键为1000001,取出后生成3位(据需求增减)随机数或字母。插入主键值中。构成如10000c1Dsd这样的串,可保证推广码不反复及不可猜测出(这样的方式也须要连接数据库取主键,当然能够一次性预生成所需数目的主键,然后生成相应推广码后更新进数据库,效率会高一些)
3:利用算法来保证值唯一。如RC4加密生成等,本篇重点介绍此种方式(不需连接数据库,算法选择合适则效率非常高)

详细实现
上面说了,本文重点讨论利用算法实现不反复性,首先我们会想到最简单的方式:我们来看下详细效果:

/**
 * 加密,返回密文
 * @param data 明文
 * @param key 密钥
 * @return 密文
 */
public static String encry_RC4_string(String data, String key) {
    if (data == null || key == null) {
        return null;
    }
    return toHexString(asString(encry_RC4_byte(data, key)));
}


private static String asString(byte[] buf) {
    StringBuffer strbuf = new StringBuffer(buf.length);
    for (int i = 0; i < buf.length; i++) {
        strbuf.append((char) buf[i]);
    }
    return strbuf.toString();
}

private static byte[] initKey(String aKey) {
    byte[] b_key = null;
    try {
        b_key = aKey.getBytes(CHARSET);
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
    byte state[] = new byte[256];

    for (int i = 0; i <256; i++) {
        state[i] = (byte) i;
    }
    int index1 = 0;
    int index2 = 0;
    if (b_key == null || b_key.length == 0) {
        return null;
    }
    for (int i = 0; i < 256; i++) {
        index2 = ((b_key[index1] & 0xff) + (state[i] & 0xff) + index2) & 0xff;
        byte tmp = state[i];
        state[i] = state[index2];
        state[index2] = tmp;
        index1 = (index1 + 1) % b_key.length;
    }
    return state;
}

private static String toHexString(String s) {
    String str = "";
    for (int i = 0; i < s.length(); i++) {
        int ch = (int) s.charAt(i);
        String s4 = Integer.toHexString(ch & 0xFF);
        if (s4.length() == 1) {
            s4 = '0' + s4;
        }
        str = str + s4;
    }
    return str;// 0x表示十六进制
}

private static byte[] HexString2Bytes(String src) {
    int size = src.length();
    byte[] ret = new byte[size / 2];
    byte[] tmp = src.getBytes();
    for (int i = 0; i < size / 2; i++) {
        ret[i] = uniteBytes(tmp[i * 2], tmp[i * 2 + 1]);
    }
    return ret;
}

private static byte uniteBytes(byte src0, byte src1) {
    char _b0 = (char) Byte.decode("0x" + new String(new byte[]{src0})).byteValue();
    _b0 = (char) (_b0 << 4);
    char _b1 = (char) Byte.decode("0x" + new String(new byte[]{src1})).byteValue();
    byte ret = (byte) (_b0 ^ _b1);
    return ret;
}

private static byte[] RC4Base(byte[] input, String mKkey) {
    int x = 0;
    int y = 0;
    byte key[] = initKey(mKkey);
    int xorIndex;
    byte[] result = new byte[input.length];

    for (int i = 0; i < input.length; i++) {
        x = (x + 1) & 0xff;
        y = ((key[x] & 0xff) + y) & 0xff;
        byte tmp = key[x];
        key[x] = key[y];
        key[y] = tmp;
        xorIndex = ((key[x] & 0xff) + (key[y] & 0xff)) & 0xff;
        result[i] = (byte) (input[i] ^ key[xorIndex]);
    }
    return result;
}

public static void main(String[] args) {
    for (int i = 0; i < 20 ; i++) {
        int gg =(int)(Math.random()*900)+100;
        String code = Integer.toString(gg);
        String key = UUID.randomUUID().toString().replace("-","");
        String encryStr = encry_RC4_string(String.format("%03d",123 ), key);
        System.out.println(encryStr);
        System.out.println(decry_RC4(encryStr, key));
    }

}

/**
 * 生成6位密文
 * @param data 明文,可以为用户id等
 * @param key 密钥
 * @return
 */
public static String m4Rc4Encrypt(int data, String key) {
    if (null == key) {
        key = UUID.randomUUID().toString();
    }
    return encry_RC4_string(String.format("%03d", data), key);
}

在这里 String.format(“%03d”, i) 主要实现位数不够前面补零。如果一个数字不超过3位,则会在其前面补零以到达规定的3位数,其中0被填充到缺省位,3代表规定位数 d代表是整型。

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

晨集

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值