Example #1
0
        public string EncryptData(string msg)
        {
            if (Init())
            {
                if (_type == 1)
                {
                    SHA256 sha256Hash = SHA256.Create();
                    byte[] bytes      = sha256Hash.ComputeHash(Encoding.UTF8.GetBytes(msg));
                    var    rsa        = RSA.Create();
                    rsa.KeySize = 1024;
                    rsa.ImportRSAPrivateKey(Convert.FromBase64String(_privateKey), out int byteReads);
                    var    signed = rsa.SignData(bytes, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1);
                    string result = Convert.ToBase64String(signed);

                    return(result);
                }
                else
                {
                    PGPLib pgp    = new PGPLib();
                    var    signed = pgp.SignString(msg, new FileInfo(_privateKey), _pgpKeyPassword);
                    return(signed);
                }
            }
            else
            {
                return(null);
            }
        }
Example #2
0
        public String encrypet(string InputText, string path, int ID)
        {
            String plainString = InputText;

            // create an instance of the library
            PGPLib   pgp          = new PGPLib();
            Studnets loginStudent = new Studnets();
            DataRow  loginS       = loginStudent.drSearchStudentEmail(ID);
            string   password     = null;

            if (loginS != null)
            {
                password = loginS["Password"].ToString();
            }
            String signedString = "";

            try
            {
                signedString = pgp.SignString(plainString,
                                              new FileInfo(path),
                                              password);
            }
            catch
            {
                return("false");
            }
            return(signedString);
        }
Example #3
0
        public string EncryptData(string msg, string code = "")
        {
            if (Init())
            {
                if (_type == 1)
                {
                    //SHA256 sha256Hash = SHA256.Create();
                    // byte[] bytes = sha256Hash.ComputeHash(Encoding.UTF8.GetBytes(msg));
                    //var key = Encoding.UTF8.GetBytes(code);
                    //HMACMD5 hmac = new HMACMD5(key);
                    // Convert the input string to a byte array and compute the hash.
                    //byte[] bytes = hmac.ComputeHash(Encoding.UTF8.GetBytes(msg));

                    var rsa = RSA.Create();
                    rsa.KeySize = _keySize;
                    rsa.ImportRSAPrivateKey(Convert.FromBase64String(_privateKey), out int byteReads);
                    var r = rsa.SignData(Encoding.UTF8.GetBytes(msg), HashAlgorithmName.MD5, RSASignaturePadding.Pkcs1);
                    //string result = Convert.ToBase64String(r);
                    StringBuilder sBuilder = new StringBuilder();

                    // Loop through each byte of the hashed data
                    // and format each one as a hexadecimal string.
                    for (int i = 0; i < r.Length; i++)
                    {
                        sBuilder.Append(r[i].ToString("x2"));
                    }

                    return(sBuilder.ToString());
                }
                else
                {
                    PGPLib pgp    = new PGPLib();
                    byte[] bytes  = Encoding.UTF8.GetBytes(_privateKey);
                    var    stream = new MemoryStream(bytes);
                    var    signed = pgp.SignString(msg, stream, _pgpKeyPassword);

                    // Xử lý chuỗi lấy ra base64 string
                    var    split  = signed.Split(Environment.NewLine, StringSplitOptions.RemoveEmptyEntries);
                    string result = string.Join("", split.Skip(2).Take(split.Length - 3));
                    //
                    return(result);
                }
            }
            else
            {
                return(null);
            }
        }
        public String encrypet(string InputText, string path, string password)
        {
            String plainString = InputText;

            // create an instance of the library
            PGPLib pgp = new PGPLib();

            String signedString = "";

            try
            {
                signedString = pgp.SignString(plainString,
                                              new FileInfo(path),
                                              password);
            }
            catch
            {
                return("false");
            }
            return(signedString);
        }
Example #5
0
        public String encrypet(string InputText, string path)
        {
            int    ID          = Convert.ToInt32(Session["ID"].ToString());
            String plainString = InputText;

            // create an instance of the library
            PGPLib   pgp          = new PGPLib();
            Studnets loginStudent = new Studnets();
            DataRow  loginS       = loginStudent.drSearchStudentEmail(ID);
            string   password     = null;

            if (loginS != null)
            {
                password = loginS["Password"].ToString();
            }
            String signedString = pgp.SignString(plainString,
                                                 new FileInfo(path),
                                                 password);

            return(signedString);
        }