Jump to content
Tuts 4 You

how to convert Java method in to C#


kate

Recommended Posts

Hello Friends,

          I'm trying to implement one key generation logic developed in java in to c#. So can anyone tell me how to convert it in c# so that both the java and C# programs will work in similar manner. My java method which will implement the key is as below.

public static String calculateOTP(byte[] seedValue, String userPIN, String challenge) {
        try {
            SHA1 sha1 = new SHA1();
            byte[] hashedChallenge = sha1.digest(challenge.getBytes());
            sha1.init();
            sha1.update(seedValue);
            sha1.update(hashedChallenge);
            sha1.finish();
            byte[] otp0byte = sha1.digest();
            int otp0 = 0;
            for (int i = 17; i < otp0byte.length; i++) {
                otp0 = (otp0 << 8) | (otp0byte[i] & SecretKeyPacket.USAGE_CHECKSUM);
            }
            String num = Integer.toString((otp0 + Integer.parseInt(userPIN)) & ViewCompat.MEASURED_SIZE_MASK, 16);
            while (num.length() < 6) {
                num = "0" + num;
            }
            return num;
        } catch (Exception e) {
            return null;
        }
    }

If you know how to effectively do this than please help me.

Edited by kate
Link to comment
9 minutes ago, kate said:

otp0 = (otp0 << 8) | (otp0byte & SecretKeyPacket.USAGE_CHECKSUM);

This code makes no sense to me. otp0byte is an array, SecretKeyPacket.USAGE_CHECKSUM seems to be integer constant. Maybe Java allows you to do that, but I have no idea what the result would be.

Apart from that, C# code will look almost the same.

Link to comment
9 minutes ago, kao said:

This code makes no sense to me. otp0byte is an array, SecretKeyPacket.USAGE_CHECKSUM seems to be integer constant. Maybe Java allows you to do that, but I have no idea what the result would be.

Apart from that, C# code will look almost the same.

Probable will not even compile! probable the good code is this:
otp0 = (otp0 << 8) | (otp0byte [ i ] & SecretKeyPacket.USAGE_CHECKSUM);
 
Crap it is board problem: can't enter " [ i ] "  without spaces " " !
 

Edited by CodeCracker
Link to comment

Please don't PM me. I will respond in public threads if and when I have something to say.

You can use SHA1Managed class for hash calculation: TransformBlock function replaces sha1.update, TransformFinalBlock replaces sha1.finish.

int.Parse replaces Integer.parseInt and String.Format will format the final result. That's it.

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...