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. 《SLIC Superpixels》阅读笔记

    原始链接:http://blog.csdn.net/jkhere/article/details/16819285 或许有改动,请参考原文! SLIC 超像素(SLICSuperpixels) Rad ...

  2. Kinect+OpenNI+OpenCV使用

    关于OpenNI,已经可以使用2.0,可以不再使用PrimeSense: 这里的是转载其他人的 OpenCV系列: 原文:http://blog.csdn.net/chenxin_130/articl ...

  3. 开发一个 App 有多难?说出来你可能不信!

    上图为程序员客栈儿童类视频APP 1.开发一个APP有多难?说实话,单纯地从开发来说,开发一个APP没有那么难.如果一款APP的基本功能点确定了,开发时间一般为1-2个月就完成了,费用大约5-10万的 ...

  4. BZOJ 3940: [Usaco2015 Feb]Censoring AC自动机_栈

    Description Farmer John has purchased a subscription to Good Hooveskeeping magazine for his cows, so ...

  5. python笔记4----字典

    1.哈希: 输入任意长度,输出固定长度. 即判断是否哈希,即判断可不可变. 2.创建字典 (1)直接创建:dic={1:'a',2:'b',3:'c'} (2)dict函数创建: list=[(1,' ...

  6. Apache Http Client 4 上传多个文件 (示例代码可在 github 上找到)

    转自:http://www.baeldung.com/httpclient-multipart-upload Multipart Upload with HttpClient 4 1. Overvie ...

  7. redis 参数配置总结

    redis.conf 配置项说明如下 1. Redis默认不是以守护进程的方式运行,可以通过该配置项修改,使用yes启用守护进程   daemonize no 2. 当Redis以守护进程方式运行时, ...

  8. vue+Ueditor集成 [前后端分离项目][图片、文件上传][富文本编辑]

    后端DEMO:https://github.com/coderliguoqing/UeditorSpringboot 前端DEMO:https://github.com/coderliguoqing/ ...

  9. json_encode把中文字符的数组转为json格式

    function ch_json_encode($data) { /** * 将中文编码 * @param array $data * @returnstring */ function ch_url ...

  10. Android网络编程(十)Retrofit2后篇[注解]

    G相关文章 Android网络编程(一)HTTP协议原理 Android网络编程(二)HttpClient与HttpURLConnection Android网络编程(三)Volley用法全解析 An ...