用户登录  |  用户注册
首 页商业源码原创产品编程论坛
当前位置:PB创新网文章中心编程技巧VisualBasic

用VB实现DES加解密算法(二)(2)

减小字体 增大字体 作者:佚名  来源:本站整理  发布时间:2009-03-16 19:54:36

 
   
    For i = 0 To 31
        ''P变换
        CodeP(i) = RetS(P(i))
       
        ''产生L11,R11
        R11(i) = L10(i) Xor CodeP(i)
        L11(i) = R10(i)
    Next
   
   
    ''进行第12次迭代
    For i = 0 To 47
        CodeE(i) = R11(E(i))                 ''经过E变换扩充,由32位变为48位
        CodeE(i) = CodeE(i) Xor K12(i)        ''与K12按位作不进位加法运算
    Next
   
    ''分8组
    For i = 0 To 5
        CodeS1(i) = CodeE(i)
        CodeS2(i) = CodeE(i + 6)
        CodeS3(i) = CodeE(i + 12)
        CodeS4(i) = CodeE(i + 18)
        CodeS5(i) = CodeE(i + 24)
        CodeS6(i) = CodeE(i + 30)
        CodeS7(i) = CodeE(i + 36)
        CodeS8(i) = CodeE(i + 42)
    Next
   
    ''S盒运算,得到8个数
   
        S(0) = S1(CodeS1(5) + CodeS1(0) * 2, CodeS1(4) + CodeS1(3) * 2 + CodeS1(2) * 4 + CodeS1(1) * 8)
        S(1) = S2(CodeS2(5) + CodeS2(0) * 2, CodeS2(4) + CodeS2(3) * 2 + CodeS2(2) * 4 + CodeS2(1) * 8)
        S(2) = S3(CodeS3(5) + CodeS3(0) * 2, CodeS3(4) + CodeS3(3) * 2 + CodeS3(2) * 4 + CodeS3(1) * 8)
        S(3) = S4(CodeS4(5) + CodeS4(0) * 2, CodeS4(4) + CodeS4(3) * 2 + CodeS4(2) * 4 + CodeS4(1) * 8)
        S(4) = S5(CodeS5(5) + CodeS5(0) * 2, CodeS5(4) + CodeS5(3) * 2 + CodeS5(2) * 4 + CodeS5(1) * 8)
        S(5) = S6(CodeS6(5) + CodeS6(0) * 2, CodeS6(4) + CodeS6(3) * 2 + CodeS6(2) * 4 + CodeS6(1) * 8)
        S(6) = S7(CodeS7(5) + CodeS7(0) * 2, CodeS7(4) + CodeS7(3) * 2 + CodeS7(2) * 4 + CodeS7(1) * 8)
        S(7) = S8(CodeS8(5) + CodeS8(0) * 2, CodeS8(4) + CodeS8(3) * 2 + CodeS8(2) * 4 + CodeS8(1) * 8)
   
    ''S盒运算32位结果
    For i = 0 To 7
        RetS(i * 4 + 0) = (S(i) And &H8) \ &H8
        RetS(i * 4 + 1) = (S(i) And &H4) \ &H4
        RetS(i * 4 + 2) = (S(i) And &H2) \ &H2
        RetS(i * 4 + 3) = (S(i) And &H1) \ &H1
    Next
   
   
    For i = 0 To 31
        ''P变换
        CodeP(i) = RetS(P(i))
       
        ''产生L12,R12
        R12(i) = L11(i) Xor CodeP(i)
        L12(i) = R11(i)
    Next
   
    ''进行第13次迭代
    For i = 0 To 47
        CodeE(i) = R12(E(i))                 ''经过E变换扩充,由32位变为48位
        CodeE(i) = CodeE(i) Xor K13(i)        ''与K13按位作不进位加法运算
    Next
   
    ''分8组
    For i = 0 To 5
        CodeS1(i) = CodeE(i)
        CodeS2(i) = CodeE(i + 6)
        CodeS3(i) = CodeE(i + 12)
        CodeS4(i) = CodeE(i + 18)
        CodeS5(i) = CodeE(i + 24)
        CodeS6(i) = CodeE(i + 30)
        CodeS7(i) = CodeE(i + 36)
        CodeS8(i) = CodeE(i + 42)
    Next
   
    ''S盒运算,得到8个数
   
        S(0) = S1(CodeS1(5) + CodeS1(0) * 2, CodeS1(4) + CodeS1(3) * 2 + CodeS1(2) * 4 + CodeS1(1) * 8)
        S(1) = S2(CodeS2(5) + CodeS2(0) * 2, CodeS2(4) + CodeS2(3) * 2 + CodeS2(2) * 4 + CodeS2(1) * 8)
        S(2) = S3(CodeS3(5) + CodeS3(0) * 2, CodeS3(4) + CodeS3(3) * 2 + CodeS3(2) * 4 + CodeS3(1) * 8)
        S(3) = S4(CodeS4(5) + CodeS4(0) * 2, CodeS4(4) + CodeS4(3) * 2 + CodeS4(2) * 4 + CodeS4(1) * 8)
        S(4) = S5(CodeS5(5) + CodeS5(0) * 2, CodeS5(4) + CodeS5(3) * 2 + CodeS5(2) * 4 + CodeS5(1) * 8)
        S(5) = S6(CodeS6(5) + CodeS6(0) * 2, CodeS6(4) + CodeS6(3) * 2 + CodeS6(2) * 4 + CodeS6(1) * 8)
        S(6) = S7(CodeS7(5) + CodeS7(0) * 2, CodeS7(4) + CodeS7(3) * 2 + CodeS7(2) * 4 + CodeS7(1) * 8)
        S(7) = S8(CodeS8(5) + CodeS8(0) * 2, CodeS8(4) + CodeS8(3) * 2 + CodeS8(2) * 4 + CodeS8(1) * 8)
   
    ''S盒运算32位结果
    For i = 0 To 7
        RetS(i * 4 + 0) = (S(i) And &H8) \ &H8
        RetS(i * 4 + 1) = (S(i) And &H4) \ &H4
        RetS(i * 4 + 2) = (S(i) And &H2) \ &H2
        RetS(i * 4 + 3) = (S(i) And &H1) \ &H1
    Next
   
   
    For i = 0 To 31
        ''P变换
        CodeP(i) = RetS(P(i))
       
        ''产生L13,R13
        R13(i) = L12(i) Xor CodeP(i)
        L13(i) = R12(i)
    Next
   
   
    ''进行第14次迭代
    For i = 0 To 47
        CodeE(i) = R13(E(i))                 ''经过E变换扩充,由32位变为48位
        CodeE(i) = CodeE(i) Xor K14(i)        ''与K14按位作不进位加法运算
    Next
   
    ''分8组
    For i = 0 To 5
        CodeS1(i) = CodeE(i)
        CodeS2(i) = CodeE(i + 6)
        CodeS3(i) = CodeE(i + 12)
        CodeS4(i) = CodeE(i + 18)
        CodeS5(i) = CodeE(i + 24)
        CodeS6(i) = CodeE(i + 30)
        CodeS7(i) = CodeE(i + 36)
        CodeS8(i) = CodeE(i + 42)
    Next
   
    ''S盒运算,得到8个数
   
        S(0) = S1(CodeS1(5) + CodeS1(0) * 2, CodeS1(4) + CodeS1(3) * 2 + CodeS1(2) * 4 + CodeS1(1) * 8)
        S(1) = S2(CodeS2(5) + CodeS2(0) * 2, CodeS2(4) + CodeS2(3) * 2 + CodeS2(2) * 4 + CodeS2(1) * 8)
        S(2) = S3(CodeS3(5) + CodeS3(0) * 2, CodeS3(4) + CodeS3(3) * 2 + CodeS3(2) * 4 + CodeS3(1) * 8)
        S(3) = S4(CodeS4(5) + CodeS4(0) * 2, CodeS4(4) + CodeS4(3) * 2 + CodeS4(2) * 4 + CodeS4(1) * 8)
        S(4) = S5(CodeS5(5) + CodeS5(0) * 2, CodeS5(4) + CodeS5(3) * 2 + CodeS5(2) * 4 + CodeS5(1) * 8)
        S(5) = S6(CodeS6(5) + CodeS6(0) * 2, CodeS6(4) + CodeS6(3) * 2 + CodeS6(2) * 4 + CodeS6(1) * 8)
        S(6) = S7(CodeS7(5) + CodeS7(0) * 2, CodeS7(4) + CodeS7(3) * 2 + CodeS7(2) * 4 + CodeS7(1) * 8)
        S(7) = S8(CodeS8(5) + CodeS8(0) * 2, CodeS8(4) + CodeS8(3) * 2 + CodeS8(2) * 4 + CodeS8(1) * 8)
   
    ''S盒运算32位结果
    For i = 0 To 7
        RetS(i * 4 + 0) = (S(i) And &H8) \ &H8
        RetS(i * 4 + 1) = (S(i) And &H4) \ &H4
        RetS(i * 4 + 2) = (S(i) And &H2) \ &H2
        RetS(i * 4 + 3) = (S(i) And &H1) \ &H1
    Next
   
   
    For i = 0 To 31
        ''P变换
        CodeP(i) = RetS(P(i))
       
        ''产生L14,R14
        R14(i) = L13(i) Xor CodeP(i)
        L14(i) = R13(i)
    Next
   
   
    ''进行第15次迭代
    For i = 0 To 47
        CodeE(i) = R14(E(i))                 ''经过E变换扩充,由32位变为48位
        CodeE(i) = CodeE(i) Xor K15(i)        ''与K15按位作不进位加法运算
    Next
   
    ''分8组
    For i = 0 To 5
        CodeS1(i) = CodeE(i)
        CodeS2(i) = CodeE(i + 6)
        CodeS3(i) = CodeE(i + 12)
        CodeS4(i) = CodeE(i + 18)
        CodeS5(i) = CodeE(i + 24)
        CodeS6(i) = CodeE(i + 30)
        CodeS7(i) = CodeE(i + 36)
        CodeS8(i) = CodeE(i + 42)
    Next
   
    ''S盒运算,得到8个数
   
        S(0) = S1(CodeS1(5) + CodeS1(0) * 2, CodeS1(4) + CodeS1(3) * 2 + CodeS1(2) * 4 + CodeS1(1) * 8)
        S(1) = S2(CodeS2(5) + CodeS2(0) * 2, CodeS2(4) + CodeS2(3) * 2 + CodeS2(2) * 4 + CodeS2(1) * 8)
        S(2) = S3(CodeS3(5) + CodeS3(0) * 2, CodeS3(4) + CodeS3(3) * 2 + CodeS3(2) * 4 + CodeS3(1) * 8)
        S(3) = S4(CodeS4(5) + CodeS4(0) * 2, CodeS4(4) + CodeS4(3) * 2 + CodeS4(2) * 4 + CodeS4(1) * 8)
        S(4) = S5(CodeS5(5) + CodeS5(0) * 2, CodeS5(4) + CodeS5(3) * 2 + CodeS5(2) * 4 + CodeS5(1) * 8)
        S(5) = S6(CodeS6(5) + CodeS6(0) * 2, CodeS6(4) + CodeS6(3) * 2 + CodeS6(2) * 4 + CodeS6(1) * 8)
        S(6) = S7(CodeS7(5) + CodeS7(0) * 2, CodeS7(4) + CodeS7(3) * 2 + CodeS7(2) * 4 + CodeS7(1) * 8)
        S(7) = S8(CodeS8(5) + CodeS8(0) * 2, CodeS8(4) + CodeS8(3) * 2 + CodeS8(2) * 4 + CodeS8(1) * 8)
   
    ''S盒运算32位结果
    For i = 0 To 7
        RetS(i * 4 + 0) = (S(i) And &H8) \ &H8
        RetS(i * 4 + 1) = (S(i) And &H4) \ &H4
        RetS(i * 4 + 2) = (S(i) And &H2) \ &H2
        RetS(i * 4 + 3) = (S(i) And &H1) \ &H1
    Next
   
   
    For i = 0 To 31
        ''P变换
        CodeP(i) = RetS(P(i))
       
        ''产生L15,R15
        R15(i) = L14(i) Xor CodeP(i)
        L15(i) = R14(i)
    Next
   
    ''进行第16次迭代
    For i = 0 To 47
        CodeE(i) = R15(E(i))                 ''经过E变换扩充,由32位变为48位
        CodeE(i) = CodeE(i) Xor K16(i)        ''与K16按位作?

Tags:

作者:佚名

文章评论评论内容只代表网友观点,与本站立场无关!

   评论摘要(共 0 条,得分 0 分,平均 0 分) 查看完整评论
PB创新网ourmis.com】Copyright © 2000-2009 . All Rights Reserved .
页面执行时间:26,031.25000 毫秒
Email:ourmis@126.com QQ:2322888 蜀ICP备05006790号