Esempio n. 1
0
        //Пункт 2
        public CryptoWorker(string toEncrypt)
        {
            RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();

            privateKey = RSA.ExportParameters(true);
            publicKey  = RSA.ExportParameters(false);
            privateKey.ToString();

            UnicodeEncoding byteConverter = new UnicodeEncoding();

            byte[] encBytes = RSAEncrypt(byteConverter.GetBytes(toEncrypt), publicKey, false);

            string encrypt = byteConverter.GetString(encBytes);

            Debug.WriteLine("Encrypt str: " + encrypt);
            Debug.WriteLine("Encrypt bytes: " + string.Join(", ", encBytes));

            byte[] decBytes = RSADecrypt(encBytes, privateKey, false);

            Debug.WriteLine("Decrypt str: " + byteConverter.GetString(decBytes));
            Debug.WriteLine("Decrypt bytes: " + string.Join(", ", byteConverter.GetBytes(encrypt)));
            var s = RSA.ToXmlString(true);
            IEnumerable <KeyValuePair <string, string> > queries = new List <KeyValuePair <string, string> >()
            {
                new KeyValuePair <string, string> ("key", s)
            };

            //ServerWorker.PostRequest(new UriBuilder("http://34.69.5.208/get_run_mi_key/root?code_word=key").Uri, queries);
            ServerWorker.DataBaseWriting(s);
            RSA = new RSACryptoServiceProvider();
            RSA.FromXmlString(s);
        }
Esempio n. 2
0
        private static string GenerateIdToken()
        {
            using RSA provider = RSA.Create(2048);

            RSAParameters parameters = provider.ExportParameters(true);

            RsaSecurityKey secKey = new RsaSecurityKey(parameters);

            SigningCredentials credentials = new SigningCredentials(secKey, "RS256");

            credentials.Key.KeyId = parameters.ToString();

            var header = new JwtHeader(credentials)
            {
                { "jku", "https://e0d67c509fb203858ebcb2fe3f88c2aa.baas.nintendo.com/1.0.0/certificates" }
            };

            byte[] rawUserId = new byte[0x10];
            RandomNumberGenerator.Fill(rawUserId);

            byte[] deviceId = new byte[0x10];
            RandomNumberGenerator.Fill(deviceId);

            byte[] deviceAccountId = new byte[0x10];
            RandomNumberGenerator.Fill(deviceId);

            var payload = new JwtPayload
            {
                { "sub", BitConverter.ToString(rawUserId).Replace("-", "").ToLower() },
                { "aud", "ed9e2f05d286f7b8" },
                { "di", BitConverter.ToString(deviceId).Replace("-", "").ToLower() },
                { "sn", "XAW10000000000" },
                { "bs:did", BitConverter.ToString(deviceAccountId).Replace("-", "").ToLower() },
                { "iss", "https://e0d67c509fb203858ebcb2fe3f88c2aa.baas.nintendo.com" },
                { "typ", "id_token" },
                { "iat", DateTimeOffset.UtcNow.ToUnixTimeSeconds() },
                { "jti", Guid.NewGuid().ToString() },
                { "exp", (DateTimeOffset.UtcNow + TimeSpan.FromHours(3)).ToUnixTimeSeconds() }
            };

            JwtSecurityToken securityToken = new JwtSecurityToken(header, payload);

            return(new JwtSecurityTokenHandler().WriteToken(securityToken));
        }
Esempio n. 3
0
        public String createToken(String userId, String muaji, String emriMbiemri, Decimal vleraNeEuro, String llojiFatures)
        {
            var payload = new Dictionary <string, object>
            {
                { "userId", userId },
                { "muaji", muaji },
                { "emriMbiemri", emriMbiemri },
                { "vleraNeEuro", vleraNeEuro },
                { "llojiFatures", llojiFatures },
            };
            const string secret = "GQDstcKsx0NHjPOuXOYg5MbeJ1XT0uFiwDVvVBrk";

            IJwtAlgorithm     algorithm  = new HMACSHA256Algorithm();
            IJsonSerializer   serializer = new JsonNetSerializer();
            IBase64UrlEncoder urlEncoder = new JwtBase64UrlEncoder();
            IJwtEncoder       encoder    = new JwtEncoder(algorithm, serializer, urlEncoder);
            var token = encoder.Encode(payload, privateKey.ToString());

            return(token);
        }
Esempio n. 4
0
        public String createToken(String userId, String email, String emriMbiemri, Decimal paga, String titulli)
        {
            var payload = new Dictionary <string, object>
            {
                { "userId", userId },
                { "email", email },
                { "emriMbiemri", emriMbiemri },
                { "paga", paga },
                { "titulli", titulli },
            };
            const string secret = "GQDstcKsx0NHjPOuXOYg5MbeJ1XT0uFiwDVvVBrk";

            IJwtAlgorithm     algorithm  = new HMACSHA256Algorithm();
            IJsonSerializer   serializer = new JsonNetSerializer();
            IBase64UrlEncoder urlEncoder = new JwtBase64UrlEncoder();
            IJwtEncoder       encoder    = new JwtEncoder(algorithm, serializer, urlEncoder);
            var token = encoder.Encode(payload, privateKey.ToString());

            return(token);
        }
Esempio n. 5
0
        public String createToken(String userId, int Viti, String Fatura, Decimal Vlera, String Muaji, String Email)
        {
            var payload = new Dictionary <string, object>
            {
                { "userId", userId },
                { "Viti", Viti },
                { "Fatura", Fatura },
                { "Vlera", Vlera },
                { "Muaji", Muaji },
                { "Email", Email }
            };


            IJwtAlgorithm     algorithm  = new HMACSHA256Algorithm();
            IJsonSerializer   serializer = new JsonNetSerializer();
            IBase64UrlEncoder urlEncoder = new JwtBase64UrlEncoder();
            IJwtEncoder       encoder    = new JwtEncoder(algorithm, serializer, urlEncoder);
            var token = encoder.Encode(payload, privateKey.ToString());

            return(token);
        }
Esempio n. 6
0
        /// <summary>
        /// Initializes a new instance of the <see cref="RsaSecurityKey"/> class.
        /// </summary>
        /// <param name="rsaParameters"><see cref="RSAParameters"/></param>
        public RsaSecurityKey(RSAParameters rsaParameters)
        {
            // must have modulus and exponent otherwise the crypto operations fail later
            if (rsaParameters.Modulus == null || rsaParameters.Exponent == null)
            {
                throw LogHelper.LogExceptionMessage(new ArgumentException(LogHelper.FormatInvariant(LogMessages.IDX10700, rsaParameters.ToString())));
            }

            _hasPrivateKey             = rsaParameters.D != null && rsaParameters.DP != null && rsaParameters.DQ != null && rsaParameters.P != null && rsaParameters.Q != null && rsaParameters.InverseQ != null;
            _foundPrivateKey           = _hasPrivateKey.Value ? PrivateKeyStatus.Exists : PrivateKeyStatus.DoesNotExist;
            _foundPrivateKeyDetermined = true;
            Parameters = rsaParameters;
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="RsaSecurityKey"/> class.
        /// </summary>
        /// <param name="rsaParameters"><see cref="RSAParameters"/></param>
        public RsaSecurityKey(RSAParameters rsaParameters)
        {
            // must have modulus and exponent otherwise the crypto operations fail later
            if (rsaParameters.Modulus == null || rsaParameters.Exponent == null)
            {
                throw LogHelper.LogException <ArgumentException>(LogMessages.IDX10700, rsaParameters.ToString());
            }

            _hasPrivateKey = rsaParameters.D != null && rsaParameters.DP != null && rsaParameters.DQ != null && rsaParameters.P != null && rsaParameters.Q != null && rsaParameters.InverseQ != null;
            Parameters     = rsaParameters;
        }
Esempio n. 8
0
    static void Main()
    {
        //	long p, q, e;
        //	Console.WriteLine("Podaj wartość p");
        //	p = long.Parse(Console.ReadLine());
        //	Console.WriteLine("Podaj wartość q");
        //	q = long.Parse(Console.ReadLine());
        //	Console.WriteLine("Podaj wartość e");
        //	e = long.Parse(Console.ReadLine());

        //	long n = p * q;
        //	long d = ObliczD(e, p, q);
        //	long dp = d % (p - 1);
        //	long dq = d % (q - 1);
        //	long inverseQ = (long)Math.Pow(q,(p-2)) % p;
        //	Console.WriteLine($"inverseQ = {inverseQ}");
        //	Console.WriteLine($"d = {d}");
        //	RSAParameters user_param = new RSAParameters();
        //	user_param.D = BitConverter.GetBytes(d);
        //	user_param.DP = BitConverter.GetBytes(dp);
        //	user_param.DQ = BitConverter.GetBytes(dq);
        //	user_param.Exponent = BitConverter.GetBytes(e);
        //	user_param.P = BitConverter.GetBytes(p);
        //	user_param.Q = BitConverter.GetBytes(q);
        //	user_param.Modulus = BitConverter.GetBytes(n);
        //	user_param.InverseQ = BitConverter.GetBytes(inverseQ);

        RSACryptoServiceProvider RSA1 = new RSACryptoServiceProvider();

        Console.WriteLine("Podaj treść wiadomosci do zaszyfrowania");
        string message = Console.ReadLine();

        Console.WriteLine();
        char[] dataASCII = message.ToCharArray(0, message.Length);
        int    i         = 0;

        byte[] data = new byte[message.Length];

        foreach (var item in dataASCII)
        {
            data[i] = Convert.ToByte((int)item);
            i++;
        }

        byte[] encryptedASCII = RSA1.Encrypt(data, true);
        i = 0;
        char[] encrypted = new char[encryptedASCII.Length];
        foreach (var item in encryptedASCII)
        {
            encrypted[i] = (char)item;
            i++;
        }
        string enMessage = string.Join("", encrypted);

        Console.WriteLine("Zaszyfrowana wiadomość: ");
        Console.WriteLine(enMessage);
        Console.WriteLine();         // szyfrowanie zakończone

        RSAParameters parameters = new RSAParameters();

        parameters = RSA1.ExportParameters(true);

        Console.WriteLine($"e = {BitConverter.ToString(parameters.Exponent, 0)}");
        Console.WriteLine($"p = {BitConverter.ToString(parameters.P, 0)}");
        Console.WriteLine($"q = {BitConverter.ToString(parameters.Q, 0)}");
        Console.WriteLine();

        Console.WriteLine("** tu sie dzieje **");
        Console.WriteLine(parameters.ToString());

        string s = "Tekst do zapisu w pliku!";

        File.WriteAllText(@"E:\Szyfrowanie.txt", s);


        RSACryptoServiceProvider RSA2 = new RSACryptoServiceProvider();

        RSA2.ImportParameters(parameters);         // import parametrów szyfrowania klucza prywatnego jak i publicznego



        byte[] descryptedASCII = RSA2.Decrypt(encryptedASCII, true);
        i = 0;
        char[] descrypted = new char[descryptedASCII.Length];
        foreach (var item in descryptedASCII)
        {
            descrypted[i] = (char)item;
            i++;
        }
        string desMessage = string.Join("", descrypted);

        Console.WriteLine("Odzyskana wiadomość: ");
        Console.WriteLine(desMessage);
        Console.WriteLine("*");

        Console.ReadKey();
    }