在64位计算机上运行vb.net应用程序时发生异常 [英] Exception While running vb.net application on 64bit machine

查看:120
本文介绍了在64位计算机上运行vb.net应用程序时发生异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下功能接受加密数据,如加密格式
在cs.close语句的Final块中发生错误.

.Net框架使用的:3.5.1

请帮忙.


错误:
运行时遇到致命错误.错误的地址在线程0xbb0上的地址为0x7fa7a3ab.错误代码是0xc0000005.此错误可能是CLR或用户代码的不安全或不可验证部分中的错误.该错误的常见来源包括COM-interop或PInvoke的用户封送处理错误,这些错误可能会破坏堆栈.


功能:

Following function acceptes encryptedData as in encrypted format
and error occures in Finally block at cs.close Statement.

.Net framework used :3.5.1

Please help.


Error:
The runtime has encountered a fatal error. The address of the error was at 0x7fa7a3ab, on thread 0xbb0. ?The error code is 0xc0000005. This error may be a bug in the CLR or in the unsafe or non-verifiable?portions of user code. Common sources of this bug include user marshaling errors for COM-interop or PInvoke,which may corrupt the stack.


Fuction:

Imports System.Security.Cryptography

Public Function Decrypt(ByVal encryptedData As Data) As Data
    Try
        Dim ms As New System.IO.MemoryStream(encryptedData.Bytes, 0,encryptedData.Bytes.Length)
        Dim b() As Byte = New Byte(encryptedData.Bytes.Length - 1) {}
        ValidateKeyAndIv(False)
        Dim cs As New CryptoStream(ms, _crypto.CreateDecryptor(), CryptoStreamMode.Read)
        Try
            cs.Read(b, 0, encryptedData.Bytes.Length - 1)
        Catch ex As CryptographicException
            Throw New CryptographicException("Unable to decrypt data. The provided key may be invalid.", ex)
        Finally                  
            cs.Close()           
        End Try
        Return New Data(b)
    Catch ex As Exception
        clsLogging.WriteToFileLog(sErrLogFileNM, ex.Message, ex.StackTrace, "")
    End Try 
End Function

推荐答案

您的代码段中缺少一些信息,即"ValidateKeyAndIv"的实现以及"_crypto"是什么以及如何创建和设置它.

根本不需要内部的Try,我将致力于使用更有意义的变量名.一个字母变量的日子早已一去不复返了.另外,名称"Data"是一个糟糕的类型名称.您可能想称它为更有意义的东西.

我可能会重写这小段代码来阅读:
There are pieces of information missing from your code snippet, namely, the implementation of "ValidateKeyAndIv" and what "_crypto" is and how it was created and setup.

Your inner Try is not needed at all and I''d work on using more meaningful variable names. The days of one letter variables are long gone. Also, the name "Data" is a terrible type name. You may want to call it something more meaningful.

I''d probably rewrite this little piece of code to read:
'' Validate encryptedData is an actual instance first, then
'' make sure it contains usable data here.
If ... Then
End If

'' Allocate a buffer to hold decrypted data
Dim buffer(encryptedData.Length - 1) As Byte

Try
    Using sourceStream As New MemoryStream(encryptedData.Bytes, 0, encryptedData.Bytes.Length - 1)
        Using cryptStream As New CryptoStream(sourceStream, Nothing, CryptoStreamMode.Read)
            cryptStream.Read(buffer, 0, encryptedData.Bytes.Length - 1)
        End Using
    End Using
Catch ex As Exception
    clsLogging.WriteToFileLog(sErrLogFileNM, ex.Message, ex.StackTrace, "")
Finally
    Return New Data(buffer)
End Try


这篇关于在64位计算机上运行vb.net应用程序时发生异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆