需要C#等价的java Triple DES加密算法 [英] Need C# equivalent of java Triple DES Encryption Algorithm
本文介绍了需要C#等价的java Triple DES加密算法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我得到了针对引脚加密方法的以下示例java编码。但我需要在c#应用程序上执行此操作。我尝试了从互联网上找到的一些示例方法,但仍未找到合适的解决方案。有人可以帮我在c#上做这件事。
Hi,
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();
推荐答案
试试TripleDES类 [ ^ ]
这篇关于需要C#等价的java Triple DES加密算法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文