import java.io.UnsupportedEncodingException;
import java.util.Base64;
public class AjaxEncoder {
static private char[] alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-|".toCharArray();
static private byte[] codes = new byte[256];
static {
for (int i = 0; i < 256; i++)
codes[i] = -1;
for (int i = 'A'; i <= 'Z'; i++)
codes[i] = (byte) (i - 'A');
for (int i = 'a'; i <= 'z'; i++)
codes[i] = (byte) (26 + i - 'a');
for (int i = '0'; i <= '9'; i++)
codes[i] = (byte) (52 + i - '0');
codes['_'] = 62;
codes['-'] = 63;
}
static public char[] encode(byte[] data) {
char[] out = new char[((data.length + 2) / 3) * 4];
for (int i = 0, index = 0; i < data.length; i += 3, index += 4) {
boolean quad = false;
boolean trip = false;
int val = (0xFF & (int) data[i]);
val <<= 8;
if ((i + 1) < data.length) {
val |= (0xFF & (int) data[i + 1]);
trip = true;
}
val <<= 8;
if ((i + 2) < data.length) {
val = (0x0F & (int) data[i]) | ((0x0F & (int) data[i + 2] << 4));
val <<= 8;
val |= ((0x0F & (int) data[i + 1]) << 4) | ((0xF0 & (int) data[i + 1]) >> 4);
val <<= 8;
val |= ((0xF0 & (int) data[i]) >> 4) | ((0xF0 & (int) data[i + 2]));
quad = true;
}
out[index + 3] = alphabet[(quad ? (val & 0x3F) : 64)];
val >>= 6;
out[index + 2] = alphabet[(trip ? (val & 0x3F) : 64)];
val >>= 6;
out[index + 1] = alphabet[val & 0x3F];
val >>= 6;
out[index + 0] = alphabet[val & 0x3F];
}
return out;
}
static public String encode(String data) {
String result = null;
try {
result = new String(encode(data.getBytes("UTF-8")));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return result;
}
static public byte[] decode(char[] data) {
int len = ((data.length + 3) / 4) * 3;
if (data.length > 0 && data[data.length - 1] == alphabet[64])
--len;
if (data.length > 0 && data[data.length - 2] == alphabet[64])
--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 >= 24) {
accum = (accum & 0xF0) | ((accum & 0x0F) << 20) | ((accum & 0xF000) >> 4) | ((accum & 0x0F00) << 4) | ((accum & 0xF00000) >>
20) | (accum & 0x0F0000);
while (shift > 0) {
shift -= 8;
out[index++] = (byte) ((accum >> shift) & 0xff);
}
}
}
}
while (shift >= 8) {
shift -= 8;
out[index++] = (byte) ((accum >> shift) & 0xff);
}
if (index != out.length) {
return null;
}
return out;
}
static public String decode(String data) {
String result = null;
try {
result = new String(decode(data.toCharArray()), "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return result;
}
}
这个是我自己翻译 的,但是加解密都对不上java的
using System;
using System.Collections.Generic;
using System.Text;
namespace AjaxEncoder
{
public class AjaxEncoder
{
private static char[] alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-|".ToCharArray();
private static byte[] codes = new byte[256];
public AjaxEncoder()
{
for (int i = 'A'; i <= 'Z'; i++)
codes[i] = (byte)(i - 'A');
for (int i = 'a'; i <= 'z'; i++)
codes[i] = (byte)(26 + i - 'a');
for (int i = '0'; i <= '9'; i++)
codes[i] = (byte)(52 + i - '0');
codes['_'] = 62;
codes['-'] = 63;
}
public static char[] encode(byte[] data)
{
char[] out1 = new char[((data.Length + 2) / 3) * 4];
for (int i = 0, index = 0; i < data.Length; i += 3, index += 4)
{
bool quad = false;
bool trip = false;
int val = (0xFF & (int)data[i]);
val <<= 8;
if ((i + 1) < data.Length)
{
val |= (0xFF & (int)data[i + 1]);
trip = true;
}
val <<= 8;
if ((i + 2) < data.Length)
{
val = (0x0F & (int)data[i]) | ((0x0F & (int)data[i + 2] << 4));
val <<= 8;
val |= ((0x0F & (int)data[i + 1]) << 4) | ((0xF0 & (int)data[i + 1]) >> 4);
val <<= 8;
val |= ((0xF0 & (int)data[i]) >> 4) | ((0xF0 & (int)data[i + 2]));
quad = true;
}
out1[index +3] = alphabet[(quad ? (val & 0x3F) : 64)];
val >>= 6;
out1[index +2] = alphabet[(trip ? (val & 0x3F) : 64)];
val >>= 6;
out1[index +1] = alphabet[val & 0x3F];
val >>= 6;
out1[index +0] = alphabet[val & 0x3F];
}
return out1;
}
public static string encode(string data)
{
string result = null;
try
{
result = new string(encode(Encoding.Default.GetBytes(data)));
}
catch (Exception e)
{
return e.Message;
}
return result;
}
public static byte[] decode(char[] data)
{
int len = ((data.Length + 3) / 4) * 3;
if (data.Length > 0 && data[data.Length - 1] == alphabet[64])
--len;
if (data.Length > 0 && data[data.Length - 2] == alphabet[64])
--len;
byte[] out1 = 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 >= 24)
{
accum = (accum & 0xF0) | ((accum & 0x0F) << 20) | ((accum & 0xF000) >> 4) | ((accum & 0x0F00) << 4) | ((accum & 0xF00000) >>
20) | (accum & 0x0F0000);
while (shift > 0)
{
shift -= 8;
out1[index++] = (byte)((accum >> shift) & 0xff);
}
}
}
}
while (shift >= 8)
{
shift -= 8;
out1[index++] = (byte)((accum >> shift) & 0xff);
}
if (index != out1.Length)
{
return null;
}
return out1;
}
public static string decode(string data)
{
string result = null;
try
{
var ms = data.ToCharArray();
result = Encoding.Default.GetString(decode(ms));
}
catch (Exception ex)
{
return ex.Message;
}
return result;
}
}
}
为什么用默认编码不用utf-8的
这个我改了utf-8的,但是还是不行,第三方加密出的数据和我的完全不一样,解密我还会出错
for (int i = 0; i < 256; i++)
codes[i] = -1;
这段干嘛去掉
@厘米~: 这段在C#中出错,强转也不行,C#的是从0开始的
@周庆明: 你可以用sbyte试试 https://www.cnblogs.com/passedbylove/p/11318983.html
@厘米~: for (int i = 0; i < 256; i++)
codes[i] = -1 & 0xff;
其实我也加了这句,但是解密老是解不了最后一个“}” 因为out1数组老是长度不够
@厘米~: while (shift > 0)
{
shift -= 8;
out1[index++] = (byte)((accum >> shift) & 0xff);
}
就是这句老是越界
@周庆明: 🏳
System.Web.HttpUtility.UrlEncode(str,System.Text.Encoding.UTF8);
System.Web.HttpUtility.UrlDecode(str,System.Text.Encoding.UTF8);
这个是第三方使用java定制的base64加密,必须要把java代码翻译过去
@周庆明: 发一个 加密前的数据和一个加密后的数据
@风中起舞:
加密前:{"YHBH":"1000000000","BJBH":"10049008","CXSJ":"2020/11/5 10:30:01"}
加密后:myJXiCREIqMiAQMzAAMzAAMzwCIjoiRCIoQkGiIzQAMzCQMzyCIjgjRSI6QlKiIzACMzHxMyD1MioQMzowMzIBMjfQ||
给一个加密前的数据和一个加密后的数据
加密前:{"YHBH":"1000000000","BJBH":"10049008","CXSJ":"2020/11/5 10:30:01"}
加密后:myJXiCREIqMiAQMzAAMzAAMzwCIjoiRCIoQkGiIzQAMzCQMzyCIjgjRSI6QlKiIzACMzHxMyD1MioQMzowMzIBMjfQ||
@周庆明: 加密规则是什么
@竜咚咚: 就是我贴出来的那两套代码,一个是java的,java是原版的,C#是翻译过来的,但是加解密都跟java对不上
@周庆明: 加密前:ABCDEFGHIJKLMNOPQRSTUVWXYZ
加密后:ASREBFREB4RECrREDeREABVVA0VVBnVVWVo|
你走一遍java的代码看看是否一致
@风中起舞: 可以,就是有几个字符乱码了
@周庆明: private void button3_Click(object sender, EventArgs e)
{
string data = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
string result = null;
try
{
result = new string(encode(Encoding.Default.GetBytes(data)));
MessageBox.Show(result);
}
catch (Exception exception)
{
Console.WriteLine(exception);
throw;
}
}
public static char[] encode(byte[] data)
{
char[] alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-|".ToCharArray();
char[] outs = new char[((data.Length + 2) / 3) * 4];
for (int i = 0, index = 0; i < data.Length; i += 3, index += 4)
{
bool quad = false;
bool trip = false;
int val = (0xFF & (int)data[i]);
val <<= 8;
if ((i + 1) < data.Length)
{
val |= (0xFF & (int)data[i + 1]);
trip = true;
}
val <<= 8;
if ((i + 2) < data.Length)
{
val = (0x0F & (int)data[i]) | ((0x0F & (int)data[i + 2] << 4));
val <<= 8;
val |= ((0x0F & (int)data[i + 1]) << 4) | ((0xF0 & (int)data[i + 1]) >> 4);
val <<= 8;
val |= ((0xF0 & (int)data[i]) >> 4) | ((0xF0 & (int)data[i + 2]));
quad = true;
}
outs[index + 3] = alphabet[(quad ? (val & 0x3F) : 64)];
val >>= 6;
outs[index + 2] = alphabet[(trip ? (val & 0x3F) : 64)];
val >>= 6;
outs[index + 1] = alphabet[val & 0x3F];
val >>= 6;
outs[index + 0] = alphabet[val & 0x3F];
}
return outs;
}
是有几个字符串乱码 全部替换为A了 你自己看看吧
@周庆明: using System;
using System.Text;
namespace HelloWorldApplication
{
class HelloWorld
{
static void Main(string[] args)
{
/* 我的第一个 C# 程序*/
string data = "{"YHBH":"1000000000","BJBH":"10049008","CXSJ":"2020/11/5 10:30:01"}";
var result = new string(encode(Encoding.Default.GetBytes(data)));
Console.WriteLine(result);
Console.ReadKey();
}
public static char[] encode(byte[] data)
{
char[] alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-|".ToCharArray();
char[] outs = new char[((data.Length + 2) / 3) * 4];
for (int i = 0, index = 0; i < data.Length; i += 3, index += 4)
{
bool quad = false;
bool trip = false;
int val = (0xFF & (int)data[i]);
val <<= 8;
if ((i + 1) < data.Length)
{
val |= (0xFF & (int)data[i + 1]);
trip = true;
}
val <<= 8;
if ((i + 2) < data.Length)
{
val = (0x0F & (int)data[i]) | ((0x0F & (int)data[i + 2] << 4));
val <<= 8;
val |= ((0x0F & (int)data[i + 1]) << 4) | ((0xF0 & (int)data[i + 1]) >> 4);
val <<= 8;
val |= ((0xF0 & (int)data[i]) >> 4) | ((0xF0 & (int)data[i + 2]));
quad = true;
}
outs[index + 3] = alphabet[(quad ? (val & 0x3F) : 64)];
val >>= 6;
outs[index + 2] = alphabet[(trip ? (val & 0x3F) : 64)];
val >>= 6;
outs[index + 1] = alphabet[val & 0x3F];
val >>= 6;
outs[index + 0] = alphabet[val & 0x3F];
}
return outs;
}
}
}
@周庆明: json 反编译一下 加密出来和JAVA的一致 你试试
@竜咚咚: 加密后是这个myJXiCREIqMiAQMzAAMzAAMzwCIjoiRCIoQkGiIzQAMzCQMzyCIjgjRSI6QlKiIzACMzHxMyD1MioQMzowMzIBMjfQ||,应该是不一样的
@风中起舞: 你这个加密方法跟我给出的是一样的呀
@周庆明: 用JAVA的环境和C#的环境 加密同一串 出来的结果是一致的啊
@竜咚咚: 我找到问题了,我给出来的java和原版java有点差异
static public char[] encode1(byte[] data) {
char[] out = new char[((data.length + 2) / 3) * 4];
for (int i = 0, index = 0; i < data.length; i += 3, index += 4) {
boolean quad = false;
boolean trip = false;
int val = (0xFF & (int) data[i]);
val <<= 8;
if ((i + 1) < data.length) {
val |= (0xFF & (int) data[i + 1]);
trip = true;
}
val <<= 8;
if ((i + 2) < data.length) {
val = (0x0F & (int) data[i]) | ((0x0F & (int) data[i + 2]) << 4);
val <<= 8;
val |= ((0x0F & (int) data[i + 1]) << 4) | ((0xF0 & (int) data[i + 1]) >> 4);
val <<= 8;
val |= ((0xF0 & (int) data[i]) >> 4) | ((0xF0 & (int) data[i + 2]));
quad = true;
}
out[index + 3] = alphabet[(quad ? (val & 0x3F) : 64)];
val >>= 6;
out[index + 2] = alphabet[(trip ? (val & 0x3F) : 64)];
val >>= 6;
out[index + 1] = alphabet[val & 0x3F];
val >>= 6;
out[index + 0] = alphabet[val & 0x3F];
}
return out;
}
帮忙解决下,大佬们
– 周庆明 4年前