算术运算导致溢出 - 河豚算法

时间:2014-06-26 06:36:54

标签: vb.net blowfish

执行此代码时:

Dim bytearray() As Byte = System.IO.File.ReadAllBytes("C:\Users\Fery Ferdiansyah\Desktop\asd\asd.txt")
Dim key() As Byte = System.Text.Encoding.UTF8.GetBytes("swagger")
Dim bf As New blowfish2(key)

此函数中的此语法y = S(0, a) + S(1, b)会导致算术溢出:

Private Function F(ByVal x As UInteger) As UInteger
    Dim a As UShort
    Dim b As UShort
    Dim c As UShort
    Dim d As UShort
    Dim y As UInteger

    d = CUShort((x And &HFF))
    x >>= 8
    c = CUShort((x And &HFF))
    x >>= 8
    b = CUShort((x And &HFF))
    x >>= 8
    a = CUShort((x And &HFF))
    y = S(0, a) + S(1, b)
    y = y Xor S(2, c)
    y = y + S(3, d)

    Return y
End Function

有人可以帮我解决这个问题吗?

2 个答案:

答案 0 :(得分:0)

您可以禁用整数溢出检查(Advanced Compiler Settings Dialog Box (Visual Basic))或将y声明为UInt64然后

Return CUInt(y And &HFFFFFFFF)

答案 1 :(得分:0)

添加两个整数(Int32)的结果必须存储到Int64中,以确保结果可以存储而不会溢出。

因此,在这种情况下,您的方法应该返回UInt64,y也应该是UInt64

相关问题