zoukankan      html  css  js  c++  java
  • 身份证号、姓名按照某个规则加密

    1、身份证号加密
    规则如下:
    出生月份+1,如果超出12则变01;倒数第2、3位+1,如果超出99则变01,最后还要重新计算最后一位的校验码,计算规则如下:
    (1)身份证号码前17位数分别乘以不同的系数并相加 7 9 10 5 8 4 2 1 6 3 7 9 10 5 8 4 2
    (2)用第1步的结果除以11,余数只可能有0 1 2 3 4 5 6 7 8 9 10这11个数字中,分别对应身份证的最后一位1 0 X 9 8 7 6 5 4 3 2
           
    例:110101198808083034 加密后为 110101198808083034

    /// <summary>
    /// 证件加密:出生月份+1,如果超出12则变01;倒数第2、3位+1,如果超出99则变01,重计最后一位校验码
    /// </summary>
    /// <param name="idno"></param>
    /// <returns></returns>
    public static string GetEncryptIdNo(string idno)
    {
        string tempIdno = "", newIdno = "";
        if (idno.Length != 18)
            return idno;
        string head = idno.Substring(0, 10);
        string month = AddNumerOne(idno.Substring(10, 2), 12);
        string end = idno.Substring(12, 3) + AddNumerOne(idno.Substring(15, 2), 99);
        tempIdno = head + month + end;
    
        int[] Wi = { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 };
        string[] arrVerifyCode = { "1", "0", "x", "9", "8", "7", "6", "5", "4", "3", "2" };
        int ret = 0;
        for (int i = 0; i < 17; i++)
            ret += int.Parse(tempIdno.Substring(i, 1)) * Wi[i];
    
        newIdno = tempIdno.Substring(0, 17) + arrVerifyCode[ret %= 11];
        return newIdno;
    }
    
    /// <summary>
    /// 对传进来的数加1,如果超过则减去参数v
    /// </summary>
    /// <param name="month"></param>
    /// <param name="v"></param>
    /// <returns></returns>
    private static string AddNumerOne(string s, int v)
    {
        int result = Convert.ToInt32(s) + 1;
        if (result > v)
            result = result - v;
        return result.ToString().PadLeft(2, '0');
    }
    

    2、姓名的姓加密
    规则如下:先获取系统所有的姓集合,保存为字典,根据传进来的参数姓,查找字典中对应的KEY值,替换为下一个KEY的姓,如果参数姓为最后一个,则替换为第一个KEY的姓。

    下面例子,如传入姓名“何必”,则结果为“董必”。

    /// <summary>
    /// 姓加密:根据传进来的参数姓,查找字典中对应的KEY值,替换为下一个KEY的姓,如果参数姓为最后一个,则替换为第一个KEY的姓,找不到则默认第一个KEY的姓
    /// </summary>
    /// <param name="name"></param>
    /// <returns></returns>
    public static string GetEncryptName(string name)
            {
                string result = "";
                if (name.Length == 0)
                    return "";
    
                Dictionary<int, string> dict = new Dictionary<int, string>()
                {
                   {1,"叶"},{2,"魏"},{3,"洪"},{4,"孙"},{5,"何"},{6,"董"},{7,"饶"},{8,"邵"},{9,"赖"},{10,"夏"},
                   {11,"肖"},{12,"付"},{13,"徐"},{14,"范"},{15,"孟"},{16,"柯"},{17,"任"},{18,"欧"},{19,"温"},{20,"邓"}
            //太多了。。这里省略不写
                };
                result = name.Substring(0, 1);
                int key = dict.FirstOrDefault(d => d.Value == result).Key + 1;
                if (key > dict.Keys.Count)
                    key = key - dict.Keys.Count;
    
                result = dict.FirstOrDefault(d => d.Key == key).Value;
                return result + name.Substring(1);
            }
    




  • 相关阅读:
    set集合操作
    python中字符串操作
    字典----增删改查遍历
    C#反射回顾笔记
    消息队列之ActiveMQ学习笔记(二、C#实例实现)
    消息队列之ActiveMQ学习笔记(一、下载及安装)
    依赖注入之AutoFac
    layer弹框层学习笔记
    VS自定义代码块Code Snippet
    博客园添加链接
  • 原文地址:https://www.cnblogs.com/gdjlc/p/3259307.html
Copyright © 2011-2022 走看看