求助,用JAVA写base64的decode和encode方法,不用SUN的包

inter1 2007-01-29 03:41:47

现在想写一个base64的decode和encode方法,
我在J2EE下面看到了一个org\apache\catalina\util\Base64 的类
我想直接用这个类是不是就可以做base64的转换,

代码中加入

import org.apache.catalina.util.*;

public class Base64Code {


private Base64Code() {

}

public static String encode( String s ) {

String str = "test";
str = Base64.encode( s.getBytes() ).toString();
return str;
}
}

编译没有问题,为什么执行的时候总是报java.lang.NoClassDefFoundError的错呢。

求助,如果不用 org\apache\catalina\util\Base64,可否用其他实现BASE64转换的方法。

谢谢。


...全文
10809 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
ayyong2010 2011-10-19
  • 打赏
  • 举报
回复
up!
ts2794 2007-01-31
  • 打赏
  • 举报
回复
up

inter1 2007-01-30
  • 打赏
  • 举报
回复
另外,为什么我引用了
import org.apache.catalina.util.*;

这个包,却不能用,还请教。
inter1 2007-01-30
  • 打赏
  • 举报
回复
谢谢各位,
看来只能拷别人的代码了,以下是国外网站上拷的:
package com.meterware.httpunit;
/********************************************************************************************************************
* $Id: Base64.java,v 1.4 2002/12/24 15:17:17 russgold Exp $
*
* Copyright (c) 2000-2002 by Russell Gold
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
* to permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions
* of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
* THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*
*******************************************************************************************************************/

/**
* A utility class to convert to and from base 64 encoding.
*
* @author <a href="mailto:russgold@httpunit.org">Russell Gold</a>
**/
public class Base64 {

final static String encodingChar = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";


/**
* Returns the base 64 encoded equivalent of a supplied string.
* @param source the string to encode
*/
public static String encode( String source ) {
char[] sourceBytes = getPaddedBytes( source );
int numGroups = (sourceBytes.length + 2) / 3;
char[] targetBytes = new char[4];
char[] target = new char[ 4 * numGroups ];

for (int group = 0; group < numGroups; group++) {
convert3To4( sourceBytes, group*3, targetBytes );
for (int i = 0; i < targetBytes.length; i++) {
target[ i + 4*group ] = encodingChar.charAt( targetBytes[i] );
}
}

int numPadBytes = sourceBytes.length - source.length();

for (int i = target.length-numPadBytes; i < target.length; i++) target[i] = '=';
return new String( target );
}


private static char[] getPaddedBytes( String source ) {
char[] converted = source.toCharArray();
int requiredLength = 3 * ((converted.length+2) /3);
char[] result = new char[ requiredLength ];
System.arraycopy( converted, 0, result, 0, converted.length );
return result;
}


private static void convert3To4( char[] source, int sourceIndex, char[] target ) {
target[0] = (char) ( source[ sourceIndex ] >>> 2);
target[1] = (char) (((source[ sourceIndex ] & 0x03) << 4) | (source[ sourceIndex+1 ] >>> 4));
target[2] = (char) (((source[ sourceIndex+1 ] & 0x0f) << 2) | (source[ sourceIndex+2 ] >>> 6));
target[3] = (char) ( source[ sourceIndex+2 ] & 0x3f);
}


/**
* Returns the plaintext equivalent of a base 64-encoded string.
* @param source a base 64 string (which must have a multiple of 4 characters)
*/
public static String decode( String source ) {
if (source.length()%4 != 0) throw new RuntimeException( "valid Base64 codes have a multiple of 4 characters" );
int numGroups = source.length() / 4;
int numExtraBytes = source.endsWith( "==" ) ? 2 : (source.endsWith( "=" ) ? 1 : 0);
byte[] targetBytes = new byte[ 3*numGroups ];
byte[] sourceBytes = new byte[4];
for (int group = 0; group < numGroups; group++) {
for (int i = 0; i < sourceBytes.length; i++) {
sourceBytes[i] = (byte) Math.max( 0, encodingChar.indexOf( source.charAt( 4*group+i ) ) );
}
convert4To3( sourceBytes, targetBytes, group*3 );
}
return new String( targetBytes, 0, targetBytes.length - numExtraBytes );
}


private static void convert4To3( byte[] source, byte[] target, int targetIndex ) {
target[ targetIndex ] = (byte) (( source[0] << 2) | (source[1] >>> 4));
target[ targetIndex+1 ] = (byte) (((source[1] & 0x0f) << 4) | (source[2] >>> 2));
target[ targetIndex+2 ] = (byte) (((source[2] & 0x03) << 6) | (source[3]));
}

}


---------------------------
歪嘴鱼 2007-01-30
  • 打赏
  • 举报
回复
不能算是加密,只是编码,为了能够顺利通过7位网关发送8位数据
bluesmile979 2007-01-29
  • 打赏
  • 举报
回复
一种URL里面常用的加密手段,防君子不防小人
fengmingjie 2007-01-29
  • 打赏
  • 举报
回复
不好意思哈,问一下Base64Encode和Base64Decode是干啥用的,能给解释一下吗
bluesmile979 2007-01-29
  • 打赏
  • 举报
回复
SUN自己有Base64Encode,Base64Decode两个类,不过不在基本包里,需要的话可以自己到sun的官网上查找一下,再sun.mis*****后面忘了,呵呵
bluesmile979 2007-01-29
  • 打赏
  • 举报
回复
import java.lang.*;
import java.io.*;

public class Base64 {
private static char[] base64EncodeChars = new char[] {
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
'w', 'x', 'y', 'z', '0', '1', '2', '3',
'4', '5', '6', '7', '8', '9', '+', '/' };

private static byte[] base64DecodeChars = new byte[] {
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63,
52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1,
-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1,
-1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1 };

public static String encode(byte[] data) {
StringBuffer sb = new StringBuffer();
int len = data.length;
int i = 0;
int b1, b2, b3;
while (i < len) {
b1 = data[i++] & 0xff;
if (i == len)
{
sb.append(base64EncodeChars[b1 >>> 2]);
sb.append(base64EncodeChars[(b1 & 0x3) << 4]);
sb.append("==");
break;
}
b2 = data[i++] & 0xff;
if (i == len)
{
sb.append(base64EncodeChars[b1 >>> 2]);
sb.append(base64EncodeChars[((b1 & 0x03) << 4) | ((b2 & 0xf0) >>> 4)]);
sb.append(base64EncodeChars[(b2 & 0x0f) << 2]);
sb.append("=");
break;
}
b3 = data[i++] & 0xff;
sb.append(base64EncodeChars[b1 >>> 2]);
sb.append(base64EncodeChars[((b1 & 0x03) << 4) | ((b2 & 0xf0) >>> 4)]);
sb.append(base64EncodeChars[((b2 & 0x0f) << 2) | ((b3 & 0xc0) >>> 6)]);
sb.append(base64EncodeChars[b3 & 0x3f]);
}
return sb.toString();
}

public static byte[] decode(String str) throws UnsupportedEncodingException {
StringBuffer sb = new StringBuffer();
byte[] data = str.getBytes("US-ASCII");
int len = data.length;
int i = 0;
int b1, b2, b3, b4;
while (i < len) {
/* b1 */
do {
b1 = base64DecodeChars[data[i++]];
} while (i < len && b1 == -1);
if (b1 == -1) break;
/* b2 */
do {
b2 = base64DecodeChars[data[i++]];
} while (i < len && b2 == -1);
if (b2 == -1) break;
sb.append((char)((b1 << 2) | ((b2 & 0x30) >>> 4)));
/* b3 */
do {
b3 = data[i++];
if (b3 == 61) return sb.toString().getBytes("ISO-8859-1");
b3 = base64DecodeChars[b3];
} while (i < len && b3 == -1);
if (b3 == -1) break;
sb.append((char)(((b2 & 0x0f) << 4) | ((b3 & 0x3c) >>> 2)));
/* b4 */
do {
b4 = data[i++];
if (b4 == 61) return sb.toString().getBytes("ISO-8859-1");
b4 = base64DecodeChars[b4];
} while (i < len && b4 == -1);
if (b4 == -1) break;
sb.append((char)(((b3 & 0x03) << 6) | b4));
}
return sb.toString().getBytes("ISO-8859-1");
}
}

inter1 2007-01-29
  • 打赏
  • 举报
回复
up

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧