DES算法应该都相通的,你先去网上找个JAVA版的,测试下看看,然后对一下结果是否一致

解决方案 »

  1.   

    vb.net 的写法。真心看起费劲。。 LZ头像好妖艳的感觉
      

  2.   

    +1要注意的的是编码 关键的地方
     inputByteArray = Encoding.UTF8.GetBytes(Text)
                des.Key = ASCIIEncoding.ASCII.GetBytes("12345678")
                des.IV = ASCIIEncoding.ASCII.GetBytes("12345678")
      

  3.   

    版主不要乱移版块,我要翻译成java代码,当然在这个区
      

  4.   


    import javax.crypto.Cipher;
    import javax.crypto.SecretKey;
    import javax.crypto.SecretKeyFactory;
    import javax.crypto.spec.DESKeySpec;
    import javax.crypto.spec.IvParameterSpec;public class DESCrypt {  private static final char[] HEX_CHAR = new char[] {
          '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D',
          'E', 'F'
      };  public static String encrypt(String text, String key) {
        try {
          DESKeySpec ks = new DESKeySpec("12345678".getBytes("ASCII"));
          SecretKeyFactory skf = SecretKeyFactory.getInstance("DES");
          SecretKey sk = skf.generateSecret(ks);
          Cipher cip = Cipher.getInstance("DES/CBC/PKCS5Padding");
          IvParameterSpec iv2 = new IvParameterSpec("12345678".getBytes("ASCII"));
          cip.init(Cipher.ENCRYPT_MODE, sk, iv2);
          byte[] bytes = cip.doFinal(text.getBytes("UTF-8"));
          return toHexString(bytes);
        } catch (Exception ex) {
          return "EnStrError"; // 还是直接抛异常好
        }
      }  public static String decrypt(String text, String key) {
        try {
          DESKeySpec ks = new DESKeySpec("12345678".getBytes("ASCII"));
          SecretKeyFactory skf = SecretKeyFactory.getInstance("DES");
          SecretKey sk = skf.generateSecret(ks);
          Cipher cip = Cipher.getInstance("DES/CBC/PKCS5Padding");
          IvParameterSpec iv2 = new IvParameterSpec("12345678".getBytes("ASCII"));
          cip.init(Cipher.DECRYPT_MODE, sk, iv2);
          byte[] bytes = new byte[text.length() >> 1];
          for (int i = 0, pos = 0; i < bytes.length; i++, pos += 2) {
            bytes[i] = (byte)Integer.parseInt(text.substring(pos, pos+2), 16);
          }
          byte[] decrypt = cip.doFinal(bytes);
          return new String(decrypt, "UTF-8");
        } catch (Exception ex) {
          return "DeStrError"; 
        }
      }  private static String toHexString(byte[] bytes) {
        int len = bytes.length << 1;
        char[] chars = new char[len];
        for (int i = 0, pos = 0; i < bytes.length; i++, pos += 2) {
          int unsignedByte = bytes[i] & 0xFF;
          chars[pos] = HEX_CHAR[unsignedByte >> 4];
          chars[pos + 1] = HEX_CHAR[unsignedByte & 0xF];
        }
        return new String(chars);
      }  public static void main(String[] args) throws Exception {
        String[] tests = new String[] {
            "abcd",
            "1234567890",
            "Hello World!",
            "你好,世界!"
        };
        for (String test : tests) {
          System.out.println(test);
          String encrypt = encrypt(test, "");
          System.out.println(encrypt);
          String decrypt = decrypt(encrypt, "");
          System.out.println(decrypt);
        }
        System.out.println(encrypt("abcd", ""));
        System.out.println(encrypt("Hello World!", ""));
        System.out.println(encrypt("你好,世界!", ""));
      }
    }
      

  5.   

    参考这篇文章
    相关VB代码,用等价的C#测试过,至少这几个main方法中测试结果是一致的。