converting python to c# .net

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ogtheterror
    New Member
    • Mar 2009
    • 2

    converting python to c# .net

    Hi I have a very limited understanding of Python and have given this the best shot i have but still have not been able to get it working.
    Is there anyone that knows how to get this into a .net assembly?

    Code:
    _________________________________________________________
    def encrypt_password(challenge, password):
        if challenge['algorithm'] == 'md5':
            inner = md5(challenge['salt'] + password).hexdigest()
        elif challenge['algorithm'] == 'sha1':
            inner = sha(challenge['salt'] + password).hexdigest()
        else:
            raise NotImplementedError(
                "Password encryption algorithm '%s' not implemented." %
                challenge['algorithm'])
        return md5(inner + challenge['nonce']).hexdigest()
    _________________________________________________________
    This is my attempt at this:

    Code:
    _________________________________________________________
    
            public static String GetEncryptedPassword(String password, String username)
            {
                INoncesChallengeUser proxyNoncesChallengeUser = XmlRpcProxyGen.Create<INoncesChallengeUser>();
                XmlRpcStruct NoncesChallengeUser = proxyNoncesChallengeUser.GetNoncesChallengeUser(username);
    
                StringBuilder s = new StringBuilder();
                String nonce = (String)NoncesChallengeUser["nonce"];
                String salt = (String)NoncesChallengeUser["salt"];
                String algorithm = (String)NoncesChallengeUser["algorithm"];
    
                byte[] nonceBytes = new byte[nonce.Length];
                byte[] saltBytes = new byte[salt.Length];
                byte[] passwordBytes = new byte[password.Length];
                byte[] algorithmBytes = new byte[salt.Length + password.Length];
                byte[] InnerBytes;
                
                saltBytes = Encoding.UTF8.GetBytes(salt);
                passwordBytes = Encoding.UTF8.GetBytes(password);
                nonceBytes = Encoding.UTF8.GetBytes(nonce);
    
                //Copy password bytes into resulting array
                for (int i = 0; i < passwordBytes.Length; i++)
                    algorithmBytes[i] = passwordBytes[i];
    
                //Append salt bytes to the resulting array.
                for (int i = 0; i < saltBytes.Length; i++)
                    algorithmBytes[saltBytes.Length + i] = saltBytes[i];
    
                switch (algorithm.ToUpper())
                {
                    case "SHA1":
                        //String encrypted_password = md5(sha1(salt + password) + nonce);
                        SHA1CryptoServiceProvider sha1 = new SHA1CryptoServiceProvider();
                        InnerBytes = sha1.ComputeHash(algorithmBytes);
                        break;
    
                    default: //MD5
                        //String encrypted_password = md5(sha1(salt + password) + nonce);
                        MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
                        InnerBytes = md5.ComputeHash(algorithmBytes);
                        break;
                }
    
                //Create finalEncryptedPassword array
                byte[] encryptedPasswordBytes = new byte[InnerBytes.Length + nonceBytes.Length];
    
                //Copy inner bytes into resulting array
                for (int i = 0; i < InnerBytes.Length; i++)
                    encryptedPasswordBytes[i] = InnerBytes[i];
    
                //Append nonce bytes to the resulting array.
                for (int i = 0; i < nonceBytes.Length; i++)
                    encryptedPasswordBytes[InnerBytes.Length + i] = nonceBytes[i];
    
                //Now do the final md5 Encryption
                byte[] finalEncryptedPasswordBytes = new byte[32];
                MD5CryptoServiceProvider md5Final = new MD5CryptoServiceProvider();
                finalEncryptedPasswordBytes = md5Final.ComputeHash(encryptedPasswordBytes);
    
                foreach (byte b in finalEncryptedPasswordBytes)
                {
                    s.Append(b.ToString("x2").ToLower());
                }
                return s.ToString();
            }
    _________________________________________________________
    Last edited by Frinavale; Mar 13 '09, 06:39 PM. Reason: Moved to C# forum from phython. Bvdet add code tags
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    Looks like the long way around to me.
    If all you're looking to do is encode a password or some other text maybe this will help get you on your way.

    Code:
    using System.Security;
    using System.Security.Cryptography;
    using System.Globalization;
    
            public static string EncodePassword(string originalPassword)
            {
                //Declarations
                Byte[] originalBytes;
                Byte[] encodedBytes;
                MD5 md5;
    
                //Instantiate MD5CryptoServiceProvider, get bytes for original password and compute hash (encoded password)
                md5 = new MD5CryptoServiceProvider();
                originalBytes = ASCIIEncoding.Default.GetBytes(originalPassword);
                encodedBytes = md5.ComputeHash(originalBytes);
    
                //Convert encoded bytes back to a 'readable' string
                return BitConverter.ToString(encodedBytes);
            }

    Comment

    • ogtheterror
      New Member
      • Mar 2009
      • 2

      #3
      Thanks for your speedy response, I've revised the code and yes its way neater but i still don't seem to be getting the correct response.

      Code:
              public static String GetEncryptedPassword(String password, String username)
              {
                  INoncesChallengeUser proxyNoncesChallengeUser = XmlRpcProxyGen.Create<INoncesChallengeUser>();
                  XmlRpcStruct NoncesChallengeUser = proxyNoncesChallengeUser.GetNoncesChallengeUser(username);
      
                  String nonce = (String)NoncesChallengeUser["nonce"];
                  String salt = (String)NoncesChallengeUser["salt"];
                  String algorithm = (String)NoncesChallengeUser["algorithm"];
                  String inner;
                  String final;
      
                  //encrypted_password = md5(sha1(salt + password) + nonce);
                  if (algorithm.ToUpper() == "SHA1")
                      inner = Sha1EncodePassword(salt + password);
                  else
                      inner = Md5EncodePassword(salt + password);
      
                  final = Md5EncodePassword(inner + nonce);
                  return final;
              }
      
              private static string Md5EncodePassword(string originalPassword)
              {
                  //Declarations
                  Byte[] originalBytes;
                  Byte[] encodedBytes;
                  MD5 md5;
      
                  //Instantiate MD5CryptoServiceProvider, get bytes for original password and compute hash (encoded password)
                  md5 = new MD5CryptoServiceProvider();
                  originalBytes = ASCIIEncoding.Default.GetBytes(originalPassword);
                  encodedBytes = md5.ComputeHash(originalBytes);
      
                  //Convert encoded bytes back to a 'readable' string
                  return BitConverter.ToString(encodedBytes).ToLower();
              }
      
              private static string Sha1EncodePassword(string originalPassword)
              {
                  //Declarations
                  Byte[] originalBytes;
                  Byte[] encodedBytes;
                  SHA1 sha1;
      
                  //Instantiate MD5CryptoServiceProvider, get bytes for original password and compute hash (encoded password)
                  sha1 = new SHA1CryptoServiceProvider();
                  originalBytes = ASCIIEncoding.Default.GetBytes(originalPassword);
                  encodedBytes = sha1.ComputeHash(originalBytes);
      
                  //Convert encoded bytes back to a 'readable' string
                  return BitConverter.ToString(encodedBytes).ToLower();
              }
      Last edited by bvdet; Mar 13 '09, 03:58 PM. Reason: Add code tags

      Comment

      • bvdet
        Recognized Expert Specialist
        • Oct 2006
        • 2851

        #4
        ogtheterror

        In the future, please use code tags when posting code.

        posting guidelines

        -BV
        Moderator

        Comment

        • tlhintoq
          Recognized Expert Specialist
          • Mar 2008
          • 3532

          #5
          Thanks for your speedy response, I've revised the code and yes its way neater but i still don't seem to be getting the correct response.
          Feel free to expand on that... What *are* you getting and in what way is it not what you were expecting? Are you getting errors, crashes, or just not what you thought you would get and how?

          Comment

          • toanmh
            New Member
            • Jun 2010
            • 4

            #6
            Help me convert Python to C# code form

            Hi all!
            I'm looking in google found some code in python to unwrap package in oracle. But I don't know it.Some one help me to convert it to C# or Java. Please.
            Thanks.!

            Code:
            import re
            import base64
            import zlib
            import sys
            
            # simple substitution table
            charmap = [0x3d, 0x65, 0x85, 0xb3, 0x18, 0xdb, 0xe2, 0x87, 0xf1, 0x52, 0xab, 0x63, 0x4b, 0xb5, 0xa0, 0x5f, 0x7d, 0x68, 0x7b, 0x9b, 0x24, 0xc2, 0x28, 0x67, 0x8a, 0xde, 0xa4, 0x26, 0x1e, 0x03, 0xeb, 0x17, 0x6f, 0x34, 0x3e, 0x7a, 0x3f, 0xd2, 0xa9, 0x6a, 0x0f, 0xe9, 0x35, 0x56, 0x1f, 0xb1, 0x4d, 0x10, 0x78, 0xd9, 0x75, 0xf6, 0xbc, 0x41, 0x04, 0x81, 0x61, 0x06, 0xf9, 0xad, 0xd6, 0xd5, 0x29, 0x7e, 0x86, 0x9e, 0x79, 0xe5, 0x05, 0xba, 0x84, 0xcc, 0x6e, 0x27, 0x8e, 0xb0, 0x5d, 0xa8, 0xf3, 0x9f, 0xd0, 0xa2, 0x71, 0xb8, 0x58, 0xdd, 0x2c, 0x38, 0x99, 0x4c, 0x48, 0x07, 0x55, 0xe4, 0x53, 0x8c, 0x46, 0xb6, 0x2d, 0xa5, 0xaf, 0x32, 0x22, 0x40, 0xdc, 0x50, 0xc3, 0xa1, 0x25, 0x8b, 0x9c, 0x16, 0x60, 0x5c, 0xcf, 0xfd, 0x0c, 0x98, 0x1c, 0xd4, 0x37, 0x6d, 0x3c, 0x3a, 0x30, 0xe8, 0x6c, 0x31, 0x47, 0xf5, 0x33, 0xda, 0x43, 0xc8, 0xe3, 0x5e, 0x19, 0x94, 0xec, 0xe6, 0xa3, 0x95, 0x14, 0xe0, 0x9d, 0x64, 0xfa, 0x59, 0x15, 0xc5, 0x2f, 0xca, 0xbb, 0x0b, 0xdf, 0xf2, 0x97, 0xbf, 0x0a, 0x76, 0xb4, 0x49, 0x44, 0x5a, 0x1d, 0xf0, 0x00, 0x96, 0x21, 0x80, 0x7f, 0x1a, 0x82, 0x39, 0x4f, 0xc1, 0xa7, 0xd7, 0x0d, 0xd1, 0xd8, 0xff, 0x13, 0x93, 0x70, 0xee, 0x5b, 0xef, 0xbe, 0x09, 0xb9, 0x77, 0x72, 0xe7, 0xb2, 0x54, 0xb7, 0x2a, 0xc7, 0x73, 0x90, 0x66, 0x20, 0x0e, 0x51, 0xed, 0xf8, 0x7c, 0x8f, 0x2e, 0xf4, 0x12, 0xc6, 0x2b, 0x83, 0xcd, 0xac, 0xcb, 0x3b, 0xc4, 0x4e, 0xc0, 0x69, 0x36, 0x62, 0x02, 0xae, 0x88, 0xfc, 0xaa, 0x42, 0x08, 0xa6, 0x45, 0x57, 0xd3, 0x9a, 0xbd, 0xe1, 0x23, 0x8d, 0x92, 0x4a, 0x11, 0x89, 0x74, 0x6b, 0x91, 0xfb, 0xfe, 0xc9, 0x01, 0xea, 0x1b, 0xf7, 0xce]
            
            def decode_base64_package(base64str):
            	base64dec = base64.b64decode(base64str)[20:] # we strip the first 20 chars (SHA1 hash, I don't bother checking it at the moment)
            	decoded = ''
            	for byte in range(0, len(base64dec)):
            		decoded += chr(charmap[ord(base64dec[byte])])
            	return zlib.decompress(decoded)
            	
            
            sys.stderr.write("=== Oracle 10g/11g PL/SQL unwrapper - by Niels Teusink - blog.teusink.net ===\n\n" )
            if len(sys.argv) < 2:
            	sys.stderr.write("Usage: %s infile.plb [outfile]\n" % sys.argv[0])
            	sys.exit(1)
            
            infile = open(sys.argv[1])
            outfile = None
            if len(sys.argv) == 3:
            	outfile = open(sys.argv[2], 'w')
            
            lines = infile.readlines()
            if outfile:
            for i in range(0, len(lines)):
            	# this is really naive parsing, but works on every package I've thrown at it
            	matches = re.compile(r"^[0-9a-f]+ ([0-9a-f]+)$").match(lines[i])
            	if matches:
            		base64len = int(matches.groups()[0], 16)
            		#print matches.groups()[0]
            		base64str = ''
            		j = 0
            		#print base64len
            		while len(base64str) < base64len:
            			j+=1
            			base64str += lines[i+j]
            			#print j
            		base64str = base64str.replace("\n","")
            		
            		if outfile:
            			outfile.write(decode_base64_package(base64str) + "\n")
            		else:
            			print decode_base64_package(base64str)
            print "Unwrap is successfully!"

            Comment

            • hitmanjd
              New Member
              • Jul 2010
              • 3

              #7
              Originally posted by toanmh
              Hi all!
              I'm looking in google found some code in python to unwrap package in oracle. But I don't know it.Some one help me to convert it to C# or Java. Please.
              Thanks.!

              Code:
              import re
              import base64
              import zlib
              import sys
              
              # simple substitution table
              charmap = [0x3d, 0x65, 0x85, 0xb3, 0x18, 0xdb, 0xe2, 0x87, 0xf1, 0x52, 0xab, 0x63, 0x4b, 0xb5, 0xa0, 0x5f, 0x7d, 0x68, 0x7b, 0x9b, 0x24, 0xc2, 0x28, 0x67, 0x8a, 0xde, 0xa4, 0x26, 0x1e, 0x03, 0xeb, 0x17, 0x6f, 0x34, 0x3e, 0x7a, 0x3f, 0xd2, 0xa9, 0x6a, 0x0f, 0xe9, 0x35, 0x56, 0x1f, 0xb1, 0x4d, 0x10, 0x78, 0xd9, 0x75, 0xf6, 0xbc, 0x41, 0x04, 0x81, 0x61, 0x06, 0xf9, 0xad, 0xd6, 0xd5, 0x29, 0x7e, 0x86, 0x9e, 0x79, 0xe5, 0x05, 0xba, 0x84, 0xcc, 0x6e, 0x27, 0x8e, 0xb0, 0x5d, 0xa8, 0xf3, 0x9f, 0xd0, 0xa2, 0x71, 0xb8, 0x58, 0xdd, 0x2c, 0x38, 0x99, 0x4c, 0x48, 0x07, 0x55, 0xe4, 0x53, 0x8c, 0x46, 0xb6, 0x2d, 0xa5, 0xaf, 0x32, 0x22, 0x40, 0xdc, 0x50, 0xc3, 0xa1, 0x25, 0x8b, 0x9c, 0x16, 0x60, 0x5c, 0xcf, 0xfd, 0x0c, 0x98, 0x1c, 0xd4, 0x37, 0x6d, 0x3c, 0x3a, 0x30, 0xe8, 0x6c, 0x31, 0x47, 0xf5, 0x33, 0xda, 0x43, 0xc8, 0xe3, 0x5e, 0x19, 0x94, 0xec, 0xe6, 0xa3, 0x95, 0x14, 0xe0, 0x9d, 0x64, 0xfa, 0x59, 0x15, 0xc5, 0x2f, 0xca, 0xbb, 0x0b, 0xdf, 0xf2, 0x97, 0xbf, 0x0a, 0x76, 0xb4, 0x49, 0x44, 0x5a, 0x1d, 0xf0, 0x00, 0x96, 0x21, 0x80, 0x7f, 0x1a, 0x82, 0x39, 0x4f, 0xc1, 0xa7, 0xd7, 0x0d, 0xd1, 0xd8, 0xff, 0x13, 0x93, 0x70, 0xee, 0x5b, 0xef, 0xbe, 0x09, 0xb9, 0x77, 0x72, 0xe7, 0xb2, 0x54, 0xb7, 0x2a, 0xc7, 0x73, 0x90, 0x66, 0x20, 0x0e, 0x51, 0xed, 0xf8, 0x7c, 0x8f, 0x2e, 0xf4, 0x12, 0xc6, 0x2b, 0x83, 0xcd, 0xac, 0xcb, 0x3b, 0xc4, 0x4e, 0xc0, 0x69, 0x36, 0x62, 0x02, 0xae, 0x88, 0xfc, 0xaa, 0x42, 0x08, 0xa6, 0x45, 0x57, 0xd3, 0x9a, 0xbd, 0xe1, 0x23, 0x8d, 0x92, 0x4a, 0x11, 0x89, 0x74, 0x6b, 0x91, 0xfb, 0xfe, 0xc9, 0x01, 0xea, 0x1b, 0xf7, 0xce]
              
              def decode_base64_package(base64str):
              	base64dec = base64.b64decode(base64str)[20:] # we strip the first 20 chars (SHA1 hash, I don't bother checking it at the moment)
              	decoded = ''
              	for byte in range(0, len(base64dec)):
              		decoded += chr(charmap[ord(base64dec[byte])])
              	return zlib.decompress(decoded)
              	
              
              sys.stderr.write("=== Oracle 10g/11g PL/SQL unwrapper - by Niels Teusink - blog.teusink.net ===\n\n" )
              if len(sys.argv) < 2:
              	sys.stderr.write("Usage: %s infile.plb [outfile]\n" % sys.argv[0])
              	sys.exit(1)
              
              infile = open(sys.argv[1])
              outfile = None
              if len(sys.argv) == 3:
              	outfile = open(sys.argv[2], 'w')
              
              lines = infile.readlines()
              if outfile:
              for i in range(0, len(lines)):
              	# this is really naive parsing, but works on every package I've thrown at it
              	matches = re.compile(r"^[0-9a-f]+ ([0-9a-f]+)$").match(lines[i])
              	if matches:
              		base64len = int(matches.groups()[0], 16)
              		#print matches.groups()[0]
              		base64str = ''
              		j = 0
              		#print base64len
              		while len(base64str) < base64len:
              			j+=1
              			base64str += lines[i+j]
              			#print j
              		base64str = base64str.replace("\n","")
              		
              		if outfile:
              			outfile.write(decode_base64_package(base64str) + "\n")
              		else:
              			print decode_base64_package(base64str)
              print "Unwrap is successfully!"
              hello all, migrate the python code to IronPython, but I have problems with the zlib library, there is no way to load the program.
              Anyone know how I can load this library.

              Comment

              Working...