|
how i can convert this code from java to c#
public static String Dcipher(String encryptedText1) {
try {
byte[] desKeyData = {(byte) 0x01, (byte) 0x02, (byte) 0x03,
(byte) 0x04, (byte) 0x05, (byte) 0x06,
(byte) 0x07, (byte) 0x08};
DESKeySpec desKeySpec = new DESKeySpec(desKeyData);
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
SecretKey key = null;
try {
key = keyFactory.generateSecret(desKeySpec);
} catch (InvalidKeySpecException ex1) {
}
byte[] initVector = new byte[] {0x10, 0x10, 0x01, 0x04, 0x01, 0x01,
0x01,
0x02};
AlgorithmParameterSpec algParamSpec = new IvParameterSpec(
initVector);
Cipher m_decrypter = Cipher.getInstance("DES/OFB32/NoPadding");
m_decrypter.init(Cipher.DECRYPT_MODE, key, algParamSpec);
byte[] encryptedText = encryptedText1.getBytes();
byte[] decryptedText = m_decrypter.doFinal(encryptedText);
return (new String(decryptedText));
} catch (BadPaddingException ex) {
ex.printStackTrace();
} catch (IllegalBlockSizeException ex) {
ex.printStackTrace();
} catch (InvalidAlgorithmParameterException ex) {
ex.printStackTrace();
} catch (InvalidKeyException ex) {
ex.printStackTrace();
} catch (NoSuchPaddingException ex) {
ex.printStackTrace();
} catch (NoSuchAlgorithmException ex) {
ex.printStackTrace();
}
return "";
}
|
|
|
|
|
|
I don't have a conversion for some of the Java types you're using, but the following syntax should be correct:
public static string Dcipher(string encryptedText1)
{
try
{
sbyte[] desKeyData = {(sbyte) 0x01, (sbyte) 0x02, (sbyte) 0x03, (sbyte) 0x04, (sbyte) 0x05, (sbyte) 0x06, (sbyte) 0x07, (sbyte) 0x08};
DESKeySpec desKeySpec = new DESKeySpec(desKeyData);
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
SecretKey key = null;
try
{
key = keyFactory.generateSecret(desKeySpec);
}
catch (InvalidKeySpecException ex1)
{
}
sbyte[] initVector = new sbyte[] {0x10, 0x10, 0x01, 0x04, 0x01, 0x01, 0x01, 0x02};
AlgorithmParameterSpec algParamSpec = new IvParameterSpec(initVector);
Cipher m_decrypter = Cipher.getInstance("DES/OFB32/NoPadding");
m_decrypter.init(Cipher.DECRYPT_MODE, key, algParamSpec);
sbyte[] encryptedText = encryptedText1.Bytes;
sbyte[] decryptedText = m_decrypter.doFinal(encryptedText);
return (new string(decryptedText));
}
catch (BadPaddingException ex)
{
Console.WriteLine(ex.ToString());
Console.Write(ex.StackTrace);
}
catch (IllegalBlockSizeException ex)
{
Console.WriteLine(ex.ToString());
Console.Write(ex.StackTrace);
}
catch (InvalidAlgorithmParameterException ex)
{
Console.WriteLine(ex.ToString());
Console.Write(ex.StackTrace);
}
catch (InvalidKeyException ex)
{
Console.WriteLine(ex.ToString());
Console.Write(ex.StackTrace);
}
catch (NoSuchPaddingException ex)
{
Console.WriteLine(ex.ToString());
Console.Write(ex.StackTrace);
}
catch (NoSuchAlgorithmException ex)
{
Console.WriteLine(ex.ToString());
Console.Write(ex.StackTrace);
}
return "";
}
Dave Doknjas
Convert between VB, C#, C++, & Java
www.tangiblesoftwaresolutions.com
Instant C# - VB to C# Converter
Instant VB - C# to VB Converter
|
|
|
|