Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. from java to c#

from java to c#

Scheduled Pinned Locked Moved C#
csharpjava
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    superamh
    wrote on last edited by
    #1

    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 "";
    }
    
    L D 2 Replies Last reply
    0
    • S superamh

      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 "";
      }
      
      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Most of the code is much the same. See Encrypt/Decrypt String using DES in C#[^] and the DES Class[^] for more information.

      1 Reply Last reply
      0
      • S superamh

        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 "";
        }
        
        D Offline
        D Offline
        Dave Doknjas
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups