开发者

How to call Crypt() function mono C#?

i'm in a need of using the unix Crypt(salt, key) function in c# code for encrypting my password using a two character salt and 8 characte开发者_Go百科rs key. In C/C++, we can do so but how to do this in C#. I'm using Mono C# in ubuntu linux. Please suggest.


Here's the complete working program which uses unix crypt:

using System;
using System.Runtime.InteropServices;

public class Test
{   
    [DllImport("libcrypt.so", EntryPoint = "crypt", ExactSpelling = true, CharSet = CharSet.Ansi)]
    public static extern IntPtr UnixCrypt([MarshalAs(UnmanagedType.LPStr)]string key, [MarshalAs(UnmanagedType.LPStr)]string salt); 

    public static void Main()
    {
        var ptrResult = UnixCrypt("test", "test2");
        Console.WriteLine(Marshal.PtrToStringAnsi(ptrResult));
    }
}

It prints result of crypting with given key and salt. Of course you can put UnixCrypt in any other class. For convenience you can also create method:

public static string MyCrypt(string key, string salt)
{
    return Marshal.PtrToStringAnsi(UnixCrypt(key, salt));
}


There is an similar question- Encrypt and decrypt a string

There is an article on Code Project, it teaches how you can create an helper class.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜