java url base64 传递_Java--使用Base64编码对url传值

import org.apache.commons.codec.binary.Base64;

import sun.misc.BASE64Decoder;

import sun.misc.BASE64Encoder;

/**

* 对url加密的加密解密算法,这样的加密结果只有数字和字母

* @author Administrator

*

*/

public class Base64Encoding {

private static final BASE64Decoder decoder = new BASE64Decoder();

private static final BASE64Encoder encoder = new BASE64Encoder();

private static final Base64 base64 = new Base64();

/**

* BASE64加密

*

* @param key

* @return

* @throws Exception

*/

public static String encryptBASE64(String key) throws Exception {

if (key == null || key.length() < 1) {

return "";

}

//return new String(encoder.encode(key.getBytes()));

return new String(base64.encodeBase64URLSafe((new String(encoder.encode(key.getBytes()))).getBytes()));

}

public void changeCodeGBK(){

}

/**

* BASE64解密

*

* @param key

* @return

* @throws Exception

*/

public static String decryptBASE64(String key) throws Exception {

if (key == null || key.length() < 1) {

return "";

}

return new String(decoder.decodeBuffer(new String(base64.decodeBase64(key.getBytes()))));

//return new String(base64.decodeBase64(key.getBytes()));

}

public static void main(String[] args) throws Exception {

String s=Base64Encoding.encryptBASE64("$%&^*%^* ^");

System.out.println(s);

System.out.println(Base64Encoding.decryptBASE64(s));

}

}

输出结果:

SkNVbVhpb2xYaW9nSUY0PQ $%&^*%^* ^

本文出自

“orangleliu笔记本”

博客,请务必保留此出处

http://www.voidcn.com/article/p-aakgneco-re.html


版权声明:本文为weixin_29668721原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。