高手请进,java算法转C#

时间:2021-11-09 13:37:31
public class DES

 private byte[] EncodAndDecodeKey = null;

  private static String strAlgorithm = "DESede";  

public void initPhar()
{
    String strKey = "1234567890987650abcdefgh";
    int size = strKey.length();
    byte[] temKey = strKey.getBytes();
    byte[] EncodAndDecodeKey = new byte[size];
    for (int i = 0; i < size; i++) {
      EncodAndDecodeKey[i] = temKey[i];
    }
}
public void DecryptData(String strEnDataFile, String strDeDataFile)
  {
    try
    {
      SecretKeySpec destmp = new SecretKeySpec(this.EncodAndDecodeKey, "DESede");
      SecretKey theKey = destmp;

      Cipher cipher = Cipher.getInstance("DESede");

      cipher.init(2, theKey);
      FileInputStream in = new FileInputStream(strEnDataFile);
      FileOutputStream out = new FileOutputStream(strDeDataFile);
      int len;
      while ((len = in.read()) > 0) {
        byte[] Data = new byte[len];
        in.read(Data);
        byte[] decryptedtext = cipher.doFinal(Data);
        out.write(decryptedtext);
      }

      in.close();
      out.close();
    }
    catch (NoSuchAlgorithmException e1) {
      e1.printStackTrace();
    }
    catch (NoSuchPaddingException e2) {
      e2.printStackTrace();
    }
    catch (Exception e3) {
      e3.printStackTrace();
    }
    catch (NoSuchMethodError e4) {
      e4.printStackTrace();
    }
  }
  public void DecryptData(String strEnDataFile, String strDeDataFile)
  {
    try
    {
      SecretKeySpec destmp = new SecretKeySpec(this.EncodAndDecodeKey, "DESede");
      SecretKey theKey = destmp;

      Cipher cipher = Cipher.getInstance("DESede");

      cipher.init(2, theKey);
      FileInputStream in = new FileInputStream(strEnDataFile);
      FileOutputStream out = new FileOutputStream(strDeDataFile);
      int len;
      while ((len = in.read()) > 0) {
        byte[] Data = new byte[len];
        in.read(Data);
        byte[] decryptedtext = cipher.doFinal(Data);
        out.write(decryptedtext);
      }

      in.close();
      out.close();
    }
    catch (NoSuchAlgorithmException e1) {
      e1.printStackTrace();
    }
    catch (NoSuchPaddingException e2) {
      e2.printStackTrace();
    }
    catch (Exception e3) {
      e3.printStackTrace();
    }
    catch (NoSuchMethodError e4) {
      e4.printStackTrace();
    }
  }
}

以上是java的一个加密解密算法,现在要根据这个算法编写C#算法,请各位高手给出代码。多谢。分不够可以加,在线等。

5 个解决方案

#1


高手请进,java算法转C#

#2


高手呢?

#3


        /// <summary> 
        /// 加密字符串 
        /// 注意:密钥必须为8位 
        /// </summary> 
        /// <param name="strText">字符串</param> 
        /// <param name="encryptKey">密钥</param>
        /// <return>加密后字符串</return>
        public static string DesEncrypt(string strText) 
        {
            string outString="";
            byte[] byKey=null; 
            byte[] IV= {0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF}; 
            try 
            {
                byKey = System.Text.Encoding.UTF8.GetBytes(encryptKey.Substring(0, encryptKey.Length)); 
                DESCryptoServiceProvider des = new DESCryptoServiceProvider();
                byte[] inputByteArray = Encoding.UTF8.GetBytes(strText); 
                MemoryStream ms = new MemoryStream(); 
                CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(byKey, IV), CryptoStreamMode.Write) ; 
                cs.Write(inputByteArray, 0, inputByteArray.Length); 
                cs.FlushFinalBlock();
                outString = Convert.ToBase64String(ms.ToArray()); 
            } 
            catch(System.Exception) 
            {
                outString = ""; 
            }

            return outString;
        } 


高手请进,java算法转C#

#4


没多大区别,算法这玩意差不多的

#1


高手请进,java算法转C#

#2


高手呢?

#3


        /// <summary> 
        /// 加密字符串 
        /// 注意:密钥必须为8位 
        /// </summary> 
        /// <param name="strText">字符串</param> 
        /// <param name="encryptKey">密钥</param>
        /// <return>加密后字符串</return>
        public static string DesEncrypt(string strText) 
        {
            string outString="";
            byte[] byKey=null; 
            byte[] IV= {0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF}; 
            try 
            {
                byKey = System.Text.Encoding.UTF8.GetBytes(encryptKey.Substring(0, encryptKey.Length)); 
                DESCryptoServiceProvider des = new DESCryptoServiceProvider();
                byte[] inputByteArray = Encoding.UTF8.GetBytes(strText); 
                MemoryStream ms = new MemoryStream(); 
                CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(byKey, IV), CryptoStreamMode.Write) ; 
                cs.Write(inputByteArray, 0, inputByteArray.Length); 
                cs.FlushFinalBlock();
                outString = Convert.ToBase64String(ms.ToArray()); 
            } 
            catch(System.Exception) 
            {
                outString = ""; 
            }

            return outString;
        } 


高手请进,java算法转C#

#4


没多大区别,算法这玩意差不多的

#5