Untitled
unknown
plain_text
4 years ago
2.4 kB
9
Indexable
#include <bits/stdc++.h>
namespace base64 {
std::string decode(const std::string& input);
std::string encode(const std::string& input);
}
std::string base64 :: decode(const std::string& input)
{
std::string b ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/");
std::string y=input;
std::string ans("");
int s=y.size();
int A[8*s];
for(int i=0;i<s;i++)
{
int k=y[i];
for(int j=8*i+7;j>=8*i;j--)
{
A[j]=k%2;
k=k/2;
}
}
int m=(8*s)/6;
int k;
for(int i=0;i<m;i++)
{
k=0;
for(int j=6*i;j<6*i+6;j++)
k=k*2+A[j];
ans+=b[k];
}
if(8*s-m*6==2)
{
k=0;
for(int j=6*m;j<6*m+2;j++)
k=k*2+A[j];
k=k<<4;
ans+=b[k];
ans+="==";
}
else if(8*s-m*6==4)
{
k=0;
for(int j=6*m;j<6*m+6;j++)
k=k*2+A[j];
k=k<<2;
ans+=b[k];
ans+="=";
}
else if(8*s-m*6==6)
{
k=0;
for(int j=6*m;j<6*m+6;j++)
k=k*2+A[j];
ans+=b[k];
ans+="===";
}
return ans;
}
std::string base64 :: encode(const std::string& input)
{
std::string b ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/");
std::string y=input;
std::string ans("");
int s=y.size();
int A[6*s];
int d,num=0;
for(int i=0;i<s;i++)
{
d=0;
int k;
for(int j=0;j<64;j++)
{
if(b[j]==y[i])
k=j;
else
d++;
}
if(d<64)
{
for(int j=6*i+5;j>=6*i;j--)
{
A[j]=k%2;
k=k/2;
}
}
else
num++;
}
int k;
int len=(6*s-6*num)/8;
for(int i=0;i<len;i++)
{
k=0;
for(int j=8*i;j<8*i+8;j++)
k=k*2+A[j];
ans+=(char)k;
}
return ans;
}Editor is loading...