Ejemplo n.º 1
0
        /// <summary>
        /// Export RSA public key
        /// </summary>
        /// <param name="rsa"></param>
        /// <param name="format"></param>
        /// <param name="usePemFormat"></param>
        /// <returns></returns>
        public static string ExportPublicKey(this MsRSA rsa, RsaKeyFormat format, bool usePemFormat = false)
        {
            var key = format switch
            {
                RsaKeyFormat.XML => rsa.ExportKeyInLvccXml(false),
                RsaKeyFormat.JSON => rsa.ExportKeyInJson(false),
#if NETCOREAPP3_1 || NETSTANDARD2_1
                RsaKeyFormat.Pkcs1 => BaseConv.ToBase64(rsa.ExportRSAPublicKey()),
                RsaKeyFormat.Pkcs8 => BaseConv.ToBase64(rsa.ExportRSAPublicKey()),
#else
                RsaKeyFormat.Pkcs1 => rsa.GetPublicKeyInPkcs1(),
                RsaKeyFormat.Pkcs8 => rsa.GetPublicKeyInPkcs8(),
#endif
                _ => throw new NotSupportedException("Unknown RSA key type.")
            };

            if (usePemFormat)
            {
                key = format switch
                {
                    RsaKeyFormat.XML => key,
                    RsaKeyFormat.JSON => key,
                    RsaKeyFormat.Pkcs1 => key.RemovePkcs1PublicKeyFormat(),
                    RsaKeyFormat.Pkcs8 => key.RemovePkcs8PublicKeyFormat(),
                    _ => throw new NotSupportedException("Unknown RSA key type.")
                };
            }

            return(key);
        }
Ejemplo n.º 2
0
        public void Encrypt()
        {
            var key        = RcFactory.GenerateKey("alexinea", Encoding.UTF8);
            var function   = RcFactory.Create(RcTypes.RC4, key);
            var cryptoVal0 = function.Encrypt("image");

            BaseConv.ToBase64(cryptoVal0.CipherData).ShouldBe("I2YRaZo=");
        }
Ejemplo n.º 3
0
        public void Encrypt_ThreeRCX()
        {
            var key        = RcFactory.GenerateKey("alexinea", Encoding.UTF8);
            var function   = RcFactory.Create(RcTypes.ThreeRCX, key);
            var cryptoVal0 = function.Encrypt("ABCDDDDDDDDDDDDDDDDDDDDDD");

            BaseConv.ToBase64(cryptoVal0.CipherData).ShouldBe("JPTCrl2N6xae4GCEXfzUiSa9YrwSa80HDg==");
        }
Ejemplo n.º 4
0
        public void Encrypt()
        {
            var key        = RcFactory.GenerateKey("alexinea", Encoding.UTF8);
            var function   = RcFactory.Create(RcTypes.RCX, key);
            var cryptoVal0 = function.Encrypt("ABCDDDDDDDDDDDDDDDDDDDDDD");

            BaseConv.ToBase64(cryptoVal0.CipherData).ShouldBe("C+YxcfWRWMVCIjbX21qXcG9OXq25jJTHmw==");
        }
Ejemplo n.º 5
0
 public string GetBase64String()
 {
     if (!IncludeData())
     {
         return(string.Empty);
     }
     return(BaseConv.ToBase64(_data));
 }
Ejemplo n.º 6
0
        public void Base64BytesTest()
        {
            var byteArray         = Encoding.UTF8.GetBytes("Alex LEWIS");
            var baseVal           = BaseConv.ToBase64(byteArray);
            var originalByteArray = BaseConv.FromBase64(baseVal);
            var originalVal       = Encoding.UTF8.GetString(originalByteArray);

            Assert.Equal("Alex LEWIS", originalVal);
        }
Ejemplo n.º 7
0
        public void EncryptDecrypt_WithSalt_WithAutoCreateKey_L128_Test()
        {
            var key        = DesFactory.GenerateKey(DesTypes.TripleDES128);
            var function   = DesFactory.Create(DesTypes.TripleDES128, key);
            var cryptoVal0 = function.Encrypt("实现中华民族伟大复兴的中国梦", "123412341234");
            var cryptoVal1 = function.Decrypt(cryptoVal0.CipherData, "123412341234");

            cryptoVal1.GetOriginalDataDescriptor().GetString().ShouldBe("实现中华民族伟大复兴的中国梦");

            var cryptoVal2 = function.Decrypt(BaseConv.ToBase64(cryptoVal0.CipherData), "123412341234", CipherTextTypes.Base64Text);

            cryptoVal2.GetOriginalDataDescriptor().GetString().ShouldBe("实现中华民族伟大复兴的中国梦");
        }
Ejemplo n.º 8
0
            public byte[] Hash(ReadOnlySpan <byte> buff)
            {
                using var algorithm = MD5.Create();
                var hashVal = algorithm.ComputeHash(buff.ToArray());

                return(_type switch
                {
                    MdTypes.Md5 => hashVal,
                    MdTypes.Md5Bit16 => hashVal.AsSpan(4, 8).ToArray(),
                    MdTypes.Md5Bit32 => hashVal,
                    MdTypes.Md5Bit64 => Encoding.UTF8.GetBytes(BaseConv.ToBase64(hashVal)),
                    _ => hashVal
                });
Ejemplo n.º 9
0
        public void Encrypt_L256_WithSalt_Test()
        {
            var key        = AesFactory.GenerateKey(AesTypes.Aes256);
            var function   = AesFactory.Create(AesTypes.Aes256, key);
            var cryptoVal0 = function.Encrypt("实现中华民族伟大复兴的中国梦", "12345678");
            var cryptoVal1 = function.Decrypt(cryptoVal0.CipherData, "12345678");

            cryptoVal1.GetOriginalDataDescriptor().GetString().ShouldBe("实现中华民族伟大复兴的中国梦");

            var cryptoVal2 = function.Decrypt(BaseConv.ToBase64(cryptoVal0.CipherData), "12345678", CipherTextTypes.Base64Text);

            cryptoVal2.GetOriginalDataDescriptor().GetString().ShouldBe("实现中华民族伟大复兴的中国梦");
        }
Ejemplo n.º 10
0
        public void EncryptDecrypt_WithAutoCreateKey_Test()
        {
            var key        = DesFactory.GenerateKey();
            var function   = DesFactory.Create(DesTypes.DES, key);
            var cryptoVal0 = function.Encrypt("image");

            var cryptoVal1 = function.Decrypt(cryptoVal0.CipherData);

            cryptoVal1.GetOriginalDataDescriptor().GetString().ShouldBe("image");

            var cryptoVal2 = function.Decrypt(BaseConv.ToBase64(cryptoVal0.CipherData), CipherTextTypes.Base64Text);

            cryptoVal2.GetOriginalDataDescriptor().GetString().ShouldBe("image");
        }
Ejemplo n.º 11
0
        public void TEATest()
        {
            var key        = TeaFactory.GenerateKey("alexinea", Encoding.UTF8);
            var function   = TeaFactory.Create(TeaTypes.TEA, key);
            var cryptoVal0 = function.Encrypt("AlexLEWIS      ");

            var cryptoVal1 = function.Decrypt(cryptoVal0.CipherData);

            cryptoVal1.GetOriginalDataDescriptor().GetString().ShouldBe("AlexLEWIS      ");

            var cryptoVal2 = function.Decrypt(BaseConv.ToBase64(cryptoVal0.CipherData), CipherTextTypes.Base64Text);

            cryptoVal2.GetOriginalDataDescriptor().GetString().ShouldBe("AlexLEWIS      ");
        }
Ejemplo n.º 12
0
        public void EncryptDecrypt_WithSalt_Test()
        {
            var key        = DesFactory.GenerateKey(DesTypes.DES, "alexinea", "forerunner", Encoding.UTF8);
            var function   = DesFactory.Create(DesTypes.DES, key);
            var cryptoVal0 = function.Encrypt("image", "123412341234");

            BaseConv.ToBase64(cryptoVal0.CipherData).ShouldBe("s4h5u8hA/2Y=");

            var cryptoVal1 = function.Decrypt(cryptoVal0.CipherData, "123412341234");

            cryptoVal1.GetOriginalDataDescriptor().GetString().ShouldBe("image");

            var cryptoVal2 = function.Decrypt(BaseConv.ToBase64(cryptoVal0.CipherData), "123412341234", CipherTextTypes.Base64Text);

            cryptoVal2.GetOriginalDataDescriptor().GetString().ShouldBe("image");
        }
Ejemplo n.º 13
0
        public void EncryptDecrypt_L192_Test()
        {
            var key        = DesFactory.GenerateKey(DesTypes.TripleDES192, "alexinea&#%12!", "forerunner", Encoding.UTF8);
            var function   = DesFactory.Create(DesTypes.TripleDES192, key);
            var cryptoVal0 = function.Encrypt("image");

            BaseConv.ToBase64(cryptoVal0.CipherData).ShouldBe("Y6tAf/GrLx8=");

            var cryptoVal1 = function.Decrypt(cryptoVal0.CipherData);

            cryptoVal1.GetOriginalDataDescriptor().GetString().ShouldBe("image");

            var cryptoVal2 = function.Decrypt(BaseConv.ToBase64(cryptoVal0.CipherData), CipherTextTypes.Base64Text);

            cryptoVal2.GetOriginalDataDescriptor().GetString().ShouldBe("image");
        }
Ejemplo n.º 14
0
 internal Sm2Key(ECPoint publicKey, BigInteger privateKey)
 {
     PublicKey  = BaseConv.ToBase64(publicKey.GetEncoded());   //Hex.Encode(publicKey.GetEncoded()).GetString(encoding.SafeEncodingValue()).ToUpper();
     PrivateKey = BaseConv.ToBase64(privateKey.ToByteArray()); //Hex.Encode(privateKey.ToByteArray()).GetString(encoding.SafeEncodingValue()).ToUpper();
 }