检查有效的IMEI

时间:2010-03-25 14:59:32

标签: .net vb.net algorithm imei

有人知道如何检查有效的IMEI吗?

我找到了一个检查此页面的功能:http://www.dotnetfunda.com/articles/article597-imeivalidator-in-vbnet-.aspx

但它返回false表示有效的IMEI(f.e。352972024585360)。 我可以在此页面上在线验证:http://www.numberingplans.com/?page=analysis&sub=imeinr

检查给定IMEI是否有效的正确方法(在VB.Net中)是什么?

PS: 上页中的此功能必须以某种方式不正确:

Public Shared Function isImeiValid(ByVal IMEI As String) As Boolean
    Dim cnt As Integer = 0
    Dim nw As String = String.Empty
    Try
        For Each c As Char In IMEI
            cnt += 1
            If cnt Mod 2 <> 0 Then
                nw += c
            Else
                Dim d As Integer = Integer.Parse(c) * 2 ' Every Second Digit has to be Doubled '
                nw += d.ToString() ' Genegrated a new number with doubled digits '
            End If
        Next
        Dim tot As Integer = 0
        For Each ch As Char In nw.Remove(nw.Length - 1, 1)
            tot += Integer.Parse(ch) ' Adding all digits together '
        Next
        Dim chDigit As Integer = 10 - (tot Mod 10) ' Finding the Check Digit my Finding the Remainder of the sum and subtracting it from 10 '
        If chDigit = Integer.Parse(IMEI(IMEI.Length - 1)) Then ' Checking the Check Digit with the last digit of the Given IMEI code '
            Return True
        Else
            Return False
        End If
    Catch ex As Exception
        Return False
    End Try
End Function

编辑:这是我的工作“checkIMEI” - 功能:

Public Shared Function checkIMEI(ByRef IMEI As String) As Boolean
    Const allowed As String = "0123456789"

    Dim cleanNumber As New System.Text.StringBuilder
    For i As Int32 = 0 To IMEI.Length - 1
        If (allowed.IndexOf(IMEI.Substring(i, 1)) >= 0) Then
            cleanNumber.Append(IMEI.Substring(i, 1))
        End If
    Next

    If cleanNumber.Length <> 15 Then
        Return False
    Else
        IMEI = cleanNumber.ToString
    End If

    For i As Int32 = cleanNumber.Length + 1 To 16
        cleanNumber.Insert(0, "0")
    Next

    Dim multiplier As Int32, digit As Int32, sum As Int32, total As Int32 = 0
    Dim number As String = cleanNumber.ToString()

    For i As Int32 = 1 To 16
        multiplier = 1 + (i Mod 2)
        digit = Int32.Parse(number.Substring(i - 1, 1))
        sum = digit * multiplier
        If (sum > 9) Then
            sum -= 9
        End If
        total += sum
    Next

    Return (total Mod 10 = 0)
End Function

2 个答案:

答案 0 :(得分:10)

使用Luhn算法验证IMEI号码。链接页面具有各种语言的实现。 This post还有一些关于如何解决Luhn算法的实现和一般方法。

答案 1 :(得分:1)

IMEISV(IMEI软件版本)没有Luhn算法校验位。相反,它的软件版本号用两位数表示。 IMEI号码的格式已经发展了几年。

我认为

Wikipedia是逐年查看格式变化的好来源。如果您查看新的IMEI和IMEISV版本格式,则会看到最终组装代码(FAC)已删除,类型分配代码(TAC)增加了8位。互联网上有free个商业TAC列表。

它可以通过在Luhn算法验证的顶部查找TAC列表中的新的和旧的IMEI编号来验证TAC编号。对于旧的IMEI号码,应舍弃2位数的FAC,而应对6位数进行TAC验证。