base64加密辨识度太高,很多开发人员都能通过密文看出这种加密方式,然后解密得到明文。我们可以通过对密文进一步加工,使别人无法直接使用base64解密得到明文。
下面的代码是c#语法,基本思路是将加密之后的密文的后面的一小部分保留,然后我们将前面偶数位的密文,分成两部分前后调换,解密的思路与之是一样的。
class Tool
{
public string Encrypt(string plaintext)
{
string raw = Base64_encode(plaintext);
int len = 0;
if (raw.Length % 2 == 0)
{
len = raw.Length - 2;
}
else
{
len = raw.Length - 1;
}
string first = raw.Substring(0, len/2);
string second = raw.Substring(len/2,len/2);
string end = raw.Substring(len);
string ciphertext = second + first + end;
return ciphertext;
}
public string Decrypt(string ciphertext)
{
int len = 0;
if (ciphertext.Length % 2 == 0)
{
len = ciphertext.Length - 2;
}
else
{
len = ciphertext.Length - 1;
}
string first = ciphertext.Substring(0, len/2);
string second = ciphertext.Substring(len/2, len/2);
string end = ciphertext.Substring(len);
string raw = second + first + end;
string plaintext = Base64_decode(raw);
return plaintext;
}
public string Base64_encode(string plaintext)
{
byte[] bytes = Encoding.Default.GetBytes(plaintext);
return Convert.ToBase64String(bytes);
}
public string Base64_decode(string ciphertext)
{
byte[] bytes = Convert.FromBase64String(ciphertext);
return Encoding.Default.GetString(bytes);
}
}