public string GenerateSignature(string compactJson, string method, string nonceStr, string privateKey, string requestUrl, string signType, string timestamp, string environment) { string signedData = ""; try { //GenerateSignatureResult result = new GenerateSignatureResult(); string plainText = ""; if (compactJson != "") { string encodedData = Encode.Base64Encode(compactJson); plainText = String.Format("data={0}&method={1}&nonceStr={2}&requestUrl={3}&signType={4}×tamp={5}", encodedData, method, nonceStr, requestUrl, signType, timestamp); } else { plainText = String.Format("method={0}&nonceStr={1}&requestUrl={2}&signType={3}×tamp={4}", method, nonceStr, requestUrl, signType, timestamp); } byte[] plainTextByte = Encoding.UTF8.GetBytes(plainText); RSACryptoServiceProvider provider = PemKeyUtils.GetRSAProviderFromPemFile(privateKey); string prikey = provider.ToXmlString(true); byte[] signedBytes = provider.SignData(plainTextByte, CryptoConfig.MapNameToOID("SHA256")); signedData = Convert.ToBase64String(signedBytes); } catch (Exception ex) { Console.WriteLine(ex.Message); } return(signedData); }
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}"); }
public static string Encrypt(string plaintext, string publickey) { string ciphertext; RSACryptoServiceProvider pubKeyCSP = PemKeyUtils.GetRSAProviderFromPem(publickey); byte[] dataToEncrypt = Encoding.UTF8.GetBytes(plaintext); using (pubKeyCSP) { byte[] encrypted = pubKeyCSP.Encrypt(dataToEncrypt, true); ciphertext = Convert.ToBase64String(encrypted); } return(ciphertext); }
static void Main(string[] args) { RSACryptoServiceProvider provider = PemKeyUtils.GetRSAProviderFromPemFile(@"C:\Users\nmose\Desktop\rsa\rsa_2048_pub.pem"); AesCryptoServiceProvider aes = new AesCryptoServiceProvider(); string originFile = @"C:\Users\nmose\Desktop\rsa\info.txt"; string encryFile = @"C:\Users\nmose\Desktop\rsa\info-crypto.bin"; string decrypFile = @"C:\Users\nmose\Desktop\rsa\info-decrypto.txt"; string msg = ""; CryptoFile.GenerateEncryFile(provider, aes, originFile, encryFile, ref msg); RSACryptoServiceProvider providerPrivate = PemKeyUtils.GetRSAProviderFromPemFile(@"C:\Users\nmose\Desktop\rsa\rsa_2048_priv.pem"); CryptoFile.DecrypFile(providerPrivate, encryFile, decrypFile, ref msg); }
//OAuth oauth_consumer_key = "jira", oauth_nonce = "LyiqlQJcnUmcvqNvMPJLzNyPTEQnlKFFrFqlGayGg", oauth_signature_method = "RSA-SHA1", oauth_timestamp = "1539430294", oauth_version = "1.0", oauth_token = "fdfgfgfg", oauth_signature = "X%2F%2BVnUmrxKOqQxgXPJ6n1kBTMI1Igq7b1Cd6Kziq8QbkF7yVZGnP8c6azof0hlzf2LF013oFqDnf2NEpvSqeCWpn7YPg7PgAzEY%2Bzn5RI3NQcXsGMoSLZeVjQL%2FthWbAzeAfZUrz9fx2tV%2BQzSZS7bpmQ%2BeShO5IEGeq6EjgajU%3D" private void SetAuthorizatonHeader(HttpClient client, Model.UserSession userSession, string url, string requestType) { var key = PemKeyUtils.GetRSAProviderFromString(userSession.AuthClientConfig.ConsumerSecret); var oauth = new OAuth.OAuthRequest(); oauth.RequestUrl = url; oauth.Method = requestType; oauth.ConsumerKey = userSession.AuthClientConfig.ConsumerKey; oauth.ConsumerSecret = userSession.AuthClientConfig.ConsumerSecret; oauth.SignatureMethod = OAuthSignatureMethod.RsaSha1; oauth.Token = userSession.AccessToken; oauth.TokenSecret = userSession.AccessSecret; oauth.Version = "1.0"; oauth.Type = OAuthRequestType.ProtectedResource; var authorizationHeader = oauth.GetAuthorizationHeader(); client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("OAuth", authorizationHeader.Substring(5)); }
public static byte[] RSAEncryptBytes(byte[] dataToEncrypt, string publicKeyPath) { RSACryptoServiceProvider provider = PemKeyUtils.GetRSAProviderFromPemFile(publicKeyPath); return(provider.Encrypt(dataToEncrypt, false)); }