コード例 #1
0
 public static string Decrypt(string privateKeyXml, string encryptedData, RsaKeyLengths rsaKeyLength = RsaKeyLengths.Bit2048)
 {
     var encryptedBytes = Convert.FromBase64String(encryptedData);
     var bytes = Decrypt(privateKeyXml, encryptedBytes, rsaKeyLength);
     var data = Encoding.UTF8.GetString(bytes);
     return data;
 }
コード例 #2
0
 public static string Encrypt(string publicKeyXml, string data, RsaKeyLengths rsaKeyLength = RsaKeyLengths.Bit2048)
 {
     var bytes = Encoding.UTF8.GetBytes(data);
     var encryptedBytes = Encrypt(publicKeyXml, bytes, rsaKeyLength);
     string encryptedData = Convert.ToBase64String(encryptedBytes);
     return encryptedData;
 }
コード例 #3
0
ファイル: CryptUtils.cs プロジェクト: jmaucher/SStack
        public static string Decrypt(string privateKey, string data, RsaKeyLengths length = RsaKeyLengths.Bit2048)
        {
            var dwKeySize = (int)length;
            // TODO: Add Proper Exception Handlers
            var rsaCryptoServiceProvider = new RSACryptoServiceProvider(dwKeySize);

            rsaCryptoServiceProvider.FromXmlString(privateKey);

            int base64BlockSize = ((dwKeySize / 8) % 3 != 0) ?
                                  (((dwKeySize / 8) / 3) * 4) + 4 : ((dwKeySize / 8) / 3) * 4;

            int iterations = data.Length / base64BlockSize;

            var arrayList = new ArrayList();

            for (int i = 0; i < iterations; i++)
            {
                byte[] encryptedBytes = Convert.FromBase64String(
                    data.Substring(base64BlockSize * i, base64BlockSize));
                // Be aware the RSACryptoServiceProvider reverses the order of
                // encrypted bytes after encryption and before decryption.
                // If you do not require compatibility with Microsoft Cryptographic
                // API (CAPI) and/or other vendors.
                // Comment out the next line and the corresponding one in the
                // EncryptString function.
                Array.Reverse(encryptedBytes);
                arrayList.AddRange(rsaCryptoServiceProvider.Decrypt(encryptedBytes, true));
            }

            return(Encoding.Unicode.GetString(arrayList.ToArray(typeof(byte)) as byte[]));
        }
コード例 #4
0
ファイル: CryptUtils.cs プロジェクト: wangpei421/ServiceStack
 public static byte[] Decrypt(byte[] encryptedBytes, string privateKeyXml, RsaKeyLengths rsaKeyLength = RsaKeyLengths.Bit2048)
 {
     using var rsa = CreateRsa(rsaKeyLength);
     rsa.FromXml(privateKeyXml);
     byte[] bytes = rsa.Decrypt(encryptedBytes);
     return(bytes);
 }
コード例 #5
0
ファイル: CryptUtils.cs プロジェクト: humanbeinc/ServiceStack
 public static RSAParameters CreatePrivateKeyParams(RsaKeyLengths rsaKeyLength = RsaKeyLengths.Bit2048)
 {
     using (var rsa = new RSACryptoServiceProvider((int)rsaKeyLength))
     {
         return(rsa.ExportParameters(includePrivateParameters: true));
     }
 }
コード例 #6
0
        private static RSA CreateRsa(RsaKeyLengths rsaKeyLength)
        {
            var rsa = RSA.Create();

            rsa.KeySize = (int)rsaKeyLength;
            return(rsa);
        }
コード例 #7
0
 public static RSAParameters CreatePrivateKeyParams(RsaKeyLengths rsaKeyLength = RsaKeyLengths.Bit2048)
 {
     using (var rsa = CreateRsa(rsaKeyLength))
     {
         return rsa.ExportParameters(includePrivateParameters: true);
     }
 }
コード例 #8
0
 public static RSAParameters CreatePrivateKeyParams(RsaKeyLengths rsaKeyLength = RsaKeyLengths.Bit2048)
 {
     using (var rsa = CreateRsa(rsaKeyLength))
     {
         return(rsa.ExportParameters(includePrivateParameters: true));
     }
 }
コード例 #9
0
        public static string Decrypt(string privateKeyXml, string encryptedData, RsaKeyLengths rsaKeyLength = RsaKeyLengths.Bit2048)
        {
            var encryptedBytes = Convert.FromBase64String(encryptedData);
            var bytes          = Decrypt(privateKeyXml, encryptedBytes, rsaKeyLength);
            var data           = Encoding.UTF8.GetString(bytes);

            return(data);
        }
コード例 #10
0
        public static string Encrypt(string publicKeyXml, string data, RsaKeyLengths rsaKeyLength = RsaKeyLengths.Bit2048)
        {
            var    bytes          = Encoding.UTF8.GetBytes(data);
            var    encryptedBytes = Encrypt(publicKeyXml, bytes, rsaKeyLength);
            string encryptedData  = Convert.ToBase64String(encryptedBytes);

            return(encryptedData);
        }
コード例 #11
0
        public static string Encrypt(string text, RSAParameters publicKey, RsaKeyLengths rsaKeyLength = RsaKeyLengths.Bit2048)
        {
            var    bytes          = Encoding.UTF8.GetBytes(text);
            var    encryptedBytes = Encrypt(bytes, publicKey, rsaKeyLength);
            string encryptedData  = Convert.ToBase64String(encryptedBytes);

            return(encryptedData);
        }
コード例 #12
0
 public static byte[] Encrypt(byte[] bytes, RSAParameters publicKey, RsaKeyLengths rsaKeyLength = RsaKeyLengths.Bit2048)
 {
     using (var rsa = CreateRsa(rsaKeyLength))
     {
         rsa.ImportParameters(publicKey);
         return(rsa.Encrypt(bytes));
     }
 }
コード例 #13
0
 public static byte[] Encrypt(byte[] bytes, string publicKeyXml, RsaKeyLengths rsaKeyLength = RsaKeyLengths.Bit2048)
 {
     using (var rsa = CreateRsa(rsaKeyLength))
     {
         rsa.FromXmlString(publicKeyXml);
         return(rsa.Encrypt(bytes));
     }
 }
コード例 #14
0
        public static string Decrypt(string encryptedText, RSAParameters privateKey, RsaKeyLengths rsaKeyLength = RsaKeyLengths.Bit2048)
        {
            var encryptedBytes = Convert.FromBase64String(encryptedText);
            var bytes          = Decrypt(encryptedBytes, privateKey, rsaKeyLength);
            var data           = Encoding.UTF8.GetString(bytes);

            return(data);
        }
コード例 #15
0
ファイル: CryptUtils.cs プロジェクト: humanbeinc/ServiceStack
 public static byte[] Encrypt(byte[] bytes, string publicKeyXml, RsaKeyLengths rsaKeyLength = RsaKeyLengths.Bit2048)
 {
     using (var rsa = new RSACryptoServiceProvider((int)rsaKeyLength))
     {
         rsa.FromXmlString(publicKeyXml);
         return(rsa.Encrypt(bytes, DoOAEPPadding));
     }
 }
コード例 #16
0
ファイル: CryptUtils.cs プロジェクト: humanbeinc/ServiceStack
 public static byte[] Encrypt(byte[] bytes, RSAParameters publicKey, RsaKeyLengths rsaKeyLength = RsaKeyLengths.Bit2048)
 {
     using (var rsa = new RSACryptoServiceProvider((int)rsaKeyLength))
     {
         rsa.ImportParameters(publicKey);
         return(rsa.Encrypt(bytes, DoOAEPPadding));
     }
 }
コード例 #17
0
ファイル: CryptUtils.cs プロジェクト: wangpei421/ServiceStack
 public static RsaKeyPair CreatePublicAndPrivateKeyPair(RsaKeyLengths rsaKeyLength = RsaKeyLengths.Bit2048)
 {
     using var rsa = CreateRsa(rsaKeyLength);
     return(new RsaKeyPair {
         PrivateKey = rsa.ToXml(includePrivateParameters: true),
         PublicKey = rsa.ToXml(includePrivateParameters: false),
     });
 }
コード例 #18
0
 public static RsaKeyPair CreatePublicAndPrivateKeyPair(RsaKeyLengths rsaKeyLength = RsaKeyLengths.Bit2048)
 {
     var rsaProvider = new RSACryptoServiceProvider((int)rsaKeyLength);
     return new RsaKeyPair
     {
         PrivateKey = rsaProvider.ToXmlString(true),
         PublicKey = rsaProvider.ToXmlString(false),
     };
 }
コード例 #19
0
 private static byte[] Decrypt(string privateKeyXml, byte[] encryptedBytes, RsaKeyLengths rsaKeyLength)
 {
     using (var RSA = new RSACryptoServiceProvider((int) rsaKeyLength))
     {
         RSA.FromXmlString(privateKeyXml);
         byte[] bytes = RSA.Decrypt(encryptedBytes, DoOAEPPadding);
         return bytes;
     }
 }
コード例 #20
0
ファイル: CryptUtils.cs プロジェクト: zuiwanting/ServiceStack
        public static RsaKeyPair CreatePublicAndPrivateKeyPair(RsaKeyLengths length = RsaKeyLengths.Bit2048)
        {
            var rsaProvider = new RSACryptoServiceProvider((int)length);

            return(new RsaKeyPair {
                PrivateKey = rsaProvider.ToXmlString(true),
                PublicKey = rsaProvider.ToXmlString(false),
            });
        }
コード例 #21
0
 private static byte[] Decrypt(string privateKeyXml, byte[] encryptedBytes, RsaKeyLengths rsaKeyLength)
 {
     using (var RSA = new RSACryptoServiceProvider((int)rsaKeyLength))
     {
         RSA.FromXmlString(privateKeyXml);
         byte[] bytes = RSA.Decrypt(encryptedBytes, DoOAEPPadding);
         return(bytes);
     }
 }
コード例 #22
0
ファイル: CryptUtils.cs プロジェクト: humanbeinc/ServiceStack
 public static byte[] Decrypt(byte[] encryptedBytes, string privateKeyXml, RsaKeyLengths rsaKeyLength = RsaKeyLengths.Bit2048)
 {
     using (var rsa = new RSACryptoServiceProvider((int)rsaKeyLength))
     {
         rsa.FromXmlString(privateKeyXml);
         byte[] bytes = rsa.Decrypt(encryptedBytes, DoOAEPPadding);
         return(bytes);
     }
 }
コード例 #23
0
 private static byte[] Encrypt(string publicKeyXml, byte[] bytes, RsaKeyLengths rsaKeyLength = RsaKeyLengths.Bit2048)
 {
     byte[] encryptedBytes;
     using (var RSA = new RSACryptoServiceProvider((int) rsaKeyLength))
     {
         RSA.FromXmlString(publicKeyXml);
         encryptedBytes = RSA.Encrypt(bytes, DoOAEPPadding);
     }
     return encryptedBytes;
 }
コード例 #24
0
 private static byte[] Encrypt(string publicKeyXml, byte[] bytes, RsaKeyLengths rsaKeyLength = RsaKeyLengths.Bit2048)
 {
     byte[] encryptedBytes;
     using (var RSA = new RSACryptoServiceProvider((int)rsaKeyLength))
     {
         RSA.FromXmlString(publicKeyXml);
         encryptedBytes = RSA.Encrypt(bytes, DoOAEPPadding);
     }
     return(encryptedBytes);
 }
コード例 #25
0
 public static RsaKeyPair CreatePublicAndPrivateKeyPair(RsaKeyLengths rsaKeyLength = RsaKeyLengths.Bit2048)
 {
     using (var rsa = CreateRsa(rsaKeyLength))
     {
         return new RsaKeyPair
         {
             PrivateKey = rsa.ToXmlString(includePrivateParameters: true),
             PublicKey = rsa.ToXmlString(includePrivateParameters: false),
         };
     }
 }
コード例 #26
0
ファイル: CryptUtils.cs プロジェクト: jmaucher/SStack
        /// <summary>
        /// Encrypt an arbitrary string of data under the supplied public key
        /// </summary>
        /// <param name="publicKey">The public key to encrypt under</param>
        /// <param name="data">The data to encrypt</param>
        /// <param name="length">The bit length or strength of the public key: 1024, 2048 or 4096 bits. This must match the 
        /// value actually used to create the publicKey</param>
        /// <returns></returns>
        public static string Encrypt(string publicKey, string data, RsaKeyLengths length = RsaKeyLengths.Bit2048)
        {
            // full array of bytes to encrypt
            byte[] bytesToEncrypt;

            // worker byte array
            byte[] block;

            // encrypted bytes
            byte[] encryptedBytes;

            // length of bytesToEncrypt
            var dataLength = 0;

            // number of bytes in key                
            var keySize = 0;

            // maximum block length to encrypt          
            var maxLength = 0;

            // how many blocks must we encrypt to encrypt entire message?
            var iterations = 0;

            // the encrypted data
            var encryptedData = new StringBuilder();

            // instantiate the crypto provider with the correct key length
            var rsaCryptoServiceProvider = new RSACryptoServiceProvider((int)length);

            // initialize the RSA object from the given public key
            rsaCryptoServiceProvider.FromXmlString(publicKey);

            // convert data to byte array
            bytesToEncrypt = Encoding.Unicode.GetBytes(data);

            // get length of byte array
            dataLength = bytesToEncrypt.Length;

            // convert length of key from bits to bytes
            keySize = (int)length / 8;

            // .NET RSACryptoServiceProvider uses SHA1 Hash function
            // use this to work out the maximum length to encrypt per block
            maxLength = ((keySize - 2) - (2 * SHA1.Create().ComputeHash(bytesToEncrypt).Length));

            // how many blocks do we need to encrypt?
            iterations = dataLength / maxLength;

            // encrypt block by block
            for (int index = 0; index <= iterations; index++)
            {
                // is there more than one full block of data left to encrypt?
                if ((dataLength - maxLength * index) > maxLength)
                {
                    block = new byte[maxLength];
                }
                else
                {
                    block = new byte[dataLength - maxLength * index];
                }

                // copy the required number of bytes from the array of bytes to encrypt to our worker array
                Buffer.BlockCopy(bytesToEncrypt, maxLength * index, block, 0, block.Length);

                // encrypt the current worker array block of bytes
                encryptedBytes = rsaCryptoServiceProvider.Encrypt(block, true);

                // RSACryptoServiceProvider reverses the order of encrypted bytesToEncrypt after encryption and before decryption.
                // Undo this reversal for compatibility with other implementations
                Array.Reverse(encryptedBytes);

                // convert to base 64 string
                encryptedData.Append(Convert.ToBase64String(encryptedBytes));
            }

            return encryptedData.ToString();
        }
コード例 #27
0
ファイル: CryptUtils.cs プロジェクト: jmaucher/SStack
        public static string Decrypt(string privateKey, string data, RsaKeyLengths length = RsaKeyLengths.Bit2048)
        {
            var dwKeySize = (int)length;
            // TODO: Add Proper Exception Handlers
            var rsaCryptoServiceProvider = new RSACryptoServiceProvider(dwKeySize);
            rsaCryptoServiceProvider.FromXmlString(privateKey);

            int base64BlockSize = ((dwKeySize / 8) % 3 != 0) ?
              (((dwKeySize / 8) / 3) * 4) + 4 : ((dwKeySize / 8) / 3) * 4;

            int iterations = data.Length / base64BlockSize;

            var arrayList = new ArrayList();
            for (int i = 0; i < iterations; i++)
            {
                byte[] encryptedBytes = Convert.FromBase64String(
                     data.Substring(base64BlockSize * i, base64BlockSize));
                // Be aware the RSACryptoServiceProvider reverses the order of 
                // encrypted bytes after encryption and before decryption.
                // If you do not require compatibility with Microsoft Cryptographic 
                // API (CAPI) and/or other vendors.
                // Comment out the next line and the corresponding one in the 
                // EncryptString function.
                Array.Reverse(encryptedBytes);
                arrayList.AddRange(rsaCryptoServiceProvider.Decrypt(encryptedBytes, true));
            }

            return Encoding.Unicode.GetString(arrayList.ToArray(typeof(byte)) as byte[]);
        }
コード例 #28
0
ファイル: CryptUtils.cs プロジェクト: pkmnfrk/ServiceStack
 public static string Decrypt(string privateKeyXml, string encryptedData, RsaKeyLengths rsaKeyLength = RsaKeyLengths.Bit2048)
 {
     return RsaUtils.Encrypt(encryptedData, privateKeyXml, rsaKeyLength);
 }
コード例 #29
0
ファイル: CryptUtils.cs プロジェクト: pkmnfrk/ServiceStack
 public static RsaKeyPair CreatePublicAndPrivateKeyPair(RsaKeyLengths rsaKeyLength = RsaKeyLengths.Bit2048)
 {
     return RsaUtils.CreatePublicAndPrivateKeyPair(rsaKeyLength);
 }
コード例 #30
0
ファイル: CryptUtils.cs プロジェクト: pkmnfrk/ServiceStack
 public static RSAParameters CreatePrivateKeyParams(RsaKeyLengths rsaKeyLength = RsaKeyLengths.Bit2048)
 {
     using (var rsa = new RSACryptoServiceProvider((int)rsaKeyLength))
     {
         return rsa.ExportParameters(includePrivateParameters: true);
     }
 }
コード例 #31
0
ファイル: CryptUtils.cs プロジェクト: pkmnfrk/ServiceStack
 public static string Encrypt(string publicKeyXml, string data, RsaKeyLengths rsaKeyLength = RsaKeyLengths.Bit2048)
 {
     return RsaUtils.Encrypt(data, publicKeyXml, rsaKeyLength);
 }
コード例 #32
0
 public static byte[] Decrypt(byte[] encryptedBytes, string privateKeyXml, RsaKeyLengths rsaKeyLength = RsaKeyLengths.Bit2048)
 {
     using (var rsa = CreateRsa(rsaKeyLength))
     {
         rsa.FromXmlString(privateKeyXml);
         byte[] bytes = rsa.Decrypt(encryptedBytes);
         return bytes;
     }
 }
コード例 #33
0
 public static bool Verify(byte[] dataToVerify, byte[] signature, RSAParameters publicKey, string hashAlgorithm = "SHA512", RsaKeyLengths rsaKeyLength = RsaKeyLengths.Bit2048)
 {
     using (var rsa = CreateRsa(rsaKeyLength))
     {
         rsa.ImportParameters(publicKey);
         var verified = rsa.VerifyData(dataToVerify, signature, hashAlgorithm);
         return verified;
     }
 }
コード例 #34
0
ファイル: CryptUtils.cs プロジェクト: lydx9876/ServiceStack
 public static bool Verify(byte[] dataToVerify, byte[] signature, RSAParameters publicKey, string hashAlgorithm = "SHA512", RsaKeyLengths rsaKeyLength = RsaKeyLengths.Bit2048)
 {
     using (var rsa = new RSACryptoServiceProvider((int)rsaKeyLength))
     {
         rsa.ImportParameters(publicKey);
         var verified = rsa.VerifyData(dataToVerify, hashAlgorithm, signature);
         return(verified);
     }
 }
コード例 #35
0
        public static byte[] Authenticate(byte[] dataToSign, RSAParameters privateKey, string hashAlgorithm = "SHA512", RsaKeyLengths rsaKeyLength = RsaKeyLengths.Bit2048)
        {
            using (var rsa = CreateRsa(rsaKeyLength))
            {
                rsa.ImportParameters(privateKey);

                //.NET 4.5 doesn't let you specify padding, defaults to PKCS#1 v1.5 padding
                var signature = rsa.SignData(dataToSign, hashAlgorithm);
                return(signature);
            }
        }
コード例 #36
0
 public static string Encrypt(string publicKeyXml, string data, RsaKeyLengths rsaKeyLength = RsaKeyLengths.Bit2048)
 {
     return(RsaUtils.Encrypt(data, publicKeyXml, rsaKeyLength));
 }
コード例 #37
0
ファイル: CryptUtils.cs プロジェクト: humanbeinc/ServiceStack
 public static byte[] Decrypt(byte[] encryptedBytes, RSAParameters privateKey, RsaKeyLengths rsaKeyLength = RsaKeyLengths.Bit2048)
 {
     using (var rsa = new RSACryptoServiceProvider((int)rsaKeyLength))
     {
         rsa.ImportParameters(privateKey);
         byte[] bytes = rsa.Decrypt(encryptedBytes, DoOAEPPadding);
         return(bytes);
     }
 }
コード例 #38
0
ファイル: CryptUtils.cs プロジェクト: jmaucher/SStack
        /// <summary>
        /// Encrypt an arbitrary string of data under the supplied public key
        /// </summary>
        /// <param name="publicKey">The public key to encrypt under</param>
        /// <param name="data">The data to encrypt</param>
        /// <param name="length">The bit length or strength of the public key: 1024, 2048 or 4096 bits. This must match the
        /// value actually used to create the publicKey</param>
        /// <returns></returns>
        public static string Encrypt(string publicKey, string data, RsaKeyLengths length = RsaKeyLengths.Bit2048)
        {
            // full array of bytes to encrypt
            byte[] bytesToEncrypt;

            // worker byte array
            byte[] block;

            // encrypted bytes
            byte[] encryptedBytes;

            // length of bytesToEncrypt
            var dataLength = 0;

            // number of bytes in key
            var keySize = 0;

            // maximum block length to encrypt
            var maxLength = 0;

            // how many blocks must we encrypt to encrypt entire message?
            var iterations = 0;

            // the encrypted data
            var encryptedData = new StringBuilder();

            // instantiate the crypto provider with the correct key length
            var rsaCryptoServiceProvider = new RSACryptoServiceProvider((int)length);

            // initialize the RSA object from the given public key
            rsaCryptoServiceProvider.FromXmlString(publicKey);

            // convert data to byte array
            bytesToEncrypt = Encoding.Unicode.GetBytes(data);

            // get length of byte array
            dataLength = bytesToEncrypt.Length;

            // convert length of key from bits to bytes
            keySize = (int)length / 8;

            // .NET RSACryptoServiceProvider uses SHA1 Hash function
            // use this to work out the maximum length to encrypt per block
            maxLength = ((keySize - 2) - (2 * SHA1.Create().ComputeHash(bytesToEncrypt).Length));

            // how many blocks do we need to encrypt?
            iterations = dataLength / maxLength;

            // encrypt block by block
            for (int index = 0; index <= iterations; index++)
            {
                // is there more than one full block of data left to encrypt?
                if ((dataLength - maxLength * index) > maxLength)
                {
                    block = new byte[maxLength];
                }
                else
                {
                    block = new byte[dataLength - maxLength * index];
                }

                // copy the required number of bytes from the array of bytes to encrypt to our worker array
                Buffer.BlockCopy(bytesToEncrypt, maxLength * index, block, 0, block.Length);

                // encrypt the current worker array block of bytes
                encryptedBytes = rsaCryptoServiceProvider.Encrypt(block, true);

                // RSACryptoServiceProvider reverses the order of encrypted bytesToEncrypt after encryption and before decryption.
                // Undo this reversal for compatibility with other implementations
                Array.Reverse(encryptedBytes);

                // convert to base 64 string
                encryptedData.Append(Convert.ToBase64String(encryptedBytes));
            }

            return(encryptedData.ToString());
        }
コード例 #39
0
 public static byte[] Encrypt(byte[] bytes, RSAParameters publicKey, RsaKeyLengths rsaKeyLength = RsaKeyLengths.Bit2048)
 {
     using (var rsa = CreateRsa(rsaKeyLength))
     {
         rsa.ImportParameters(publicKey);
         return rsa.Encrypt(bytes);
     }
 }
コード例 #40
0
 public static byte[] Encrypt(byte[] bytes, string publicKeyXml, RsaKeyLengths rsaKeyLength = RsaKeyLengths.Bit2048)
 {
     using (var rsa = CreateRsa(rsaKeyLength))
     {
         rsa.FromXmlString(publicKeyXml);
         return rsa.Encrypt(bytes);
     }
 }
コード例 #41
0
ファイル: CryptUtils.cs プロジェクト: CLupica/ServiceStack
 public static bool Verify(byte[] dataToVerify, byte[] signature, RSAParameters publicKey, string hashAlgorithm = "SHA512", RsaKeyLengths rsaKeyLength = RsaKeyLengths.Bit2048)
 {
     using (var rsa = new RSACryptoServiceProvider((int)rsaKeyLength))
     {
         rsa.ImportParameters(publicKey);
         var verified = rsa.VerifyData(dataToVerify, hashAlgorithm, signature);
         return verified;
     }
 }
コード例 #42
0
 public static RsaKeyPair CreatePublicAndPrivateKeyPair(RsaKeyLengths rsaKeyLength = RsaKeyLengths.Bit2048)
 {
     return(RsaUtils.CreatePublicAndPrivateKeyPair(rsaKeyLength));
 }
コード例 #43
0
 public static string Decrypt(string privateKeyXml, string encryptedData, RsaKeyLengths rsaKeyLength = RsaKeyLengths.Bit2048)
 {
     return(RsaUtils.Encrypt(encryptedData, privateKeyXml, rsaKeyLength));
 }
コード例 #44
0
 public static byte[] Decrypt(byte[] encryptedBytes, RSAParameters privateKey, RsaKeyLengths rsaKeyLength = RsaKeyLengths.Bit2048)
 {
     using (var rsa = CreateRsa(rsaKeyLength))
     {
         rsa.ImportParameters(privateKey);
         byte[] bytes = rsa.Decrypt(encryptedBytes);
         return bytes;
     }
 }
コード例 #45
0
ファイル: CryptUtils.cs プロジェクト: pkmnfrk/ServiceStack
 public static byte[] Encrypt(byte[] bytes, string publicKeyXml, RsaKeyLengths rsaKeyLength = RsaKeyLengths.Bit2048)
 {
     using (var rsa = new RSACryptoServiceProvider((int)rsaKeyLength))
     {
         rsa.FromXmlString(publicKeyXml);
         return rsa.Encrypt(bytes, DoOAEPPadding);
     }
 }
コード例 #46
0
 private static RSA CreateRsa(RsaKeyLengths rsaKeyLength)
 {
     var rsa = RSA.Create();
     rsa.KeySize = (int)rsaKeyLength;
     return rsa;
 }
コード例 #47
0
ファイル: CryptUtils.cs プロジェクト: pkmnfrk/ServiceStack
 public static byte[] Encrypt(byte[] bytes, RSAParameters publicKey, RsaKeyLengths rsaKeyLength = RsaKeyLengths.Bit2048)
 {
     using (var rsa = new RSACryptoServiceProvider((int)rsaKeyLength))
     {
         rsa.ImportParameters(publicKey);
         return rsa.Encrypt(bytes, DoOAEPPadding);
     }
 }
コード例 #48
0
ファイル: CryptUtils.cs プロジェクト: pkmnfrk/ServiceStack
 public static string Decrypt(string encryptedText, RSAParameters privateKey, RsaKeyLengths rsaKeyLength = RsaKeyLengths.Bit2048)
 {
     var encryptedBytes = Convert.FromBase64String(encryptedText);
     var bytes = Decrypt(encryptedBytes, privateKey, rsaKeyLength);
     var data = Encoding.UTF8.GetString(bytes);
     return data;
 }
コード例 #49
0
 public static byte[] Decrypt(byte[] encryptedBytes, RSAParameters privateKey, RsaKeyLengths rsaKeyLength = RsaKeyLengths.Bit2048)
 {
     using (var rsa = CreateRsa(rsaKeyLength))
     {
         rsa.ImportParameters(privateKey);
         byte[] bytes = rsa.Decrypt(encryptedBytes);
         return(bytes);
     }
 }
コード例 #50
0
ファイル: CryptUtils.cs プロジェクト: pkmnfrk/ServiceStack
 public static byte[] Decrypt(byte[] encryptedBytes, string privateKeyXml, RsaKeyLengths rsaKeyLength = RsaKeyLengths.Bit2048)
 {
     using (var rsa = new RSACryptoServiceProvider((int)rsaKeyLength))
     {
         rsa.FromXmlString(privateKeyXml);
         byte[] bytes = rsa.Decrypt(encryptedBytes, DoOAEPPadding);
         return bytes;
     }
 }
コード例 #51
0
 public static bool Verify(byte[] dataToVerify, byte[] signature, RSAParameters publicKey, string hashAlgorithm = "SHA512", RsaKeyLengths rsaKeyLength = RsaKeyLengths.Bit2048)
 {
     using (var rsa = CreateRsa(rsaKeyLength))
     {
         rsa.ImportParameters(publicKey);
         var verified = rsa.VerifyData(dataToVerify, signature, hashAlgorithm);
         return(verified);
     }
 }
コード例 #52
0
ファイル: CryptUtils.cs プロジェクト: pkmnfrk/ServiceStack
 public static byte[] Decrypt(byte[] encryptedBytes, RSAParameters privateKey, RsaKeyLengths rsaKeyLength = RsaKeyLengths.Bit2048)
 {
     using (var rsa = new RSACryptoServiceProvider((int)rsaKeyLength))
     {
         rsa.ImportParameters(privateKey);
         byte[] bytes = rsa.Decrypt(encryptedBytes, DoOAEPPadding);
         return bytes;
     }
 }
コード例 #53
0
ファイル: CryptUtils.cs プロジェクト: pkmnfrk/ServiceStack
 public static string Encrypt(string text, RSAParameters publicKey, RsaKeyLengths rsaKeyLength = RsaKeyLengths.Bit2048)
 {
     var bytes = Encoding.UTF8.GetBytes(text);
     var encryptedBytes = Encrypt(bytes, publicKey, rsaKeyLength);
     string encryptedData = Convert.ToBase64String(encryptedBytes);
     return encryptedData;
 }
コード例 #54
0
        public static byte[] Authenticate(byte[] dataToSign, RSAParameters privateKey, string hashAlgorithm = "SHA512", RsaKeyLengths rsaKeyLength = RsaKeyLengths.Bit2048)
        {
            using (var rsa = CreateRsa(rsaKeyLength))
            {
                rsa.ImportParameters(privateKey);

                //.NET 4.5 doesn't let you specify padding, defaults to PKCS#1 v1.5 padding
                var signature = rsa.SignData(dataToSign, hashAlgorithm);
                return signature;
            }
        }