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 ...
随机推荐
- 回滚Swtichover
从11.2.0.2开始,如果由于某种原因switchover没有成功,可以回滚switchover. For physical standby databases in situations wher ...
- IOS支付宝支付出现6002问题的解决办法
运行支付宝官方demo进行支付测试,会出现6002-网络连接错误,是因为以iOS9 SDK编译的工程会默认以SSL安全协议进行网络传输,即HTTPS,如果依然使用HTTP协议请求网络会报系统异常并中断 ...
- Vijos 1120 花生采摘
和“过河”一样,在很久以前我也曾经尝试过做这道题,可无奈当时没有看清题目.题目上说要先找最大的,然后次之.当时纠结了好久,可惜没做出来.但是现在就很好做了,只需要用结构体把每一个花生植株记录下来,so ...
- Google搜索的配置方法
在百度慢慢沦落为广告商的搜索引擎之后,对于一个追求技术的程序员,他所要追求的搜索引擎永远都应该是google. 下面保存一下我使用的能够FQ实现google搜索的方法和一些面试的测试账号. 小飞机sh ...
- ubuntu 12.04 安装谷歌浏览器
http://hi.baidu.com/kevin276/item/29bc1c96a208fabc82d29542 sudo dpkg -i google-chrome-stable_current ...
- LINQ 联合查询
List<Attachment> imgList = (from a in ZQSDWEBEntities.Attachment ...
- Oracle EBS-SQL (PO-6):检查订单接收总数.sql
SELECT sum(rcvt.quantity) 接收事务处理汇总数--已排除退货 --rsh.receipt_num 收据号, --pov.vendor_nam ...
- Oracle EBS-SQL (INV-2):检查帐户别名发放记录.sql
SELECT FU.description 操作者, ITM.SEGMENT1 物料编码, ITM.DESC ...
- POJ——多项式的加法
1:多项式加法 查看 提交 统计 提问 总时间限制: 1000ms 内存限制: 5000kB 描述 我们经常遇到两多项式相加的情况,在这里,我们就需要用程序来模拟实现把两个多项式相加到一起.首先 ...
- 艰苦的RAW格式数据恢复之旅
艰苦的RAW格式数据恢复之旅 1.RAW 格式形成原因 2.RAW 格式的解决的方法 经验之谈: 1.RAW 格式形成原因 关于形成的原因,在网上搜索了下,千奇百怪的都有,就不一一诉说了,可是有果必有 ...