天天看點

des加解密異常

主要異常資訊:

java.lang.Error: 加解密異常:miscalculated data length!

des加解密異常

主要代碼

/**
		 * 将base64編碼的資料解碼成原始資料
		 */
		static private byte[] decode(char[] data) {
			int len = ((data.length + 3) / 4) * 3;
			if (data.length > 0 && data[data.length - 1] == '=')
				--len;
			if (data.length > 1 && data[data.length - 2] == '=')
				--len;
			byte[] out = new byte[len];
			int shift = 0;
			int accum = 0;
			int index = 0;
			for (int ix = 0; ix < data.length; ix++) {
				int value = codes[data[ix] & 0xFF];
				if (value >= 0) {
					accum <<= 6;
					shift += 6;
					accum |= value;
					if (shift >= 8) {
						shift -= 8;
						out[index++] = (byte) ((accum >> shift) & 0xff);
					}
				}
			}
			if (index != out.length)
				throw new Error("加解密異常:miscalculated data length!,資料為:["+new String(data)+"]");
			return out;
		}
           

原因:使用未加密的資料調用此解密方法,造成

index != out.length

,進而抛出異常,可能情形為使用者在用戶端加載到的js中未對資料進行加密,建議清除浏覽器緩存後再重試。

繼續閱讀