首页 新闻 会员 周边

java 3DES加密,.net如何解密

0
悬赏园豆:30 [已关闭问题] 关闭于 2016-05-06 18:03

string a_strKey="12345678123456781234567812345678"; 

string a_strString="6287be5754e89731846d74d652df2178";

 

//这是我在网上找的解密方法是

public string Decrypt3DES(string a_strString, string a_strKey)         {

            TripleDESCryptoServiceProvider DES = new TripleDESCryptoServiceProvider();             DES.Key = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(a_strKey, "md5").Substring(0, 24));             DES.Mode = CipherMode.ECB;             DES.Padding = System.Security.Cryptography.PaddingMode.PKCS7;             ICryptoTransform DESDecrypt = DES.CreateDecryptor();             string result = "";             try             {                 byte[] Buffer = HexStringToBytes(a_strString);                 result = ASCIIEncoding.ASCII.GetString(DESDecrypt.TransformFinalBlock(Buffer, 0, Buffer.Length));             }             catch (Exception e)             {             }             return result;         }

 

但是会报错:不正确的数据

(如果换成其他的解密方法,就会有弱秘钥这个问题)

这个该如何解决!!!

十里外教堂的主页 十里外教堂 | 初学一级 | 园豆:71
提问于:2016-05-05 20:18
< >
分享
所有回答(3)
0

你至少把java代码贴出来吧

MrNice | 园豆:3450 (老鸟四级) | 2016-05-06 08:27

public static String encode(String value, String key) throws Exception {   byte[] valueByte = value.getBytes();   byte[] sl = encrypt(valueByte, key);   String result = BytesUtil.bytesToHex(sl);   return result;  }

 public static byte[] encrypt(byte[] input, String ck) {   try {    return ecbEncrypt(BytesUtil.hexToBytes(ck), input, 2);   } catch (Exception ex) {    System.out.println("....");    return null;   }  }

private static byte[] ecbEncrypt(byte[] key, byte[] data, int padding)    throws GeneralSecurityException {   return internalCrypt(1, 1, key, data, null, padding);  }

private static byte[] internalCrypt(int enc, int ecb, byte[] key,    byte[] data, byte[] iv, int padding)    throws GeneralSecurityException {   if (key == null     || (key.length != 8 && key.length != 16 && key.length != 24)) {    throw new IllegalArgumentException();   }

  if (data == null) {    throw new IllegalArgumentException();   }

  Cipher c = Cipher.getInstance(ecb != 0 ? "DESede/ECB/NoPadding"     : "DESede/CBC/NoPadding");

  byte[] deskey = new byte[24];   if (key.length == 8) {    System.arraycopy(key, 0, deskey, 0, 8);    System.arraycopy(key, 0, deskey, 8, 8);    System.arraycopy(key, 0, deskey, 16, 8);   } else if (key.length == 16) {    System.arraycopy(key, 0, deskey, 0, 16);    System.arraycopy(key, 0, deskey, 16, 8);   } else {    System.arraycopy(key, 0, deskey, 0, 24);   }

  byte[] desdata = data;   if (padding == 1) {    desdata = new byte[(data.length / 8 + 1) * 8];    System.arraycopy(data, 0, desdata, 0, data.length);    desdata[data.length] = (byte) 0x80;    Arrays.fill(desdata, data.length + 1, desdata.length, (byte) 0x00);   } else if (padding == 2) {    if (data.length % 8 != 0) {     desdata = new byte[(data.length / 8 + 1) * 8];     System.arraycopy(data, 0, desdata, 0, data.length);     Arrays.fill(desdata, data.length, desdata.length, (byte) 0x00);    }   }

  if (ecb != 0) {    c.init(enc != 0 ? Cipher.ENCRYPT_MODE : Cipher.DECRYPT_MODE,      new SecretKeySpec(deskey, "DESede"));   } else {    byte[] zero = { (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,      (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00 };    if (iv == null) {     iv = zero;    }    IvParameterSpec ivps = new IvParameterSpec(iv);    c.init(enc != 0 ? Cipher.ENCRYPT_MODE : Cipher.DECRYPT_MODE,      new SecretKeySpec(deskey, "DESede"), ivps);   }   return c.doFinal(desdata);  }

 

 

这个就是Java的加密方式

支持(0) 反对(0) 十里外教堂 | 园豆:71 (初学一级) | 2016-05-06 09:12
0

 

 遇到紧急问题搞不定,发布出去没人回答或者反而遭到嘲讽怎么办?

需要花时间的问题往往没人解答或者指导,毕竟时间都是金钱,

要么都是牛头不对马嘴,热心的往往都是菜鸟,毕竟高手时刻在忙

而且自己耽误的时间同样是金钱,其实可以选择付合适的费用发布到www.wuji001.com

很快就会有高手耐心指导,并且享受顾客就是上帝的服务态度 

 

无记 | 园豆:309 (菜鸟二级) | 2016-05-06 10:25
0

很多时候,就是看的太窄了

 

之所有没有解密出来,就是一个模式的问题 

十里外教堂 | 园豆:71 (初学一级) | 2016-05-06 18:02
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册