Esempio n. 1
0
	    public static string sign(string content, string privateKey)
        {
            try
            {
                PKCS8EncodedKeySpec priPKCS8 = new PKCS8EncodedKeySpec(Convert.FromBase64String(privateKey));
                        //Base64.decode(privateKey));
                KeyFactory keyf = KeyFactory.GetInstance(ALGORITHM, "BC");
                IPrivateKey priKey = keyf.GeneratePrivate(priPKCS8);

                Signature signature = Signature.GetInstance(SIGN_ALGORITHMS);

                signature.InitSign(priKey);
                signature.Update(Encoding.Default.GetBytes(content));

                byte[] signed = signature.Sign();
                
                return Convert.ToBase64String(signed);
            }
            catch (Exception e)
            {
                string mesg = e.StackTrace;
            }

            return null;
        }
Esempio n. 2
0
        private string GetSha256(string key, string data)
        {
            string trimmedKey = key.Replace("-----\\w+ PRIVATE KEY-----", "").Replace("\\s", "");

            try
            {
                byte[] result = Android.Util.Base64.Decode(trimmedKey, Android.Util.Base64Flags.Default);

                var str     = new Java.Lang.String(data);
                var factory = Java.Security.KeyFactory.GetInstance("RSA");
                Java.Security.Spec.EncodedKeySpec keySpec = new Java.Security.Spec.PKCS8EncodedKeySpec(result);
                var signature = Java.Security.Signature.GetInstance("SHA256withRSA");
                var s         = factory.GeneratePrivate(keySpec);
                signature.InitSign(s);
                signature.Update(str.GetBytes());

                byte[] encrypted = signature.Sign();
                return(Android.Util.Base64.EncodeToString(encrypted, Android.Util.Base64Flags.NoWrap));
            }
            catch (Exception e)
            {
            }
            return(null);
        }