關閉
標題:simpleEncode
內容:
js:
function simpleDecode(data) {
const key = 0x5A;
let bytes = Uint8Array.from(atob(data), c => c.charCodeAt(0));
for (let i = 0; i < bytes.length; i++)
bytes[i] ^= key;
return new TextDecoder().decode(bytes);
}
function simpleEncode(text) {
const key = 0x5A;
let bytes = new TextEncoder().encode(text);
for (let i = 0; i < bytes.length; i++)
bytes[i] ^= key;
return btoa(String.fromCharCode(...bytes));
}
C#:
public string simpleEncode(string data)
{
byte key = 0x5A;
byte[] bytes = Encoding.UTF8.GetBytes(data);
for (int i = 0; i < bytes.Length; i++)
bytes[i] ^= key;
return Convert.ToBase64String(bytes);
}
public string simpleDecode(string data)
{
byte key = 0x5A;
byte[] bytes = Convert.FromBase64String(data);
for (int i = 0; i < bytes.Length; i++)
bytes[i] ^= key;
return Encoding.UTF8.GetString(bytes);
}