from java to c#
C#
3
Posts
3
Posters
0
Views
1
Watching
-
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 ""; }
-
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 ""; }
Most of the code is much the same. See Encrypt/Decrypt String using DES in C#[^] and the DES Class[^] for more information.
-
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