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 characters 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.
            Asked
            
        
        
            Active
            
        
            Viewed 639 times
        
    2 Answers
2
            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));
}
 
    
    
        Argbart
        
- 73
- 5
0
            
            
        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.
- 
                    you are not familiar with UNIX Crypt() function i guess. plz google about it and then answer if you can. – vijay Jul 12 '11 at 11:03
 
     
    