java cryptostream_Java加密代码 转换成Net版

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.IO;

using System.Text.RegularExpressions;

namespace ConsoleApplication2

{public classBase64Utils

{///**//* Base64方法重写//*///public static void main(String[] args)//{//String aa = "商户私有域";//String aaa = byteArrayToBase64(aa.getBytes());//System.out.println(aaa);//try//{//String bb = new String(base64ToByteArray(aaa));//System.out.println(bb);//String tests = encodeBuffer("【厚本金融】", "GBK");//System.out.println(tests);//System.out.println(decodeBuffer(tests, "GBK"));//}//catch (Exception e)//{// //TODO Auto-generated catch block//e.printStackTrace();//}//}

/*** 将字符数组base64字符串

*@parama 要转化的字符数组

*@returnbase64字符串*/

public static String byteArrayToBase64(byte[] a)

{int aLen = a.Length; //总长度

int numFullGroups = aLen / 3; //以3个byte组成以4个字符为一组的组数

int numBytesInPartialGroup = aLen - 3 * numFullGroups; //余数

int resultLen = 4 * ((aLen + 2) / 3); //输出长度总是4倍数,如果有余数,(aLen+2)/3保证将余数包含,并有空间放置填充符=//StringBuffer result = new StringBuffer(resultLen);

StringBuilder result = newStringBuilder(resultLen);int inCursor = 0;for (int i = 0; i < numFullGroups; i++)

{int byte0 = a[inCursor++] & 0xff;int byte1 = a[inCursor++] & 0xff;int byte2 = a[inCursor++] & 0xff;

result.Append(intToBase64[byte0>> 2]);

result.Append(intToBase64[(byte0<< 4) & 0x3f | (byte1 >> 4)]);

result.Append(intToBase64[(byte1<< 2) & 0x3f | (byte2 >> 6)]);

result.Append(intToBase64[byte2& 0x3f]);

}//处理余数

if (numBytesInPartialGroup != 0)

{int byte0 = a[inCursor++] & 0xff;

result.Append(intToBase64[byte0>> 2]);//余数为1

if (numBytesInPartialGroup == 1)

{

result.Append(intToBase64[(byte0<< 4) & 0x3f]);

result.Append("==");

}else{//余数为2

int byte1 = a[inCursor++] & 0xff;

result.Append(intToBase64[(byte0<< 4) & 0x3f | (byte1 >> 4)]);

result.Append(intToBase64[(byte1<< 2) & 0x3f]);

result.Append('=');

}

}returnresult.ToString();

}static readonly char[] intToBase64 = { 'A', 'B', 'C', 'D', 'E', 'F', /*索引 0 ~ 5*/

'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', /*索引6 ~ 18*/

'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', /*索引 19 ~ 31*/

'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', /*索引 32 ~ 44*/

't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', /*索引 45 ~ 57*/

'6', '7', '8', '9', '.'/*原先是字符+*/, '_'/*原先是字符/*/ }; /*索引58 ~ 63*/

/*** base64字符串还原为字符数组

*@params base64字符串

*@return还原后的字符串数组

*@throwsException*/

public static byte[] base64ToByteArray(String s)

{//字符总长必须是4的倍数

int sLen =s.Length;int numGroups = sLen / 4;if (4 * numGroups !=sLen)throw newArgumentException("字串长度必须是4的倍数");//余1个byte则算漏了两个byte,余2个byte则算漏掉了1个byte

int missingBytesInLastGroup = 0;int numFullGroups =numGroups;if (sLen != 0)

{//余2个byte的情况

if (s.charAt(sLen - 1) == '=')

{

missingBytesInLastGroup++;//如果有余数发生,则完整3个byte组数少一个。

numFullGroups--;

}//余1个byte的情况

if (s.charAt(sLen - 2) == '=')

missingBytesInLastGroup++;

}//总字节长度

byte[] result = new byte[3 * numGroups -missingBytesInLastGroup];try{int inCursor = 0, outCursor = 0;for (int i = 0; i < numFullGroups; i++)

{int ch0 = base64toInt(s.charAt(inCursor++), base64ToInt);int ch1 = base64toInt(s.charAt(inCursor++), base64ToInt);int ch2 = base64toInt(s.charAt(inCursor++), base64ToInt);int ch3 = base64toInt(s.charAt(inCursor++), base64ToInt);

result[outCursor++] = (byte)((ch0 << 2) | (ch1 >> 4));

result[outCursor++] = (byte)((ch1 << 4) | (ch2 >> 2));

result[outCursor++] = (byte)((ch2 << 6) |ch3);

}if (missingBytesInLastGroup != 0)

{int ch0 = base64toInt(s.charAt(inCursor++), base64ToInt);int ch1 = base64toInt(s.charAt(inCursor++), base64ToInt);//不管余1还是余2个byte,肯定要解码一个byte。

result[outCursor++] = (byte)((ch0 << 2) | (ch1 >> 4));//如果余2个,即差一个才构成3byte,那么还要解码第二个byte。

if (missingBytesInLastGroup == 1)

{int ch2 = base64toInt(s.charAt(inCursor++), base64ToInt);

result[outCursor++] = (byte)((ch1 << 4) | (ch2 >> 2));

}

}

}catch(Exception e)

{throwe;

}returnresult;

}private static int base64toInt(char c, int[] alphaToInt)

{int result =alphaToInt[c];if (result < 0)throw new Exception("illegal index!");//非法索引值

returnresult;

}static readonly int[] base64ToInt = { 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, 1/*原先是62*/, 1, 1, 62/*原先是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,63/*原先是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};//static readonly byte[] base64ToInt = { -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, -1/*原先是62*/, -1, -1, 62/*原先是-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,//63/*原先是-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 };

/***@paramstr原始中文字符串

*@paramcharset进行二进制字符串转化的字符编码

*@return二进制字符串*/

public staticString encodeBuffer(String str)

{if (str == null)

{

str=String.Empty;

}try{returnBase64Utils.byteArrayToBase64(Encoding.UTF8.GetBytes(str));

}catch(Exception e)

{returnString.Empty;

}

}/*** 此方法为还原中文字符串转化后的二进制字符串

*@paramstr 二进制字符串

*@paramcharset 字符编码

*@return正常中文字符串*/

public staticString decodeBuffer(String str)

{if (str == null)

{return str =String.Empty;

}try{byte[] byteStr;try{

byteStr=Base64Utils.base64ToByteArray(str.Trim());

}catch(Exception e)

{returnString.Empty;

}returnEncoding.UTF8.GetString(byteStr);

}catch(Exception e)

{returnString.Empty;

}

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值