using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Security.Cryptography; namespace SogClient { public static class RSABigIntegerUtils { private static BigInteger bi_n; private static BigInteger bi_e; private static BigInteger bi_d; public static void GenRSAKey() { byte[] pseudoPrime1 = { (byte)0x85, (byte)0x84, (byte)0x64, (byte)0xFD, (byte)0x70, (byte)0x6A, (byte)0x9F, (byte)0xF0, (byte)0x94, (byte)0x0C, (byte)0x3E, (byte)0x2C, (byte)0x74, (byte)0x34, (byte)0x05, (byte)0xC9, (byte)0x55, (byte)0xB3, (byte)0x85, (byte)0x32, (byte)0x98, (byte)0x71, (byte)0xF9, (byte)0x41, (byte)0x21, (byte)0x5F, (byte)0x02, (byte)0x9E, (byte)0xEA, (byte)0x56, (byte)0x8D, (byte)0x8C, (byte)0x44, (byte)0xCC, (byte)0xEE, (byte)0xEE, (byte)0x3D, (byte)0x2C, (byte)0x9D, (byte)0x2C, (byte)0x12, (byte)0x41, (byte)0x1E, (byte)0xF1, (byte)0xC5, (byte)0x32, (byte)0xC3, (byte)0xAA, (byte)0x31, (byte)0x4A, (byte)0x52, (byte)0xD8, (byte)0xE8, (byte)0xAF, (byte)0x42, (byte)0xF4, (byte)0x72, (byte)0xA1, (byte)0x2A, (byte)0x0D, (byte)0x97, (byte)0xB1, (byte)0x31, (byte)0xB3, }; byte[] pseudoPrime2 = { (byte)0x99, (byte)0x98, (byte)0xCA, (byte)0xB8, (byte)0x5E, (byte)0xD7, (byte)0xE5, (byte)0xDC, (byte)0x28, (byte)0x5C, (byte)0x6F, (byte)0x0E, (byte)0x15, (byte)0x09, (byte)0x59, (byte)0x6E, (byte)0x84, (byte)0xF3, (byte)0x81, (byte)0xCD, (byte)0xDE, (byte)0x42, (byte)0xDC, (byte)0x93, (byte)0xC2, (byte)0x7A, (byte)0x62, (byte)0xAC, (byte)0x6C, (byte)0xAF, (byte)0xDE, (byte)0x74, (byte)0xE3, (byte)0xCB, (byte)0x60, (byte)0x20, (byte)0x38, (byte)0x9C, (byte)0x21, (byte)0xC3, (byte)0xDC, (byte)0xC8, (byte)0xA2, (byte)0x4D, (byte)0xC6, (byte)0x2A, (byte)0x35, (byte)0x7F, (byte)0xF3, (byte)0xA9, (byte)0xE8, (byte)0x1D, (byte)0x7B, (byte)0x2C, (byte)0x78, (byte)0xFA, (byte)0xB8, (byte)0x02, (byte)0x55, (byte)0x80, (byte)0x9B, (byte)0xC2, (byte)0xA5, (byte)0xCB, }; BigInteger bi_p = new BigInteger(pseudoPrime1); BigInteger bi_q = new BigInteger(pseudoPrime2); BigInteger bi_pq = (bi_p - 1) * (bi_q - 1); bi_n = bi_p * bi_q; Random rand = new Random(); bi_e = bi_pq.genCoPrime(32, rand); bi_d = bi_e.modInverse(bi_pq); } public static RSAParameters GetRSAParameters() { RSAParameters rp = new RSAParameters(); rp.Modulus = bi_n.getBytes(); rp.Exponent = bi_e.getBytes(); rp.D = bi_d.getBytes(); return rp; } public static string ToHexString(byte[] bytes) // 0xae00cf => "AE00CF " { string hexString = string.Empty; if (bytes != null) { StringBuilder strB = new StringBuilder(); for (int i = 0; i < bytes.Length; i++) { strB.Append(bytes[i].ToString("X2")); } hexString = strB.ToString(); } return hexString; } public static byte[] DecryptBytesPrivite(byte[] dataBytes) { BigInteger biText = new BigInteger(dataBytes); TraceLog.Trace("DecryptBytesPrivite enText {0}", biText.ToString()); BigInteger biEnText = biText.modPow(bi_d, bi_n); byte[] resultbyte = biEnText.getBytes(); return resultbyte; } public static byte[] EncryptBytesPublic(byte[] bytes) { BigInteger biText = new BigInteger(bytes); BigInteger biEnText = biText.modPow(bi_e, bi_n); byte[] outbytes = biEnText.getBytes(); return outbytes; } public static string EncryptStringPublic(string bytes) { BigInteger biText = new BigInteger(bytes,16); BigInteger biEnText = biText.modPow(bi_e, bi_n); string str = biEnText.ToHexString(); return str; } public static string DecryptStringPrivite(string dataBytes) { BigInteger biText = new BigInteger(dataBytes,16); BigInteger biEnText = biText.modPow(bi_d, bi_n); string str = biEnText.ToHexString(); return str; } public static void CheckError(byte[] bytes) { // encrypt and decrypt data BigInteger bi_data = new BigInteger(bytes, bytes.Length); BigInteger bi_encrypted = bi_data.modPow(bi_e, bi_n); byte[] trans = bi_encrypted.getBytes(); BigInteger bi_encrypted2 = new BigInteger(trans); BigInteger bi_decrypted = bi_encrypted.modPow(bi_d, bi_n); // compare if (bi_decrypted != bi_data) { Console.WriteLine("\nError "); Console.WriteLine(bi_data + "\n"); return; } byte[] encrypt = EncryptBytesPublic(bytes); byte[] decrypt = DecryptBytesPrivite(encrypt); } } }