-
-
Save eoner/af3fa0f6cd53bd4b8367 to your computer and use it in GitHub Desktop.
kisisorgu veritabanını encrypt/decrypt etmek için fonksiyonlar
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class SorguDecrypt | |
{ | |
// Şifrelenmiş veri alfabesi | |
private char[] outAlphabet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz/++".ToCharArray(); | |
// Şifrelenmemiş veri alfabesi (ISO-8859-9) | |
private char[] inAlphabet = null; | |
private Encoding turkishEncoding = null; | |
public SorguDecrypt() | |
{ | |
turkishEncoding = Encoding.GetEncoding("iso-8859-9"); | |
var bytes = Enumerable.Range(0, 255).Select(a => (byte)a).ToArray(); | |
inAlphabet = turkishEncoding.GetChars(bytes); | |
} | |
public string Encode(string clearText) | |
{ | |
if (String.IsNullOrEmpty(clearText)) return String.Empty; | |
StringBuilder encodedString = new StringBuilder(); | |
clearText = clearText.PadRight(((clearText.Length - 1) / 3 + 1) * 3, (char)0); | |
for (int j = 0; j < clearText.Length; j += 3) | |
{ | |
var bytes = turkishEncoding.GetBytes(clearText.Substring(j, 3)); | |
int c1 = (int)(bytes[0]); | |
int c2 = (int)(bytes[1]); | |
int c3 = (int)(bytes[2]); | |
int k1 = (c1) / 4; | |
int k2 = (c1) % 4; | |
int k3 = (c2) / 16; | |
int k4 = (c2) % 16; | |
int k5 = (c3) / 64; | |
int k6 = (c3) % 64; | |
char ec1 = outAlphabet[k1]; | |
char ec2 = outAlphabet[k3 + k2 * 16]; | |
char ec3 = outAlphabet[k5 + k4 * 4]; | |
char ec4 = outAlphabet[k6]; | |
encodedString.Append(ec1); | |
encodedString.Append(ec2); | |
if (c2 != 0) encodedString.Append(ec3); | |
if (c3 != 0) encodedString.Append(ec4); | |
} | |
return encodedString.ToString(); | |
} | |
public string Decode(string cipherText) | |
{ | |
if (String.IsNullOrEmpty(cipherText)) return String.Empty; | |
StringBuilder decodedString = new StringBuilder(); | |
cipherText = cipherText.PadRight(((cipherText.Length - 1) / 4 + 1) * 4, (char)0); | |
for (int j = 0; j < cipherText.Length; j += 4) | |
{ | |
string s = cipherText.Substring(j, 4); | |
int c1 = Array.IndexOf(outAlphabet,(s[0])); | |
int c2 = Array.IndexOf(outAlphabet, (s[1])) + (s[0] == '/' ? 64 : 0) ; | |
int c3 = Array.IndexOf(outAlphabet, (s[2])) + (s[1] == '/' ? 64 : 0) ; | |
int c4 = Array.IndexOf(outAlphabet, (s[3])) + (s[2] == '/' ? 64 : 0) ; | |
int r1 = (c2 * 16) / 255; | |
int r2 = (c3 * 64) / 255; | |
char dc1 = inAlphabet[(c1 * 4 + r1)]; | |
char dc2 = inAlphabet[((c2 * 16 + r2) % 256)]; | |
int r3 = Math.Min(254,(c3 - Array.IndexOf(inAlphabet, dc2) % 16 * 4) % 64 * 64 + c4); | |
char dc3 = c4 != -1 ? inAlphabet[r3] : (char)0; | |
decodedString.Append((dc1)); | |
if (c3 != -1) decodedString.Append((dc2)); | |
if (c4 != -1) decodedString.Append((dc3)); | |
} | |
return decodedString.ToString(); | |
} | |
public static void Test() | |
{ | |
SorguDecrypt decrypter = new SorguDecrypt(); | |
string enc = decrypter.Encode("DENEME"); | |
string dec = decrypter.Decode(enc); | |
Console.WriteLine("{0}: {1}",enc,dec); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment