public string Encrypt(string target) { var key = "-----BEGIN PUBLIC KEY-----\YOURKEY\n-----END PUBLIC KEY-----"; var rsaProvider = PemKeyUtils.GetRSAProviderFromPemString(key); RSAEncyptionManager manager = new RSAEncyptionManager(rsaProvider, RSAEncryptionPadding.Pkcs1); return(Convert.ToBase64String(manager.Encrypt(target))); }
protected override void OnCreate(Bundle savedInstanceState) { rsaProvider = PemKeyUtils.GetRSAProviderFromPemString(key); base.OnCreate(savedInstanceState); shp = Application.Context.GetSharedPreferences("ewosettings", FileCreationMode.Append); edtr = shp.Edit(); if (!shp.Contains("Area")) { edtr.PutString("Area", ""); edtr.Commit(); } if (!shp.Contains("name")) { edtr.PutString("name", "invalid"); edtr.Commit(); } if (!shp.Contains("token")) { edtr.PutString("token", "invalid"); edtr.Commit(); } if (!shp.Contains("record_id")) { edtr.PutString("record_id", "not_found"); edtr.Commit(); } if (shp.GetString("record_id", "not_found") != "not_found") { StartActivity(typeof(MainActivity)); } SetContentView(Resource.Layout.InitailSettings); tv1 = FindViewById <TextView>(Resource.Id.textView1); ed1 = FindViewById <EditText>(Resource.Id.tokenEdit); phoneText = FindViewById <EditText>(Resource.Id.phoneEdit); valText = FindViewById <EditText>(Resource.Id.validEditText); emailText = FindViewById <EditText>(Resource.Id.emailEdit); tokenID = FindViewById <TextView>(Resource.Id.tokenID); tv2 = FindViewById <TextView>(Resource.Id.textView2); tv4 = FindViewById <TextView>(Resource.Id.textView4); tv5 = FindViewById <TextView>(Resource.Id.textView5); tv6 = FindViewById <TextView>(Resource.Id.textView6); tv7 = FindViewById <TextView>(Resource.Id.textView7); tv8 = FindViewById <TextView>(Resource.Id.textView8); userName = FindViewById <TextView>(Resource.Id.userNameText); userDept = FindViewById <TextView>(Resource.Id.depText); b1 = FindViewById <Button>(Resource.Id.button1); b3 = FindViewById <Button>(Resource.Id.button3); b2 = FindViewById <Button>(Resource.Id.button2); b3.Click += requestValidation; b2.Click += B2_Click; b1.Click += B1_Click; switchUI(true); // Create your application here }
public static string GetEncryptedPassword(this IInstaApi api, string password, long?providedTime = null) { var pubKey = api.GetLoggedUser().PublicKey; var pubKeyId = api.GetLoggedUser().PublicKeyId; byte[] randKey = new byte[32]; byte[] iv = new byte[12]; secureRandom.NextBytes(randKey, 0, randKey.Length); secureRandom.NextBytes(iv, 0, iv.Length); long time = providedTime ?? DateTime.UtcNow.ToUnixTime(); byte[] associatedData = Encoding.UTF8.GetBytes(time.ToString()); var pubKEY = Encoding.UTF8.GetString(Convert.FromBase64String(pubKey)); byte[] encryptedKey; using (var rdr = PemKeyUtils.GetRSAProviderFromPemString(pubKEY.Trim())) encryptedKey = rdr.Encrypt(randKey, false); byte[] plaintext = Encoding.UTF8.GetBytes(password); var cipher = new GcmBlockCipher(new AesEngine()); var parameters = new AeadParameters(new KeyParameter(randKey), 128, iv, associatedData); cipher.Init(true, parameters); var ciphertext = new byte[cipher.GetOutputSize(plaintext.Length)]; var len = cipher.ProcessBytes(plaintext, 0, plaintext.Length, ciphertext, 0); cipher.DoFinal(ciphertext, len); var con = new byte[plaintext.Length]; for (int i = 0; i < plaintext.Length; i++) { con[i] = ciphertext[i]; } ciphertext = con; var tag = cipher.GetMac(); byte[] buffersSize = BitConverter.GetBytes(Convert.ToInt16(encryptedKey.Length)); byte[] encKeyIdBytes = BitConverter.GetBytes(Convert.ToUInt16(pubKeyId)); if (BitConverter.IsLittleEndian) { Array.Reverse(encKeyIdBytes); } encKeyIdBytes[0] = 1; var payload = Convert.ToBase64String(encKeyIdBytes.Concat(iv).Concat(buffersSize).Concat(encryptedKey).Concat(tag).Concat(ciphertext).ToArray()); return($"#PWD_INSTAGRAM:4:{time}:{payload}"); }