C#BASE64 UTF8字符串加密解密
base 64 解码
base64 bb = new base64();
string orgStr= Encoding.Default.GetString(bb.GetDecoded("base64编译后的字符"));
UTF8
Subject = Encoding.GetEncoding("utf-8").GetString(Convert.FromBase64String("utf编译后的字符"));
base64加密
public class base64
{
char[] source;
int length, length2, length3;
int blockCount;
int paddingCount;
public static base64 Decoder = new base64();
public base64()
{ }
private void init(char[] input)
{
int temp = 0;
source = input;
length = input.Length; for (int x = 0; x < 2; x++)
{
if (input[length - x - 1] == '=')
temp++;
}
paddingCount = temp; blockCount = length / 4;
length2 = blockCount * 3;
}
/// <summary>
/// 加密后的字符串解密
/// </summary>
/// <param name="strInput"></param>
/// <returns></returns>
public byte[] GetDecoded(string strInput)
{
//初始化
init(strInput.ToCharArray()); byte[] buffer = new byte[length];
byte[] buffer2 = new byte[length2]; for (int x = 0; x < length; x++)
{
buffer[x] = char2sixbit(source[x]);
} byte b, b1, b2, b3;
byte temp1, temp2, temp3, temp4; for (int x = 0; x < blockCount; x++)
{
temp1 = buffer[x * 4];
temp2 = buffer[x * 4 + 1];
temp3 = buffer[x * 4 + 2];
temp4 = buffer[x * 4 + 3]; b = (byte)(temp1 << 2);
b1 = (byte)((temp2 & 48) >> 4);
b1 += b; b = (byte)((temp2 & 15) << 4);
b2 = (byte)((temp3 & 60) >> 2);
b2 += b; b = (byte)((temp3 & 3) << 6);
b3 = temp4;
b3 += b; buffer2[x * 3] = b1;
buffer2[x * 3 + 1] = b2;
buffer2[x * 3 + 2] = b3;
} length3 = length2 - paddingCount;
byte[] result = new byte[length3]; for (int x = 0; x < length3; x++)
{
result[x] = buffer2[x];
} return result;
} private byte char2sixbit(char c)
{
char[] lookupTable = new char[64]{
'A','B','C','D','E','F','G','H','I','J','K','L','M','N',
'O','P','Q','R','S','T','U','V','W','X','Y', 'Z',
'a','b','c','d','e','f','g','h','i','j','k','l','m','n',
'o','p','q','r','s','t','u','v','w','x','y','z',
'0','1','2','3','4','5','6','7','8','9','+','/'};
if (c == '=')
return 0;
else
{
for (int x = 0; x < 64; x++)
{
if (lookupTable[x] == c)
return (byte)x;
} return 0;
} } /// <summary>
/// Base64解密,采用utf8编码方式解密
/// </summary>
/// <param name="result">待解密的密文</param>
/// <returns>解密后的字符串</returns>
public static string DecodeBase64(string result)
{
return DecodeBase64(Encoding.UTF8, result);
}
/// <summary>
/// Base64解密
/// </summary>
/// <param name="codeName">解密采用的编码方式,注意和加密时采用的方式一致</param>
/// <param name="result">待解密的密文</param>
/// <returns>解密后的字符串</returns>
public static string DecodeBase64(Encoding encode, string result)
{
string decode = "";
byte[] bytes = Convert.FromBase64String(result);
try
{
decode = encode.GetString(bytes);
}
catch
{
decode = result;
}
return decode;
}
/// <summary>
/// Base64加密
/// </summary>
/// <param name="codeName">加密采用的编码方式</param>
/// <param name="source">待加密的明文</param>
/// <returns></returns>
public static string EncodeBase64(Encoding encode, string source)
{
string decode = "";
byte[] bytes = encode.GetBytes(source);
try
{
decode = Convert.ToBase64String(bytes);
}
catch
{
decode = source;
}
return decode;
} /// <summary>
/// Base64加密,采用utf8编码方式加密
/// </summary>
/// <param name="source">待加密的明文</param>
/// <returns>加密后的字符串</returns>
public static string EncodeBase64(string source)
{
return EncodeBase64(Encoding.UTF8, source);
}
}
C#BASE64 UTF8字符串加密解密的更多相关文章
- C# 字符串加密解密函数
原文:C# 字符串加密解密函数 using System; using System.Text;using System.Security.Cryptography; using System.IO; ...
- 简单的JavaScript字符串加密解密
简单的JavaScript字符串加密解密 <div> <input type="text" id="input" autofocus=&quo ...
- java字符串加密解密
java字符串加密解密 字符串加密解密的方式很多,每一种加密有着相对的解密方法.下面要说的是java中模拟php的pack和unpack的字符串加密解密方法. java模拟php中pack: /** ...
- 用C#实现Base64处理,加密解密,编码解码
using System; using System.Text; namespace Common { /// <summary> /// 实现Base64加密解密 /// 作者:周公 / ...
- C# 字符串加密解密方法
这个是加密的算法的命名空间,使用加密算法前要引用该程序集 System.Security.Cryptography using System;using System.Data;using Syst ...
- 在JavaWeb项目中URL中字符串加密解密方案
URL由来: 一般来说,URL只能使用英文字母.阿拉伯数字和某些标点符号,不能使用其他文字和符号.比如,世界上有英文字母的网址 “http://www.abc.com”,但是没有希腊字母的网址“htt ...
- NET实现RSA AES DES 字符串 加密解密以及SHA1 MD5加密
本文列举了 数据加密算法(Data Encryption Algorithm,DEA) 密码学中的高级加密标准(Advanced EncryptionStandard,AES)RSA公钥加密算法 ...
- base64,base32bit加密解密
import base64 str='admin' str=str.encode('utf-8') #加密 bs64=base64.b64encode(str) #解密 debs64=base64.b ...
- 从网上整理的一些delphi字符串加密解密方法
function Encode(Str: string): string; var //加密 TmpChr: AnsiChar; i, Len: integer; begin Result := St ...
随机推荐
- spark on yarn :state: ACCEPTED一直 出现
今天运行spark on yarn 一直出现 16/09/20 18:40:41 INFO yarn.Client: Application report for application_147417 ...
- AfxSocketInit()
作用:初始化Windows套接字 原型:BOOL AfxSocketInit(WSADATA* lpwsaData = NULL ); 参数:lpwsaData 指向WSADATA结构的指针. ...
- WARNING [Project: :app] To shrink resources you must also enable ProGuard
新版本的Android Gradle plugin中,对于resource有了更加一步的管理,可以把unused resource移除,不仅是自己工程,并且library里面也可以没有用到的,也可以移 ...
- Javascript基本概念(语句和函数)
语句 for语句 for语句中的初始化表达式,控制表达式和循环后表达式都是可选的,将这三个表达式省略,就会创建一个无线循环. ECMAScript中不存在块级作用域,因此在循环内容部定义的变量也可以在 ...
- 在github搭建你的个人主页
一. 有没有遇到过在简历上要求写项目地址的经历.或者面试时面试官问你的项目在线地址是多少. 二. github 不但有代码托管的功能,还可以搭建在线演示项目,对于一个没有多少私房钱的穷屌丝,这个是不是 ...
- linux 如何禁用账号和解除禁用账号
把账号禁用可以有几个方法:1. # usermod -L <username> # usermod -U <username> // 解除禁用2. 修改/etc/passwd文 ...
- struts2之动态方法调用(转)
转自:http://blog.csdn.net/longwentao/article/details/6940289 当我们访问一个Action时,默认是访问execute()方法,但当在一个Acti ...
- LED发光二极管
半导体发光器件包括半导体发光二极管(简称LED).数码管.符号管.米字管及点阵式显示屏(简称矩阵管)等.事实上,数码管.符号管.米字管及矩阵管中的每个发光单元都是一个发光二极管. 一. 半导体发光二极 ...
- 基于IAP和Keil MDK的远程升级设计
写在前面:三个周之前,我突然想写一个远程升级的程序.那个时候我只是大概知道IAP的意思是在应用编程,但怎么编,我还一无所知.我给自己定下一个个阶段目标,从最基础的代码一点点写起,解决一个又一个的问题. ...
- 谷歌page speed 安装使用及页面问题详解
原文地址:http://wenku.baidu.com/view/b0a61f3ebcd126fff7050b40.html 谷歌page speed 安装使用及页面问题详解 谷歌page speed ...