000

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace Libraries
{
public class Base64Crypt
{
private string S;
private string K;
private List<char> T;
public Base64Crypt()
{
T = new List<char>();
K = "あいうえおかきくけこさしすせそたちつてとなにぬねのはひふへほまみむめもやよらりるれろわをぐげござじずぞだぢづでばびぶべぱぴぷぺぽ";
//K = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";//标准码表
}
public string Token
{
get
{
return S == null ? K : S;
}
set
{
T.Clear();
S = value;
if (S == null)
{
foreach (var item in K)
{
T.Add(item);
}
}else if (S.Length < )
{
foreach (var item in S)
{
T.Add(item);
}
for (int i = ; i < -S.Length; i++)
{
T.Add(K[i]);
}
}
else
{
for (int i = ; i < ; i++)
{
T.Add(S[i]);
}
}
}
} public string Encode(string x)
{
return string.IsNullOrEmpty(x) ? x : InternalEncode(Encoding.UTF8.GetBytes(x));
}
public string Decode(string x)
{
return string.IsNullOrEmpty(x) ? x : Encoding.UTF8.GetString(InternalDecode(x));
} public byte[] Encode(byte[] x)
{
return x == null ? null : Encoding.UTF8.GetBytes(InternalEncode(x));
}
public byte[] Decode(byte[] x)
{
return x == null ? null : InternalDecode(Encoding.UTF8.GetString(x));
}
private void CheckToken()
{
if (T.Count != )
{
Token = K;
}
}
private byte[] InternalDecode(string x)
{
CheckToken();
byte[] r;
string t;
int p = ;
int m = x.Length / ;
int n = x.Length % ;
if (n == )
{
r = new byte[ * m];
}
else
{
r = new byte[ * m + n-];
t = string.Empty; for (int i = n; i > ; i--)
{
t += ByteToBin((byte)T.IndexOf(x[x.Length - i])).Substring();
} for (int i = ; i < n- ; i++)
{
r[ * m + i] = BinToByte(t.Substring( * i, ));
}
}
for (int i = ; i < m; i++)
{
t = string.Empty;
for (int j = ; j < ; j++)
{
t += ByteToBin((byte)T.IndexOf(x[*i+j])).Substring();
}
for (int j = ; j < t.Length/; j++)
{
r[p++] = BinToByte(t.Substring(*j,));
}
}
return r;
}
private string InternalEncode(byte[] x)
{
CheckToken();
string r = string.Empty;
string t;
int m = x.Length / ;
int n = x.Length % ;
for (int i = ; i < m; i++)
{
t = string.Empty;
for (int j = ; j < ; j++)
{
t += ByteToBin(x[ * i + j]);
}
r += base64Encode(t);
} if (n == )
{
t = ByteToBin(x[x.Length-]).PadRight(,'');
r += base64Encode(t);
}
else if (n == )
{
t = string.Empty;
for (int i = n; i > ; i--)
{
t += ByteToBin(x[x.Length - i]);
}
t = t.PadRight(,'');
r += base64Encode(t);
}
return r;
}
private string base64Encode(string x)
{
string r = string.Empty;
for (int i = ; i < x.Length / ; i++)
{
r += T[BinToByte(x.Substring( * i, ))];
}
return r;
} private string ByteToBin(byte x)
{
return Convert.ToString(x,).PadLeft(,'');
}
private byte BinToByte(string x)
{
return Convert.ToByte(x,);
} }
}

c#可自定义码表的base64加密解密算法类的更多相关文章

  1. Base64加密解密原理以及代码实现(VC++)

    Base64加密解密原理以及代码实现 转自:http://blog.csdn.net/jacky_dai/article/details/4698461 1. Base64使用A--Z,a--z,0- ...

  2. JS实现base64加密解密

    JS实现base64加密解密 转载自http://blog.csdn.net/fengzheng0306/archive/2006/04/25/676055.aspx 方法一: <HTML> ...

  3. 【代码笔记】iOS-3DES+Base64加密解密

    一,工程目录. 二,代码. RootViewController.m #import "RootViewController.h" #import "NSString+T ...

  4. 实现Base64加密解密

    using System; using System.Text;   namespace Common { /// <summary> /// 实现Base64加密解密 /// </ ...

  5. Java中使用BASE64加密&解密

    package com.bao.tools.encryption; import java.io.IOException; import org.junit.Test; import sun.misc ...

  6. Java Base64 加密解密

    使用JDK的类 BASE64Decoder  BASE64Encoder package test; import sun.misc.BASE64Decoder; import sun.misc.BA ...

  7. php使用base64加密解密图片

    php使用base64加密解密图片的实例代码. 例子: <?php //文件名:base64.php $data="/9j/4AAQSkZJRgABAQAAAQABAAD/2wCEAB ...

  8. Base64加密解密

    /// <summary> /// 实现Base64加密解密 /// </summary> public sealed class Base64 { /// <summa ...

  9. password学3——Java BASE64加密解密

    Base64是网络上最常见的用于传输8Bit字节代码的编码方式之中的一个,大家能够查看RFC2045-RFC2049.上面有MIME的具体规范.Base64编码可用于在HTTP环境下传递较长的标识信息 ...

随机推荐

  1. 安装mysql-python的遇到的问题

    最近更新环境后遇到安装mysql-python的问题,感觉挺折腾的,记录一下. 安装mysql-python的时候一直提示下面的错误 _mysql.c() : fatal error C1083: C ...

  2. layui table 时间戳

    , { field: , title: '时间', templet: '<div>{{ laytpl.toDateString(d) }}</div>' }, 或者 , { f ...

  3. pandaboy Merry Christmas

  4. 序列模型(3)---LSTM(长短时记忆)

    摘自https://www.cnblogs.com/pinard/p/6519110.html 一.RNN回顾 略去上面三层,即o,L,y,则RNN的模型可以简化成如下图的形式: 二.LSTM模型结构 ...

  5. C语言基础 (12) 文件的操作 FILE

    课程回顾 结构体基本操作: 结构体类型的定义 // struct为关键字 Stu为自定义标识符 // struct Stu才是结构体类型 // 结构体成员不能在定义类型时赋值 struct Stu { ...

  6. UNIX时间转换ASP代码.txt

    '参数:strTime:要转换的时 间:intTimeZone:该时间对应的时区 '返回值:strTime相对于1970年1月1日午夜0点经过的秒数 '示例:ToUnixTime("2008 ...

  7. [CodeForces]786B Legacy

    线段树优化建图. 建立两棵线段树,其上点的点权分别表示"到达这个区间内所有点的最小花费"和"到达这个区间内任意一个点的最小花费". 对于第一种路直接加边即可 对 ...

  8. [TJOI2008]彩灯

    线性基裸题,求最大线性无关组. 注意:1ll<<i #include <cstdio> int n,m; const int mod=2008; long long b[64] ...

  9. leetCode笔记--binary tree

    993. Cousins in Binary Tree In a binary tree, the root node is at depth 0, and children of each dept ...

  10. jsp js action之间传值

    1.struts2 action如何向JSP的JS函数传值 action中定义变量 public class TestAction extends ActionSupport implements S ...