本文讲述了从C#项目调用c本机函数!具有很好的参考价值,希望对大家有所帮助。一起跟随六星小编过来看看吧,具体如下:
我正在做一个C项目,在这个项目中,我必须将一些密码存储在一个文件中。我想用sha1加密那些。我已经在一个C项目中加密/解密了sha1程序。我想从我的C_项目中调用"password_sha1_build",这是一个C函数,用于加密密码extern"c"uudeclspec(dllexport)char*password_sha1_build(char*password))。
我从"password-sha1-u build"函数所在的文件构建了一个dll,并尝试从我的C项目调用它,如下所示:
1 2 3 4 5 6 7 8 | [DllImport("Pass.dll")] public static extern string Password_SHA1_Build(string Password); ... string password ="iamlee"; string hashedpwd =""; hashedpwd = Password_SHA1_Build(password); |
我得到一个(法语)关于pinvoke与签名不匹配的错误…不平衡。我想这件事可能是因为我使用的是string/char*,但如果是这样的话,我可以处理吗?…
我注意到我的主题不够清楚。有一个C程序用作"服务器",它将读取名为infos.cfg的文件,其中存储在C程序中写入的加密密码。C程序不是我自己开发的,但它包含一个名为"int password_sha1_check(char*password,char*hash)"的函数,该函数可以读取由"char*password_sha1_build(char*password)"函数生成的sha1密码。
所以起初我尝试用sha1将密码存储在c中,如下所示:
1 2 3 4 5 6 7 8 9 10 | System.Security.Cryptography.SHA1 hash = System.Security.Cryptography.SHA1.Create(); System.Text.ASCIIEncoding encoder = newS ystem.Text.ASCIIEncoding(); byte[] combined = encoder.GetBytes(password); hash.ComputeHash(combined); //rethash = Convert.ToBase64String(hash.Hash); System.Security.Cryptography.SHA1CryptoServiceProvider sha1 = new System.Security.Cryptography.SHA1CryptoServiceProvider(); byte[] hash = sha1.ComputeHash(System.Text.Encoding.ASCII.GetBytes(password)); string delimitedHexHash = BitConverter.ToString(hash); string hexHash = delimitedHexHash.Replace("-",""); rethash = hexHash; |
密码像这样存储在一个文件中:"password:sha1:576295.49880ab837e9179dd68dfb142342f368e821de43"在"."之前是salt,之后是hash
但是当C程序执行他的密码检查时,他告诉我输入的密码与存储的密码不同。我在加密方面有点弱,其他工作也很忙,所以我问自己"为什么不使用已经存在并起作用的"password-sha1-u build"函数",这就是为什么我试图从我的C程序调用这个函数。我希望这是清楚的,对不起我的英语很差。
感谢ReedCopsey:我试过了,至少Pinvoke异常被排除了,但是现在我得到了一个"accessViolationException"。
更多相关技术内容咨询欢迎前往并持续关注六星社区了解详情。
想高效系统的学习Python编程语言,推荐大家关注一个微信公众号:Python编程学习圈。每天分享行业资讯、技术干货供大家阅读,关注即可免费领取整套Python入门到进阶的学习资料以及教程,感兴趣的小伙伴赶紧行动起来吧。
如果觉得我的文章对您有用,请随意打赏。你的支持将鼓励我继续创作!