// 解密
public static String EncryptAsDoNet(String message, String key)
throws Exception {
Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
DESKeySpec desKeySpec = new DESKeySpec(key.getBytes("UTF-8"));
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
IvParameterSpec iv = new IvParameterSpec(key.getBytes("UTF-8"));
cipher.init(Cipher.ENCRYPT_MODE, secretKey, iv);
byte[] encryptbyte = cipher.doFinal(message.getBytes());
return new String(Base64.encode(encryptbyte, Base64.DEFAULT));
}
最后还要注意一下,一般在客户端调用接口时,请求的是URL地址,参数需要加密,比如token,如果token里含有+号,URL会转码为空格,这时在.net端接收到token时,需要把token中的空格替换为+号:token = Regex.Replace(token, @"\s", "+");这样接收到的token才能正常的解密。