RELATEED CONSULTING
相关咨询
选择下列产品马上在线沟通
服务时间:9:30-18:00
你可能遇到了下面的问题
关闭右侧工具栏
使用MD5算法加密VFP数据
  • 作者:xiaoxiao
  • 发表时间:2020-12-23 10:38
  • 来源:未知

使用MD5算法加密VFP数据

作者:不详 转贴自:不详 点击数:315 文章录入:windshadow 来源:http://www.souha.net/ArticleShow.asp?ArticleID=463

我们的VFP数据太开放了些,用Excel随便打开一个DBF表,里面的数据就一览无遗。如果数据是比较敏感的话,可就坏事了。例如:我的数据录入员的程序登录密码是放在一个VFP表里,经过加密,就算用Excel打开DBF表,也是看不到密码的。从一个随书资料盘上见到此段代码,供大家参考。------------------------------------********md5.prg**********FUNCTION LShift(lvalue, iShiftBits)IF iShiftBits = 0 RETURN lvalueELSEIF iShiftBits = 31 IF BITAND(lvalue , 1)<>0 RETURN 0x80000000ELSERETURN 0ENDIFENDIFENDIFIF BITAND(lvalue , m_l2Power(31 - iShiftBits))<>0 RETURN BITOR( (BITAND(lvalue , m_lOnBits(31 - (iShiftBits + 1))) * m_l2Power(iShiftBits)) , 0x80000000)ELSERETURN (BITAND(lvalue , m_lOnBits(31 - iShiftBits)) * m_l2Power(iShiftBits))ENDIFENDFUNCFUNCTION RShift(lvalue, iShiftBits)IF iShiftBits = 0 RETURN lvalueELSEIF iShiftBits = 31 IF BITAND(lvalue , 0x80000000) RETURN 1ELSERETURN 0ENDIFENDIFENDIFRShift2 = INT(BITAND(lvalue , 0x7FFFFFFE) / m_l2Power(iShiftBits))IF BITAND(lvalue , 0x80000000)<>0 RShift2 =BITOR (RShift2 , INT(0x40000000 / m_l2Power(iShiftBits - 1)))ENDIFRETURN RShift2ENDFUNCFUNCTION RotateLeft(lvalue, iShiftBits)RETURN BITOR(LShift(lvalue, iShiftBits) ,RShift(lvalue, (32 - iShiftBits)))ENDFUNCFUNCTION AddUnsigned(lX, lY)lX8 = BITAND(lX , 0x80000000)lY8 = BITAND(lY , 0x80000000)lX4 = BITAND(lX , 0x40000000)lY4 = BITAND(lY , 0x40000000)lResult = BITAND(lX , 0x3FFFFFFF) + BITAND(lY , 0x3FFFFFFF)IF BITAND(lX4 , lY4)<> 0 lResult = BITXOR(BITXOR(BITXOR(lResult , 0x80000000) , lX8) , lY8)ELSEIF BITOR(lX4 , lY4)<> 0 IF BITAND(lResult , 0x40000000)<> 0 lResult = BITXOR(BITXOR(BITXOR(lResult , 0xC0000000) , lX8) , lY8)ELSElResult = BITXOR(BITXOR(BITXOR(lResult , 0x40000000) , lX8) , lY8)ENDIFELSElResult = BITXOR(BITXOR( lResult , lX8) , lY8)ENDIFENDIFRETURN lResultENDFUNCFUNCTION md5_F(x, y, z)RETURN BITOR(BITAND(x , y) , BITAND(BITNOT(x) , z))ENDFUNCFUNCTION md5_G(x, y, z)RETURN BITOR(BITAND(x , z) , BITAND(y , BITNOT(z)))ENDFUNCFUNCTION md5_H(x, y, z)RETURN BITXOR(BITXOR(x , y) , z)ENDFUNCFUNCTION md5_I(x, y, z)RETURN BITXOR(y , BITOR(x , BITNOT(z)))ENDFUNCPROCEDURE md5_FF(a, b, c, d, x, s, ac)a = AddUnsigned(a, AddUnsigned(AddUnsigned(md5_F(b, c, d), x), ac))a = RotateLeft(a, s)a = AddUnsigned(a, b)ENDPROCPROCEDURE md5_GG(a, b, c, d, x, s, ac)a = AddUnsigned(a, AddUnsigned(AddUnsigned(md5_G(b, c, d), x), ac))a = RotateLeft(a, s)a = AddUnsigned(a, b)ENDPROCPROCEDURE md5_HH(a, b, c, d, x, s, ac)a = AddUnsigned(a, AddUnsigned(AddUnsigned(md5_H(b, c, d), x), ac))a = RotateLeft(a, s)a = AddUnsigned(a, b)ENDPROCPROCEDURE md5_II(a, b, c, d, x, s, ac)a = AddUnsigned(a, AddUnsigned(AddUnsigned(md5_I(b, c, d), x), ac))a = RotateLeft(a, s)a = AddUnsigned(a, b)ENDPROCFUNCTION Hex(lByte)x=''DO WHILE lByte>0IF lByte>=16y=lByte%16ELSEy=lByteENDIFIF y<10 .and. y>=0x=STR(y,1)+xELSEx=CHR(65+y-10)+xENDIFlByte=(lByte-y)/16ENDDORETURN xENDFUNCFUNCTION WordToHex(lvalue)lResult=''FOR lCount = 0 TO 3lByte = BITAND(RShift(lvalue, lCount * BITS_TO_A_BYTE) , m_lOnBits(BITS_TO_A_BYTE ))lResult = lResult + RIGHT(0 + Hex(lByte), 2)ENDFORRETURN lResultENDFUNC**************************************************!* 这个是加密主函数,只有一个参数smessage,是字符串型*!* 我们说MD5是不可逆的加密方法,由于它完全破坏了原参数的结构*!* 下面是组建种子,对方得不到这些种子就无法进行解密FUNCTION MD5(sMessage)BITS_TO_A_BYTE = 8BYTES_TO_A_WORD = 4BITS_TO_A_WORD = 32DIME m_lOnBits(31)DIME m_l2Power(31)m_lOnBits(1) = 1m_lOnBits(2) = 3m_lOnBits(3) = 7m_lOnBits(4) = 15m_lOnBits(5) = 31m_lOnBits(6) = 63m_lOnBits(7) = 127m_lOnBits(8) = 255m_lOnBits(9) = 511m_lOnBits(10) = 1023m_lOnBits(11) = 2047m_lOnBits(12) = 4095m_lOnBits(13) = 8191m_lOnBits(14) = 16383m_lOnBits(15) = 32767m_lOnBits(16) = 65535m_lOnBits(17) = 131071m_lOnBits(18) = 262143m_lOnBits(19) = 524287m_lOnBits(20) = 1048575m_lOnBits(21) = 2097151m_lOnBits(22) = 4194303m_lOnBits(23) = 8388607m_lOnBits(24) = 16777215m_lOnBits(25) = 33554431m_lOnBits(26) = 67108863m_lOnBits(27) = 134217727m_lOnBits(28) = 268435455m_lOnBits(29) = 536870911m_lOnBits(30) = 1073741823m_lOnBits(31) = 2147483647m_l2Power(1) = 1m_l2Power(2) = 2m_l2Power(3) = 4m_l2Power(4) = 8m_l2Power(5) = 16m_l2Power(6) = 32m_l2Power(7) = 64m_l2Power(8) = 128m_l2Power(9) = 256m_l2Power(10) = 512m_l2Power(11) = 1024m_l2Power(12) = 2048m_l2Power(13) = 4096m_l2Power(14) = 8192m_l2Power(15) = 16384m_l2Power(16) = 32768m_l2Power(17) = 65536m_l2Power(18) = 131072m_l2Power(19) = 262144m_l2Power(20) = 524288m_l2Power(21) = 1048576m_l2Power(22) = 2097152m_l2Power(23) = 4194304m_l2Power(24) = 8388608m_l2Power(25) = 16777216m_l2Power(26) = 33554432m_l2Power(27) = 67108864m_l2Power(28) = 134217728m_l2Power(29) = 268435456m_l2Power(30) = 536870912m_l2Power(31) = 1073741824S11 = 7S12 = 12S13 = 17S14 = 22S21 = 5S22 = 9S23 = 14S24 = 20S31 = 4S32 = 11S33 = 16S34 = 23S41 = 6S42 = 10S43 = 15S44 = 21*********Function ConvertToWordArray(sMessage)MODULUS_BITS = 512CONGRUENT_BITS = 448lMessageLength = LEN(sMessage)lNumberOfWords = (((lMessageLength + INT(INT((MODULUS_BITS - CONGRUENT_BITS) / BITS_TO_A_BYTE)) /INT (MODULUS_BITS / BITS_TO_A_BYTE)) )+ 1) * INT(MODULUS_BITS /BITS_TO_A_WORD)DIME lWordArray(lNumberOfWords )STORE 0 TO lWordArraylBytePosition = 0lByteCount = 0DO WHILE lByteCount < lMessageLengthlWordCount = INT(lByteCount / BYTES_TO_A_WORD)lBytePosition = MOD(lByteCount , BYTES_TO_A_WORD) * BITS_TO_A_BYTElWordArray(lWordCount+1) =BITOR( lWordArray(lWordCount+1) , LShift(ASC(SUBSTR(sMessage, lByteCount + 1, 1)), lBytePosition))lByteCount = lByteCount + 1ENDDOlWordCount = INT(lByteCount / BYTES_TO_A_WORD)lBytePosition = MOD(lByteCount , BYTES_TO_A_WORD) * BITS_TO_A_BYTElWordArray(lWordCount + 1) =BITOR( lWordArray(lWordCount + 1) , LShift(0x80, lBytePosition))lWordArray(lNumberOfWords - 1) = LShift(lMessageLength, 3)lWordArray(lNumberOfWords ) = RShift(lMessageLength, 29)***************************a = 0x67452301b = 0xEFCDAB89c = 0x98BADCFEd = 0x10325476DIME x(lNumberOfWords)FOR k = 1 TO lNumberOfWordsx(k)=lWordArray(k)ENDFOR*****设置错误处理程序,因Visual FoxPro程序本身所限,对大数的处理能力不够ON ERROR do err_treatFOR k = 1 TO lNumberOfWords STEP 16AA = aBB = bCC = cDD = dDO md5_FF WITH a, b, c, d, x(k + 0), S11, 0xD76AA478DO md5_FF WITH d, a, b, c, x(k + 1), S12, 0xE8C7B756DO md5_FF WITH c, d, a, b, x(k + 2), S13, 0x242070DBDO md5_FF WITH b, c, d, a, x(k + 3), S14, 0xC1BDCEEEDO md5_FF WITH a, b, c, d, x(k + 4), S11, 0xF57C0FAFDO md5_FF WITH d, a, b, c, x(k + 5), S12, 0x4787C62ADO md5_FF WITH c, d, a, b, x(k + 6), S13, 0xA8304613DO md5_FF WITH b, c, d, a, x(k + 7), S14, 0xFD469501DO md5_FF WITH a, b, c, d, x(k + 8), S11, 0x698098D8DO md5_FF WITH d, a, b, c, x(k + 9), S12, 0x8B44F7AFDO md5_FF WITH c, d, a, b, x(k + 10), S13, 0xFFFF5BB1DO md5_FF WITH b, c, d, a, x(k + 11), S14, 0x895CD7BEDO md5_FF WITH a, b, c, d, x(k + 12), S11, 0x6B901122DO md5_FF WITH d, a, b, c, x(k + 13), S12, 0xFD987193DO md5_FF WITH c, d, a, b, x(k + 14), S13, 0xA679438EDO md5_FF WITH b, c, d, a, x(k + 15), S14, 0x49B40821DO md5_GG WITH a, b, c, d, x(k + 1), S21, 0xF61E2562DO md5_GG WITH d, a, b, c, x(k + 6), S22, 0xC040B340DO md5_GG WITH c, d, a, b, x(k + 11), S23, 0x265E5A51DO md5_GG WITH b, c, d, a, x(k + 0), S24, 0xE9B6C7AADO md5_GG WITH a, b, c, d, x(k + 5), S21, 0xD62F105DDO md5_GG WITH d, a, b, c, x(k + 10), S22, 0x2441453DO md5_GG WITH c, d, a, b, x(k + 15), S23, 0xD8A1E681DO md5_GG WITH b, c, d, a, x(k + 4), S24, 0xE7D3FBC8DO md5_GG WITH a, b, c, d, x(k + 9), S21, 0x21E1CDE6DO md5_GG WITH d, a, b, c, x(k + 14), S22, 0xC33707D6DO md5_GG WITH c, d, a, b, x(k + 3), S23, 0xF4D50D87DO md5_GG WITH b, c, d, a, x(k + 8), S24, 0x455A14EDDO md5_GG WITH a, b, c, d, x(k + 13), S21, 0xA9E3E905DO md5_GG WITH d, a, b, c, x(k + 2), S22, 0xFCEFA3F8DO md5_GG WITH c, d, a, b, x(k + 7), S23, 0x676F02D9DO md5_GG WITH b, c, d, a, x(k + 12), S24, 0x8D2A4C8ADO md5_HH WITH a, b, c, d, x(k + 5), S31, 0xFFFA3942DO md5_HH WITH d, a, b, c, x(k + 8), S32, 0x8771F681DO md5_HH WITH c, d, a, b, x(k + 11), S33, 0x6D9D6122DO md5_HH WITH b, c, d, a, x(k + 14), S34, 0xFDE5380CDO md5_HH WITH a, b, c, d, x(k + 1), S31, 0xA4BEEA44DO md5_HH WITH d, a, b, c, x(k + 4), S32, 0x4BDECFA9DO md5_HH WITH c, d, a, b, x(k + 7), S33, 0xF6BB4B60DO md5_HH WITH b, c, d, a, x(k + 10), S34, 0xBEBFBC70DO md5_HH WITH a, b, c, d, x(k + 13), S31, 0x289B7EC6DO md5_HH WITH d, a, b, c, x(k + 0), S32, 0xEAA127FADO md5_HH WITH c, d, a, b, x(k + 3), S33, 0xD4EF3085DO md5_HH WITH b, c, d, a, x(k + 6), S34, 0x4881D05DO md5_HH WITH a, b, c, d, x(k + 9), S31, 0xD9D4D039DO md5_HH WITH d, a, b, c, x(k + 12), S32, 0xE6DB99E5DO md5_HH WITH c, d, a, b, x(k + 15), S33, 0x1FA27CF8DO md5_HH WITH b, c, d, a, x(k + 2), S34, 0xC4AC5665DO md5_II WITH a, b, c, d, x(k + 0), S41, 0xF4292244DO md5_II WITH d, a, b, c, x(k + 7), S42, 0x432AFF97DO md5_II WITH c, d, a, b, x(k + 14), S43, 0xAB9423A7DO md5_II WITH b, c, d, a, x(k + 5), S44, 0xFC93A039DO md5_II WITH a, b, c, d, x(k + 12), S41, 0x655B59C3DO md5_II WITH d, a, b, c, x(k + 3), S42, 0x8F0CCC92DO md5_II WITH c, d, a, b, x(k + 10), S43, 0xFFEFF47DDO md5_II WITH b, c, d, a, x(k + 1), S44, 0x85845DD1DO md5_II WITH a, b, c, d, x(k + 8), S41, 0x6FA87E4FDO md5_II WITH d, a, b, c, x(k + 15), S42, 0xFE2CE6E0DO md5_II WITH c, d, a, b, x(k + 6), S43, 0xA3014314DO md5_II WITH b, c, d, a, x(k + 13), S44, 0x4E0811A1DO md5_II WITH a, b, c, d, x(k + 4), S41, 0xF7537E82DO md5_II WITH d, a, b, c, x(k + 11), S42, 0xBD3AF235DO md5_II WITH c, d, a, b, x(k + 2), S43, 0x2AD7D2BBDO md5_II WITH b, c, d, a, x(k + 9), S44, 0xEB86D391a = AddUnsigned(a, AA)b = AddUnsigned(b, BB)c = AddUnsigned(c, CC)d = AddUnsigned(d, DD)ENDFORON ERROR &&恢复默认的错误处理*************下面是输出16位代码的方法************* * RETURN LOWER(WordToHex(a) + WordToHex(b) + WordToHex(c) + WordToHex(d))*************下面是输出8位代码的方法*************RETURN LOWER(WordToHex(b) + WordToHex(c)) ENDFUNCPROC err_treatRETURNENDPROC