예제 #1
0
        public void BytecodeApi_Cryptography_AsymmetricEncryption()
        {
            byte[] data = MathEx.RandomNumberGenerator.GetBytes(32);
            AsymmetricEncryption.GenerateKeyPair(out RSAParameters publicKey, out RSAParameters privateKey);

            Assert.IsTrue(AsymmetricEncryption.Decrypt(AsymmetricEncryption.Encrypt(data, Resources.RSAPublicKey), Resources.RSAPrivateKey).Compare(data));
            Assert.IsTrue(AsymmetricEncryption.Decrypt(AsymmetricEncryption.Encrypt(data, publicKey), privateKey).Compare(data));
        }
예제 #2
0
        public void Can_Decrypt_Encrypted_Data()
        {
            byte[] rawData = Random.GetNumbers(128);

            byte[] encryptedData = AsymmetricEncryption.Encrypt(rawData, publicKey);
            byte[] decryptedData = AsymmetricEncryption.Decrypt(encryptedData);

            Assert.NotNull(decryptedData);
            Assert.AreEqual(rawData, decryptedData);
        }
예제 #3
0
파일: Program.cs 프로젝트: wynhaat/Aptech
        public static void Main(string[] args)
        {
            string text          = "123456";
            RSA    rsa           = RSA.Create();
            string encryptedText = AsymmetricEncryption.Encrypt(text, rsa);
            string decryptedText = AsymmetricEncryption.Decrypt(encryptedText, rsa);

            Console.WriteLine("haha");
            CreateWebHostBuilder(args).Build().Run();
            //Test secure password
        }
        public void Encrypt()
        {
            var data = "P@ssw0rd";

            var keys      = AsymmetricEncryption.GenerateKeys();
            var encrypted = AsymmetricEncryption.Encrypt(data, keys.PublicKey);
            var decrypted = AsymmetricEncryption.Decrypt(encrypted, keys.KeysPair);

            Console.WriteLine(encrypted);
            Assert.AreEqual(data, decrypted);
        }
예제 #5
0
        public static LicenseData Read(string key)
        {
            var encryptedStr = File.ReadAllText(FilePath);

            byte[] encryptedData = Convert.FromBase64String(encryptedStr);

            var         licenseData = AsymmetricEncryption.Decrypt(encryptedData, 4096, key);
            LicenseData license     = ObjectSerializer.Deserialize(licenseData) as LicenseData;

            return(license);
        }
예제 #6
0
        public IHttpActionResult Token([FromBody] Aparte.Credentials.UserCredential user)
        {
            //Authentication codes here
            var context    = new ApiContext();
            var pw         = AsymmetricEncryption.Decrypt(user.Password, AuthenticationServer.PRIVATE_KEY);
            var systemUser = Aparte.WebApi.BeginApiSession.ValidatePassword(context, user.UserId, pw);

            if (systemUser == null)
            {
                return(null);//this.Unauthorized(null);
            }
            var encryptedToken = ProduceToken(systemUser.PK, KeyFile.JEDIX_WIN_CLIENT_NAME, context);

            return(Ok <string>(encryptedToken));
        }
예제 #7
0
        public IHttpActionResult RefreshToken([FromBody] Aparte.Credentials.UserToken userToken)
        {
            var accessToken  = AsymmetricEncryption.Decrypt(userToken.AccessToken, AuthenticationServer.PRIVATE_KEY);
            var refreshToken = AsymmetricEncryption.Decrypt(userToken.RefreshToken, AuthenticationServer.PRIVATE_KEY);

            var context    = new ApiContext();
            var apiSession = Aparte.WebApi.RefreshApiSession.Execute(context, userToken.PKSystemUser, accessToken, refreshToken);
            var token      = new JWEAsymmetric();

            token.AsymmetricKey = Audiences.Item(KeyFile.JEDIX_WIN_CLIENT_NAME).PublicKey;
            token.SetExpiry(apiSession.TokenExpiry);
            token.AddClaim(JWTConstant.PK_SYSTEM_USER, apiSession.PKSystemUser.ToString());
            token.AddClaim(JWTConstant.ACCESS_TOKEN, apiSession.AccessToken.ToString());
            var encryptedToken = token.SerializeToBase64UrlString();

            UserList.Add(apiSession.PKSystemUser, apiSession.UserName, apiSession.UserCode, apiSession.AccessToken, apiSession.RefreshToken, apiSession.TokenExpiry);
            return(Ok <string>(encryptedToken));
        }
예제 #8
0
        public void DecryptWithoutPrivateKeyTest()
        {
            var encryption = new AsymmetricEncryption(_publicKey, null);

            Assert.Throws(typeof(CryptographicException), () => { encryption.Decrypt(encryption.Encrypt("aw3lrifos83fusoi3fjsofisjfo")); });
        }
예제 #9
0
        public void EncryptionStringEqualsTest()
        {
            var encryption = new AsymmetricEncryption(_publicKey, _privateKey);

            Assert.AreEqual("aw3lrifos83fusoi3fjsofisjfo", encryption.Decrypt(encryption.Encrypt("aw3lrifos83fusoi3fjsofisjfo")));
        }