Click here to Skip to main content
15,310,056 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:

I got the below sample java coding for the pin encryption method. But I need to do this on c# application. I tried some example methods found out from the internet but still not found a proper solution. can some one help me to do this on c#.

// The Triple DES algorithm requires a trusted source of random bits
SecureRandom sr = new SecureRandom();

//Create a Triple DES key object specification from the raw data.
//Treat the bytes read from file as a hex string
DESedeKeySpec dks = new DESedeKeySpec(Hex.fromString(keyString));
// Create a key factory and use it to turn the DESedeKeySpec into
// a SecretKey object
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance( "DESede" );
SecretKey key = keyFactory.generateSecret( dks );

Cipher cipher = Cipher.getInstance("DESede/ECB/NoPadding");
// Initialize the cipher with the key
cipher.init( Cipher.ENCRYPT_MODE, key );

//Get the input data padded with 'F' in a byte array
byte[] data = Hex.fromString(XORResult);

// The actual encryption step

byte encryptedData[] = cipher.doFinal( data );

/**Triple DES Encryption End **/

//Convert the encrypted bytes to big int
BigInteger bCiph = new BigInteger(encryptedData);

//Dump the byte array to a temp string
tempString = Hex.dumpString(getMagnitude(bCiph));
tempString = tempString.trim();

//If output contains hex A-F convert to
//ASCII value of 3A-3F
//Get the output string in a byte array
byte outputStr[] = new byte[tempString.length()];
//Compare each byte for values A-F
for (int hexCnt=0;hexCnt<tempString.length();hexCnt++)
        outputStr[hexCnt] = (byte) tempString.charAt(hexCnt);

//Convert the output byte array to string
encryptedPin = new String(outputStr);

encryptedPin = encryptedPin.trim();
Updated 15-Mar-15 23:14pm

1 solution

Try the TripleDES Class[^]

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900