private static byte[] DoDecryptBytes(string input, SysCrypt.RSACryptoServiceProvider rsa) { try { int b64bs = (rsa.KeySize / 8 / 3 * 4) + ((rsa.KeySize / 8 % 3) != 0 ? 4 : 0); List<byte> res = new List<byte>(); for (int i = 0; i < input.Length / b64bs; i++) res.AddRange(rsa.Decrypt(Convert.FromBase64String(input.Substring(i * b64bs, b64bs)), true)); return res.ToArray(); } catch { return null; } }
private static string DoEncrypt(string input, SysCrypt.RSACryptoServiceProvider rsa) { if (input == null) return null; byte[] bytes = Encoding.UTF8.GetBytes(input); //int utf8bs = (rsa.KeySize / 8) - 2 - (2 * SysCrypt.SHA1.Create().ComputeHash(bytes).Length); int utf8bs = (rsa.KeySize / 8) - 42; int iterat = bytes.Length / utf8bs; StringBuilder sb = new StringBuilder(); for (int i = 0; i <= iterat; i++) { byte[] result = rsa.Encrypt(bytes.Skip(i * utf8bs).Take(utf8bs).ToArray(), true); //Array.Reverse(result); sb.Append(Convert.ToBase64String(result)); } return sb.ToString(); }
private static string DoDecrypt(string input, SysCrypt.RSACryptoServiceProvider rsa) { try { return Encoding.UTF8.GetString(DoDecryptBytes(input,rsa)); } catch { return null; } }