我正在做一个C#项目,我必须把一些密码存储在一个文件中。我想用sha1加密那些。
我已经在一个C项目中有了加密/解密sha1程序。
我想从我的C项目中调用“password庘sha1庘u build”,这是一个C函数,它对外部密码“C”庘declspec(dllexport)char*password庘sha1庘u build(char*password))进行加密。
我从有“Password SHA1;1_Build”函数的文件中构建了一个dll,并尝试从我的C#项目中调用它,如下所示:

[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中,如下所示:
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,after是hash
但是当C程序执行他的密码检查时,他告诉我输入的密码和存储的密码不一样。
我有一个弱的密码和繁忙的其他作品,所以我只是问自己:“为什么不使用”PasWordsHa1Bug构建“函数已经存在和工作”,所以这就是为什么我试图调用这个函数从我的C程序。
我希望这是清楚的,对不起,我的英语很差。
对里德·科西:谢谢,我试过了,至少Pinvoke异常被排除了,但现在我得到了一个“访问违反异常”。。
所以我们欢迎更多的想法再次感谢!

最佳答案

这可能是由于调用约定不匹配造成的。PInvoke默认使用StdCall,但VC编译器默认使用CDecl
尝试更改为:

[DllImport("Pass.dll", CallingConvention=CallingConvention.Cdecl)]

其他潜在的问题是字符串封送处理技术。您可能需要指定要使用的编码,和/或作为第二个参数传递,并使用它来“填充”结果。

08-24 19:27
查看更多