using System.Collections.Generic;using System.Linq;classBase91{
publicstaticstring Decode (string input ){
string stringTable ="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!#$%&()*+,./:;<=>?@[]^_`{|}~\"";
Dictionary<byte,int> stringDecode=newDictionary<byte,int>();for(int i =0; i < stringTable. Length; i++){
stringDecode [(byte)stringTable [ i ]]= i;}string output ="";int c =0;int v =-1;int b =0;int n =0;for(int i =0; i < input. Length; i++){
c = stringDecode [(byte)input [ i ]];if( c ==-1)continue;if( v <0){
v = c;}else{
v += c *91;
b |= v << n;
n +=( v &8191)>88?13:14;do{
output +=(char)( b &255);
b >>=8;
n -=8;}while( n >7);
v =-1;}}if( v +1!=0){
output +=(char)(( b | v << n )&255);}return output;}publicstaticstring Encode (string s ){
string output ="";int b =0;int n =0;int v =0;string stringTable ="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!#$%&()*+,./:;<=>?@[]^_`{|}~\"'";for(int i =0; i < s. Length; i++){
b |=(byte)s [ i ]<< n;
n +=8;if( n >13){
v = b &8191;if( v >88){
b >>=13;
n -=13;}else{
v = b &16383;
b >>=14;
n -=14;}
output +=(char)stringTable [ v %91];
output +=(char)stringTable [ v /91];}}if( n !=0){
output +=(char)stringTable [ b %91];if( n >7|| b >90)
output +=(char)stringTable [ b /91];}return output;}}
答案2:
using System.Collections.Generic;using System.Linq;classBase91{
privatestaticreadonlychar[] _encodeTable =newchar[]{
'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','!','#','$','%','&','(',')','*','+',',','.','/',':',';','<','=','>','?','@','[',']','^','_','`','{','|','}','~','"'};privatestaticreadonly Dictionary<byte,int> _decodeTable =newDictionary<byte,int>();staticBase91(){
InitDecodeTable();}privatestaticvoidInitDecodeTable(){
for(byte i =0; i <255; i++)
_decodeTable[i]=-1;for(int i =0; i < _encodeTable.Length; i++)
_decodeTable[(byte)_encodeTable[i]]= i;}publicstaticstringEncode(string input){
string output ="";int b =0;int n =0;for(int i =0; i < input.Length; i++){
b |=(byte)input[i]<< n;
n +=8;if(n >13){
int v = b &8191;if(v >88){
b >>=13;
n -=13;}else{
v = b &16383;
b >>=14;
n -=14;}
output += _encodeTable[v %91];
output += _encodeTable[v /91];}}if(n !=0){
output += _encodeTable[b %91];if(n >7|| b >90)
output += _encodeTable[b /91];}return output;}publicstaticstringDecode(string input){
string output ="";int v =-1;int b =0;int n =0;for(int i =0; i < input.Length; i++){
int c = _decodeTable[(byte)input[i]];if(c ==-1)continue;if(v <0){
v = c;}else{
v += c *91;
b |= v << n;
n +=(v &8191)>88?13:14;do{
output +=(char)(b &255);
b >>=8;
n -=8;}while