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; }
但是会报错:不正确的数据
(如果换成其他的解密方法,就会有弱秘钥这个问题)
这个该如何解决!!!
你至少把java代码贴出来吧
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的加密方式
很多时候,就是看的太窄了
之所有没有解密出来,就是一个模式的问题