zoukankan      html  css  js  c++  java
  • 简单对称加密

    根据网上的C版本改写

    public static class WCoder
        {

            //原始加密索引
             static byte[] _cypherOrg = new byte[]
             {
              95 , 22556 , 72 , 2161151491642472  , 6  , 10 , 30 , 34 , 102170
              22952 , 92 , 22855 , 89 , 23538 , 10619021711214417123049 , 
              1  , 3  , 5  , 15 , 17 , 51 , 85 , 25526 , 46 , 11415016124819 , 53 ,
              83 , 2454  , 12 , 20 , 60 , 68 , 20479 , 209104184211110178205
              15918621310017223942 , 126130157188223122142137128
              18119687 , 24916 , 48 , 80 , 24011 , 29 , 39 , 10518721497 , 163
              25425 , 43 , 12513514617323647 , 11314717423332 , 96 , 160
              25122 , 58 , 78 , 21010918319493 , 23150 , 86 , 25021 , 63 , 65 , 
              19594 , 22661 , 71 , 20164 , 19291 , 23744 , 116156191218117
              18 , 54 , 90 , 23841 , 12314114014313813314816724213 , 23 , 
              76 , 21210316922459 , 77 , 21598 , 1662418  , 24 , 40 , 120136
              15518219388 , 23235 , 10117523437 , 11117720067 , 19784 , 
              25231 , 33 , 99 , 1652447  , 9  , 27 , 45 , 11915317620370 , 202
              13115818520810718922012712915217920673 , 219118154
              69 , 20774 , 22212113913414516822762 , 66 , 19881 , 24314 , 
              57 , 75 , 22112413215116225328 , 36 , 10818019982 , 2461
             };

            private static void Swap2Byte(ref Byte b1,ref Byte b2)
            {
                b1 ^= b2;
                b2 ^= b1;
                b1 ^= b2;
            }

            /// <summary>
            
    /// 根据key调整加密索引byte数组
            
    /// </summary>
            
    /// <param name="sKey">加密的key</param>
            
    /// <returns></returns>
            private static byte[] GetCypher(string sKey)
            {
                byte[] codeCypher =(byte[])_cypherOrg.Clone();
                for (int i = 0; i < sKey.Length; i++)
                {
                    for (int j = 0; j < codeCypher.Length; j++)
                    {
                        int switchIndex = ((sKey[i]+i) * codeCypher[j]) % 255;
                        if (switchIndex != j)
                        {
                            Swap2Byte(ref codeCypher[j], ref codeCypher[switchIndex]);
                        }
                    }
                }
                return codeCypher;
            }
            /// <summary>
            
    /// 实际加密方法
            
    /// </summary>
            
    /// <param name="BuffPassWord"></param>
            
    /// <param name="sKey"></param>
            
    /// <returns></returns>
            private static string WEnCode(string BuffPassWord, string sKey)
            {
                Byte[] cCypher = GetCypher(sKey);
                int iIndex = 0;
                byte[] Buff = System.Text.Encoding.UTF8.GetBytes(BuffPassWord);     //java:string.getBytes();
                while (iIndex < Buff.Length)
                {
                    Buff[iIndex] ^= cCypher[iIndex % 255];
                    iIndex++;
                }
                return ByteToHexString(Buff);
                //StringBuilder sb = new StringBuilder();
                
    //for (int i = 0; i < Buff.Length; i++)
                
    //{
                
    //    sb.Append(Buff[i].ToString("X2"));      //转成0-9 A-F的组合
                
    //}
                
    //return sb.ToString();
            }

            private static string ByteToHexString(Byte[] src)
            {
                StringBuilder sb = new StringBuilder();
                if (src == null || src.Length == 0)            
                    return null;
                for (int i = 0; i < src.Length; i++)
                {
                    int iCur = src[i] & 0xFF;
                    string sCur = iCur.ToString("X2");      //java:Integer.toHexString(iCur); 
                    if (sCur.Length < 2)
                    {
                        sb.Append(0);
                    }
                    sb.Append(sCur);
                }
                return sb.ToString();
            }

            /// <summary>
            
    /// 实际解密方法
            
    /// </summary>
            
    /// <param name="EncryptedPassWord"></param>
            
    /// <param name="sKey"></param>
            
    /// <returns></returns>
            private static string WDeCode(string EncryptedPassWord, string sKey)
            {
                string strPwd = "";
                int iIndex = 0;
                try
                {
                    byte[] Buff;
                    //    Buff = new byte[EncryptedPassWord.Length / 2];
                    
    //for (int i = 0; i < EncryptedPassWord.Length; i += 2)
                    
    //{
                    
    //    Buff[i / 2] = Byte.Parse(EncryptedPassWord.Substring(i, 2), System.Globalization.NumberStyles.HexNumber);
                    
    //}
                    Buff = HexStringToBytes(EncryptedPassWord);
                    Byte[] cCypher = GetCypher(sKey);
                    while (iIndex < Buff.Length)
                    {
                        Buff[iIndex] ^= cCypher[iIndex % 255];
                        iIndex++;
                    }
                    strPwd = System.Text.Encoding.UTF8.GetString(Buff); //java:new string(byte[])
                    
                }
                catch
                {
                }
                return strPwd;
            }

            /// <summary>
            
    /// 十六进制字符串(0123456789ABCDEF)转Byte数组
            
    /// </summary>
            
    /// <param name="hexString"></param>
            
    /// <returns></returns>
            private static Byte[] HexStringToBytes(string hexString)
            {
                if (hexString == null || hexString == "")
                    return null;
                hexString = hexString.ToUpper();
                int length = hexString.Length / 2;
                char[] hexChars = hexString.ToCharArray();
                byte[] bRes = new byte[length];
                for (int i = 0; i < length; i++)
                {
                    int pos = i * 2;
                    bRes[i] = (byte)(CharToByte(hexChars[pos]) << 4 | CharToByte(hexChars[pos+1]));
                }
                return bRes;
            }
            /// <summary>
            
    /// char转byte
            
    /// </summary>
            
    /// <param name="c"></param>
            
    /// <returns></returns>
            private static byte CharToByte(char c)
            {
                return (byte)"0123456789ABCDEF".IndexOf(c); 
            }

            /// <summary>
            
    /// 根据key加密password
            
    /// </summary>
            
    /// <param name="passWord"></param>
            
    /// <param name="sKey"></param>
            
    /// <returns></returns>
            public static string WEncode(string passWord, string sKey)
            {
                return WEnCode(passWord, sKey);
            }
            /// <summary>
            
    /// 根据key解密password
            
    /// </summary>
            
    /// <param name="passWord"></param>
            
    /// <param name="sKey"></param>
            
    /// <returns></returns>
            public static string WDecode(string passWord, string sKey)
            {
                return WDeCode(passWord, sKey);
            }
        }
    #coding=gbk
    import struct
    internalCypher = (95 , 225, 56 , 72 , 216, 115, 149, 164, 247, 2  , 6  , 10 , 30 , 34 , 102, 170, 
              229, 52 , 92 , 228, 55 , 89 , 235, 38 , 106, 190, 217, 112, 144, 171, 230, 49 , 
              1  , 3  , 5  , 15 , 17 , 51 , 85 , 255, 26 , 46 , 114, 150, 161, 248, 19 , 53 ,
              83 , 245, 4  , 12 , 20 , 60 , 68 , 204, 79 , 209, 104, 184, 211, 110, 178, 205, 
              159, 186, 213, 100, 172, 239, 42 , 126, 130, 157, 188, 223, 122, 142, 137, 128, 
              181, 196, 87 , 249, 16 , 48 , 80 , 240, 11 , 29 , 39 , 105, 187, 214, 97 , 163, 
              254, 25 , 43 , 125, 135, 146, 173, 236, 47 , 113, 147, 174, 233, 32 , 96 , 160, 
              251, 22 , 58 , 78 , 210, 109, 183, 194, 93 , 231, 50 , 86 , 250, 21 , 63 , 65 , 
              195, 94 , 226, 61 , 71 , 201, 64 , 192, 91 , 237, 44 , 116, 156, 191, 218, 117, 
              18 , 54 , 90 , 238, 41 , 123, 141, 140, 143, 138, 133, 148, 167, 242, 13 , 23 , 
              76 , 212, 103, 169, 224, 59 , 77 , 215, 98 , 166, 241, 8  , 24 , 40 , 120, 136, 
              155, 182, 193, 88 , 232, 35 , 101, 175, 234, 37 , 111, 177, 200, 67 , 197, 84 , 
              252, 31 , 33 , 99 , 165, 244, 7  , 9  , 27 , 45 , 119, 153, 176, 203, 70 , 202, 
              131, 158, 185, 208, 107, 189, 220, 127, 129, 152, 179, 206, 73 , 219, 118, 154, 
              69 , 207, 74 , 222, 121, 139, 134, 145, 168, 227, 62 , 66 , 198, 81 , 243, 14 , 
              57 , 75 , 221, 124, 132, 151, 162, 253, 28 , 36 , 108, 180, 199, 82 , 246, 1)
    
    def GetCurCypher(sKey):
        curCypher = [c for c in internalCypher]
        for i in range(len(sKey)):
            for j in range(len(curCypher)):
                switchIndex = ((ord(sKey[i])+i)* curCypher[j]) % 255
                if(switchIndex != j):
                    curCypher[j],curCypher[switchIndex] = curCypher[switchIndex],curCypher[j]
        return curCypher
    
    
    
    def BytestoHexString(barr):
        s = ''
        if barr is None or len(barr)==0:
            return s
        l = [GetSingleHex(b) for b in barr]
        return reduce(lambda x,y:x+y,l)
    
    def GetSingleHex(b):
        iCur = b & 0xFF
        sCur = hex(b)
        sCur = sCur.replace("0x","")
        if(len(sCur)==1):
            sCur="0" + sCur
        return sCur
    
    
    def WEncode(sPassword,sKey):
        myCypher = GetCurCypher(sKey)
        iIndex = 0
        byPwd = sPassword.encode('utf-8')
        barrPwd = bytearray(byPwd)
        barrLen = len(barrPwd)
        while iIndex<barrLen:        
            barrPwd[iIndex] = barrPwd[iIndex] ^ myCypher[iIndex % 255]
            iIndex += 1
        encryptedPwd = BytestoHexString(barrPwd)    
        return encryptedPwd
    
    def ChartoByte(c):
        s = "0123456789ABCDEF"
        return (s.find(c))
    
    def HexStringtoBytes(sHex):
        if sHex is None or sHex=="":
            return None
        sHex = sHex.upper()
        length = len(sHex)   
        barr = []
        for i in range(0,length,2):
            pos = i
            by = (ChartoByte(sHex[pos]) << 4 | ChartoByte(sHex[pos+1]))
            barr.append(by)
        return barr
    
    def WDecode(encryptedPassword,sKey):
        myCypher = GetCurCypher(sKey)
        barrPwd = HexStringtoBytes(encryptedPassword)
        strPwd = ""
        iIndex = 0
        barrLen = len(barrPwd)
        while iIndex<barrLen:
            barrPwd[iIndex] = barrPwd[iIndex] ^ myCypher[iIndex % 255]
            strPwd = strPwd + chr(barrPwd[iIndex])
            iIndex += 1    
        return strPwd;
    
    
    while(True):
        q = raw_input("input password and skey(password,skey)\n>>>")
        if(q=="q"):
            break
        else:
            oPwd = q
            sKey = ""
            if(q.find(",")>=0):
                pwd_skey = q.split(",")
                oPwd = pwd_skey[0]
                sKey = pwd_skey[1]
            enPwd = WEncode(oPwd,sKey)
            print "加密结果:" + enPwd+ "\n"
            oPwd1= WDecode(enPwd,sKey)
            print "原字符:" + oPwd1
  • 相关阅读:
    网络拓扑
    OSI 7层模型和TCP/IP 4层模型
    第一范式 第二范式 第三范式 BC范式 第四范式
    医院 信息科
    李纳斯•托瓦兹
    所谓绅士,就是做自己该做之事,而不是想做之事。
    活着
    开头词
    人际题目
    人际关系
  • 原文地址:https://www.cnblogs.com/fenix/p/2426906.html
Copyright © 2011-2022 走看看