Beispiel #1
0
 // Function to Generate a 64 bits Key.
 static string GenerateKey() 
 {
    // Create an instance of Symetric Algorithm. Key and IV is generated automatically.
    DESCryptoServiceProvider desCrypto =(DESCryptoServiceProvider)DESCryptoServiceProvider.Create();
    // Use the Automatically generated key for Encryption. 
    return ASCIIEncoding.ASCII.GetString(desCrypto.Key);
 }
Beispiel #2
0
        public void DecryptStream(Stream sourcestream, Stream deststream, string key)
        {
            byte[] _key = new byte[8];
            byte[] _IV  = new byte[8];

            byte[] myKey = Encoding.ASCII.GetBytes(key);

            for (int i = 0; i < _key.Length; i++)
            {
                _key[i] = 0;
            }
            for (int i = 0; (i < _key.Length) && (i < myKey.Length); i++)
            {
                _key[i] = myKey[i];
            }
            for (int i = 0; (i < _key.Length) && (i < _IV.Length); i++)
            {
                _IV[i] = _key[i];
            }
            _IV.Reverse();

            DES des = DESCryptoServiceProvider.Create();

            des.IV  = _IV;
            des.Key = _key;

            var decStream = new CryptoStream(sourcestream, des.CreateDecryptor(), CryptoStreamMode.Read);

            deststream.SetLength(0);
            decStream.CopyTo(deststream);
        }
Beispiel #3
0
        /// Encrypts the given byte array using the given key. The resulting
        /// cipher text is then converted to base-64 encoding.
        /// </summary>
        /// <param name="bytesPlain"></param>
        /// <param name="strKey"></param>
        /// <returns></returns>
        public static string Encrypt(byte[] bytesPlain, string strKey)
        {
            DES des = DESCryptoServiceProvider.Create();

            des.Mode = CipherMode.CBC;
            byte[] bytesKey = MakeCipherKey(strKey, des);
            des.GenerateIV();
            byte[]        bytesIV   = des.IV;
            StringBuilder sb        = new StringBuilder();
            MemoryStream  ms        = new MemoryStream();
            CryptoStream  encStream = new CryptoStream(ms, des.CreateEncryptor(bytesKey, bytesIV), CryptoStreamMode.Write);
            // Encrypt the data using default encoding
            int remainder = bytesPlain.Length % des.BlockSize;

            byte[] bytesToEncrypt = new byte[bytesPlain.Length + remainder];
            bytesPlain.CopyTo(bytesToEncrypt, 0);
            encStream.Write(bytesToEncrypt, 0, bytesPlain.Length);
            encStream.FlushFinalBlock();
            encStream.Close();
            byte[] cryptText = ms.ToArray();
            sb.Append(HexStringFromBytes(cryptText, cryptText.Length));
            sb.Append(_MARKER);
            sb.Append(HexStringFromBytes(bytesIV, bytesIV.Length));
            return(sb.ToString());
        }
Beispiel #4
0
        // 创建Key
        public string GenerateKey()
        {
            DESCryptoServiceProvider desCrypto = (DESCryptoServiceProvider)DESCryptoServiceProvider.Create();

            // return ASCIIEncoding.ASCII.GetString(desCrypto.Key);
            return("?????c.?");
        }
Beispiel #5
0
    private static void EncryptData(String inName, String outName, byte[] desKey, byte[] desIV)
    {
        FileStream fs = new FileStream(inName, FileMode.Open, FileAccess.Read);
        // Create an instance of the DESCryptoServiceProvider cipher
        SymmetricAlgorithm aes = DESCryptoServiceProvider.Create();

        // set the key to be the derivedKey computed above
        aes.Key = desKey;
        // set the IV to be all zeros
        aes.IV = desIV;  // arrays are zero-initialized
        // now wrap an encryption transform around the filestream
        CryptoStream stream1 = new CryptoStream(fs, aes.CreateEncryptor(), CryptoStreamMode.Read);
        // The result of reading from stream1 is ciphertext, but we want it
        // base64-encoded, so wrap another transform around it
        CryptoStream stream2 = new CryptoStream(stream1, new ToBase64Transform(), CryptoStreamMode.Read);

        FileStream fsout = new FileStream(outName, FileMode.OpenOrCreate);

        byte[] buffer = new byte[1024];
        int    bytesRead;

        do
        {
            bytesRead = stream2.Read(buffer, 0, 1024);
            fsout.Write(buffer, 0, bytesRead);
        } while (bytesRead > 0);
        fsout.Flush();
        fsout.Close();
    }
        private void btnGenerateKey_Click(object sender, EventArgs e)
        {
            DESCryptoServiceProvider desCrypto = (DESCryptoServiceProvider)DESCryptoServiceProvider.Create();

            desCrypto.GenerateKey();
            tbKey.Text = Convert.ToBase64String(desCrypto.Key);
        }
Beispiel #7
0
    private static void DecryptData(String inName, String outName, byte[] desKey, byte[] desIV)
    {
        FileStream fs = new FileStream(inName, FileMode.Open, FileAccess.Read);
        // Create an instance of the DESCryptoServiceProvider cipher
        SymmetricAlgorithm aes = DESCryptoServiceProvider.Create();

        // set the key to be the derivedKey computed above
        aes.Key = desKey;
        // set the IV to be all zeros
        aes.IV = desIV;  // arrays are zero-initialized
        // Base64-decode the ciphertext
        CryptoStream stream1 = new CryptoStream(fs, new FromBase64Transform(FromBase64TransformMode.IgnoreWhiteSpaces), CryptoStreamMode.Read);
        // now wrap a decryption transform around stream1
        CryptoStream stream2 = new CryptoStream(stream1, aes.CreateDecryptor(), CryptoStreamMode.Read);
        FileStream   fsout   = new FileStream(outName, FileMode.OpenOrCreate);

        byte[]       buffer = new byte[1024];
        int          bytesRead;
        UTF8Encoding utf8 = new UTF8Encoding();

        do
        {
            bytesRead = stream2.Read(buffer, 0, 1024);
            fsout.Write(buffer, 0, bytesRead);
        } while (bytesRead > 0);
        fsout.Flush();
        fsout.Close();
    }
Beispiel #8
0
        private string generiraj_kljuc()
        {
            DESCryptoServiceProvider desCrypto = (DESCryptoServiceProvider)DESCryptoServiceProvider.Create();

            imamo_kljuc = true;
            return(ASCIIEncoding.ASCII.GetString(desCrypto.Key));
        }
        public static string GetDESDecryptedUrl(string enc_value)
        {
            enc_value = enc_value.Replace("\\/", "/");
            Debug.Write("Decrypting enc_media_url: " + enc_value);
            byte[] inputByteArray = Convert.FromBase64String(enc_value);

            DES desObj = DESCryptoServiceProvider.Create();

            desObj.Mode    = CipherMode.ECB;
            desObj.Padding = PaddingMode.PKCS7;
            desObj.Key     = Encoding.ASCII.GetBytes("38346591");
            desObj.IV      = Encoding.ASCII.GetBytes("\0\0\0\0\0\0\0\0");
            var desCipher = desObj.CreateDecryptor();

            byte[] outputByteArray = desCipher.TransformFinalBlock(inputByteArray, 0, inputByteArray.Length);
            string dec_value       = Encoding.UTF8.GetString(outputByteArray);

            Debug.WriteLine("Decrypted value : " + dec_value);
            if (!dec_value.Contains("/"))
            {
                Debug.WriteLine("Decrypted URL:" + dec_value + "No resource URL for this song");
                return(null);
            }
            string media_url = FormMediaUrl(dec_value);

            Debug.WriteLine("Decrypted Media URL : " + media_url);
            return(media_url);
        }
Beispiel #10
0
 public static string GenerateKey()
 {
     using (DES desCrypto = DESCryptoServiceProvider.Create())
     {
         return(ASCIIEncoding.ASCII.GetString(desCrypto.Key));
     }
 }
Beispiel #11
0
        // Function to Generate a 64 bits Key.
        static DESCryptoServiceProvider GenerateKey()
        {
            // Create an instance of Symetric Algorithm. Key and IV is generated automatically.
            DESCryptoServiceProvider desCrypto = (DESCryptoServiceProvider)DESCryptoServiceProvider.Create();

            // Use the Automatically generated key for Encryption.
            return(desCrypto);
        }
Beispiel #12
0
        /// <summary>
        /// 生成一个64位的密钥
        /// </summary>
        /// <returns>string</returns>
        public static string GenerateKey()
        {
            //创建对称算法的一个实例。自动生成的密钥和IV。
            DESCryptoServiceProvider desCrypto = (DESCryptoServiceProvider)DESCryptoServiceProvider.Create();

            // 使用自动生成的密钥进行加密。
            return(ASCIIEncoding.ASCII.GetString(desCrypto.Key));
        }
Beispiel #13
0
        /// <summary>
        /// 生成密钥字符串
        /// Function to Generate a 64 bits Key.
        /// </summary>
        /// <returns></returns>
        public static string GenerateKey()
        {
            // Create an instance of Symetric Algorithm. Key and IV is generated automatically.
            DESCryptoServiceProvider desCrypto = (DESCryptoServiceProvider)DESCryptoServiceProvider.Create();

            // Use the Automatically generated key for Encryption.
            return(Convert.ToBase64String(desCrypto.Key));
        }
        public static byte[] Encrypt(byte[] input, byte[] key, byte[] salt, string algorithm)
        {
            if (input == null || input.Length < 1 ||
                key == null || key.Length < 1 ||
                salt == null || salt.Length < 1)
            {
                throw new ArgumentException();
            }

            System.Security.Cryptography.SymmetricAlgorithm sAlgorithm;
            switch (algorithm.ToUpperInvariant())
            {
            case "DES":
                sAlgorithm = DESCryptoServiceProvider.Create();
                break;

            case "3DES":
            case "TDES":
            case "TRIPLEDES":
                sAlgorithm = TripleDESCryptoServiceProvider.Create();
                break;

            case "AES":
                sAlgorithm = AesCryptoServiceProvider.Create();
                break;

            case "RC2":
                sAlgorithm = RC2CryptoServiceProvider.Create();
                break;

            case "RIJNDAEL":
                sAlgorithm = new RijndaelManaged();
                break;

            default:
                throw new NotSupportedException();
            }
            DeriveBytes rdb = new Rfc2898DeriveBytes(key, salt, 1024);

            sAlgorithm.Key = rdb.GetBytes(sAlgorithm.KeySize / 8);
            sAlgorithm.IV  = rdb.GetBytes(sAlgorithm.BlockSize / 8);
            byte[] reslut;
            using (ICryptoTransform encryptor = sAlgorithm.CreateEncryptor(sAlgorithm.Key, sAlgorithm.IV))
            {
                using (MemoryStream stream = new MemoryStream())
                {
                    using (CryptoStream cryptoStream = new CryptoStream(stream, encryptor, CryptoStreamMode.Write))
                    {
                        cryptoStream.Write(input, 0, input.Length);
                        cryptoStream.FlushFinalBlock();
                        cryptoStream.Clear();
                    }
                    reslut = stream.ToArray();
                }
            }
            sAlgorithm.Clear();
            return(reslut);
        }
Beispiel #15
0
        //function to generate a 64 bit key
        private string GenerateKey()
        {
            // Create an instance of Symetric Algorithm. Key and IV is generated automatically.
            DESCryptoServiceProvider desCrypto = (DESCryptoServiceProvider)DESCryptoServiceProvider.Create();

            // Set key to a pre-determined set of bytes (of length 8):
            desCrypto.Key = new byte[] { 0x4b, 0x4a, 0x41, 0x52, 0x54, 0x41, 0x4e, 0x21 };
            // Use the Automatically generated key for Encryption.
            return(ASCIIEncoding.ASCII.GetString(desCrypto.Key));
        }
Beispiel #16
0
        // Функция генерации 64-битного ключа
        static void GenerateKey(string Keyname)
        {
            // Создаем экземпляр симметричного алгоритма. Ключ и IV генерируются автоматически
            DESCryptoServiceProvider desCrypto =
                (DESCryptoServiceProvider)DESCryptoServiceProvider.Create();
            // Используем автоматически сгенерированный ключ для шифрования
            StreamWriter sw = new StreamWriter(Keyname);

            sw.WriteLine(ASCIIEncoding.ASCII.GetString(desCrypto.Key));
            sw.Close();
        }
Beispiel #17
0
        /// <summary>
        /// 解密字符串
        /// </summary>
        /// <param name="inputStr">要解密的字符串</param>
        /// <param name="keyStr">密钥</param>
        /// <returns>解密后的结果</returns>
        static public string SDecryptString(string inputStr, string keyStr)
        {
            if (string.IsNullOrEmpty(keyStr))
            {
                throw new Exception("加密密钥不能为空");
            }

            DesCryptoService des = new DesCryptoService(keyStr, DESCryptoServiceProvider.Create());

            return(des.DecryptString(inputStr));
        }
Beispiel #18
0
        /// <summary>
        /// 解密文件
        /// </summary>
        /// <param name="filePath">输入文件路径</param>
        /// <param name="savePath">解密后输出文件路径</param>
        /// <param name="keyStr">密码,可以为“”</param>
        /// <returns></returns>
        public static bool DecryptFile(string filePath, string savePath, string keyStr)
        {
            if (string.IsNullOrEmpty(keyStr))
            {
                throw new Exception("加密密钥不能为空");
            }

            DesCryptoService des = new DesCryptoService(keyStr, DESCryptoServiceProvider.Create());

            return(des.DecryptFile(filePath, savePath));
        }
Beispiel #19
0
        private string generateRandChars()
        {
            /* Random r = new Random();
             * byte[] rs = new byte[31];
             * r.NextBytes(rs);
             * return Encoding.UTF8.GetString(rs);
             */
            // Create an instance of Symetric Algorithm. Key and IV is generated automatically.
            DESCryptoServiceProvider desCrypto = (DESCryptoServiceProvider)DESCryptoServiceProvider.Create();

            // Use the Automatically generated key for Encryption.
            return(ASCIIEncoding.ASCII.GetString(desCrypto.Key));
        }
Beispiel #20
0
        /// <summary>
        /// Method for decrypting data
        /// </summary>
        /// <param name="encrypter">What encryption is used</param>
        /// <param name="message">The message to decrypt</param>
        /// <param name="key">The encryption key</param>
        /// <param name="iv">the encryption iv</param>
        /// <returns>Returns a decrypted byte[]</returns>
        public static void DecryptFolder(Encrypters encrypter, byte[] key, byte[] iv)
        {
            watch = new Stopwatch();
            watch.Start();
            SymmetricAlgorithm algorithm = null;

            switch (encrypter)
            {
            case Encrypters.DES:
                algorithm = DESCryptoServiceProvider.Create();
                break;

            case Encrypters.TRIPLEDES:
                algorithm = TripleDESCryptoServiceProvider.Create();
                break;

            case Encrypters.AES:
                algorithm = AesCryptoServiceProvider.Create();
                break;
            }

            algorithm.Mode    = CipherMode.CBC;
            algorithm.Padding = PaddingMode.PKCS7;
            try
            {
                algorithm.Key = key;
                algorithm.IV  = iv;
            }
            catch (FormatException e)
            {
                Console.WriteLine("Make sure to use the correct encryption method");
                throw e;
            }

            using (var memoryStream = new MemoryStream())
            {
                var cryptoStream = new CryptoStream(memoryStream, algorithm.CreateDecryptor(),
                                                    CryptoStreamMode.Write);

                foreach (string file in Directory.GetFiles(Environment.CurrentDirectory, "*.txt"))
                {
                    byte[] fileText = File.ReadAllBytes(file);
                    cryptoStream.Write(fileText, 0, fileText.Length);
                    cryptoStream.FlushFinalBlock();
                    File.WriteAllBytes(file, memoryStream.ToArray());
                }

                watch.Stop();
                Console.WriteLine("Stopwatch took " + watch.ElapsedMilliseconds + "ms to decrypt");
            }
        }
        string GenerateKey()
        {
            // Create an instance of Symetric Algorithm. Key and IV is generated automatically.
            DESCryptoServiceProvider desCrypto = (DESCryptoServiceProvider)DESCryptoServiceProvider.Create();
            string       key       = ASCIIEncoding.ASCII.GetString(desCrypto.Key);
            StreamWriter keyWriter = new StreamWriter("encryption._key");

            keyWriter.Write(key);
            keyWriter.Flush();
            keyWriter.Close();

            // Use the Automatically generated _key for Encryption.
            return(key);
        }
Beispiel #22
0
        public static string GenerateKey(bool encoded)
        {
            DESCryptoServiceProvider desCrypto = (DESCryptoServiceProvider)DESCryptoServiceProvider.Create();
            string ret = ASCIIEncoding.ASCII.GetString(desCrypto.Key);

            if (!encoded)
            {
                return(ret);
            }
            else
            {
                return(Base64Encode(ret));
            }
        }
Beispiel #23
0
        private static byte[] DESEncrypt(byte[] source, string key)
        {
            var des = DESCryptoServiceProvider.Create();

            des.Mode = CipherMode.ECB;
            des.Padding = PaddingMode.PKCS7;
            des.Key = Encoding.UTF8.GetBytes(key);
            des.IV = IV;

            var transformer = des.CreateEncryptor();

            var targetStream = transformer.TransformFinalBlock(source, 0, source.Length);
            return targetStream;
        }
Beispiel #24
0
        // 创建Key
        public static string GenerateKey()
        {
            DESCryptoServiceProvider desCrypto = (DESCryptoServiceProvider)DESCryptoServiceProvider.Create();
            string key = ASCIIEncoding.ASCII.GetString(desCrypto.Key);

            while (key.Contains(SysConstManage.DBInfoArraySeparator) || key.Contains(SysConstManage.DBInfoArraySeparator2) || key.Contains("'"))
            {
                desCrypto = (DESCryptoServiceProvider)DESCryptoServiceProvider.Create();
                key       = ASCIIEncoding.ASCII.GetString(desCrypto.Key);
            }
            return(key);
            ////desCrypto.Key = new byte[] {0,2,3,4};
            //return ASCIIEncoding.ASCII.GetString(desCrypto.Key);
        }
Beispiel #25
0
        /// <summary>
        /// Method for encrypting txt files in current directory
        /// </summary>
        /// <param name="encrypter">Method for encryption</param>
        /// <param name="key">The key used to encrypt with</param>
        /// <param name="iv">The iv used to encrypt with</param>
        public static void EncryptFolder(Encrypters encrypter, byte[] key, byte[] iv)
        {
            watch = new Stopwatch();
            watch.Start();
            SymmetricAlgorithm algorithm = null;

            switch (encrypter)
            {
            case Encrypters.DES:
                algorithm = DESCryptoServiceProvider.Create();
                break;

            case Encrypters.TRIPLEDES:
                algorithm = TripleDESCryptoServiceProvider.Create();
                break;

            case Encrypters.AES:
                algorithm = AesCryptoServiceProvider.Create();
                break;
            }

            algorithm.Mode    = CipherMode.CBC;
            algorithm.Padding = PaddingMode.PKCS7;

            algorithm.Key = key;
            algorithm.IV  = iv;

            using (var memoryStream = new MemoryStream())
            {
                var cryptoStream = new CryptoStream(memoryStream, algorithm.CreateEncryptor(),
                                                    CryptoStreamMode.Write);

                foreach (string file in Directory.GetFiles(Environment.CurrentDirectory, "*.txt"))
                {
                    byte[] fileText = File.ReadAllBytes(file);

                    cryptoStream.Write(fileText, 0, fileText.Length);
                    cryptoStream.FlushFinalBlock();
                    File.WriteAllBytes(file, memoryStream.ToArray());
                }

                watch.Stop();
                Console.WriteLine("Stopwatch took: " + watch.ElapsedMilliseconds + "ms to complete \n" +
                                  "Remember to save the key and IV somewhere");
                Console.WriteLine("Key:" + Convert.ToBase64String(algorithm.Key) + "\n" +
                                  "IV: " + Convert.ToBase64String(algorithm.IV));
            }
        }
        /// <summary>
        /// 対称アルゴリズムによる
        /// 暗号化サービスプロバイダを生成
        /// </summary>
        /// <param name="esa">
        /// 対称アルゴリズムによる
        /// 暗号化サービスプロバイダの種類
        /// </param>
        /// <returns>
        /// 対称アルゴリズムによる
        /// 暗号化サービスプロバイダ
        /// </returns>
        private static SymmetricAlgorithm CreateSymmetricAlgorithm(EnumSymmetricAlgorithm esa)
        {
            SymmetricAlgorithm sa = null;

            // AesCryptoServiceProvider, AesManagedは.NET Framework 3.5からの提供。
            // 暗号化プロバイダ選択の優先順は、高い順に、Managed → CAPI(CSP) → CNG。
            // Aesは、ManagedがあるのでCAPI(CSP)のAesCryptoServiceProviderを削除。
            // サポート範囲の変更により、今後、CAPI(CSP)とCNGの優先順位の反転を検討。

            //if (esa == EnumSymmetricAlgorithm.AesCryptoServiceProvider)
            //{
            //    // AesCryptoServiceProviderサービスプロバイダ
            //    sa = AesCryptoServiceProvider.Create(); // devps(1703)
            //}
            //else
            if (esa == EnumSymmetricAlgorithm.AesManaged)
            {
                // AesManagedサービスプロバイダ
                sa = AesManaged.Create(); // devps(1703)
            }
            else if (esa == EnumSymmetricAlgorithm.DESCryptoServiceProvider)
            {
                // DESCryptoServiceProviderサービスプロバイダ
                sa = DESCryptoServiceProvider.Create(); // devps(1703)
            }
            else if (esa == EnumSymmetricAlgorithm.RC2CryptoServiceProvider)
            {
                // RC2CryptoServiceProviderサービスプロバイダ
                sa = RC2CryptoServiceProvider.Create(); // devps(1703)
            }
            else if (esa == EnumSymmetricAlgorithm.RijndaelManaged)
            {
                // RijndaelManagedサービスプロバイダ
                sa = RijndaelManaged.Create(); // devps(1703)
            }
            else if (esa == EnumSymmetricAlgorithm.TripleDESCryptoServiceProvider)
            {
                // TripleDESCryptoServiceProviderサービスプロバイダ
                sa = TripleDESCryptoServiceProvider.Create(); // devps(1703)
            }
            else
            {
                throw new ArgumentException(
                          PublicExceptionMessage.ARGUMENT_INJUSTICE, "EnumSymmetricAlgorithm esa");
            }

            return(sa);
        }
Beispiel #27
0
        /// <summary>
        /// 生成随机密钥
        /// </summary>
        /// <returns></returns>
        public static byte[] CreateKeyBytes(enmSymmetricAlgorithmType enmType)
        {
            SymmetricAlgorithm desCrypto = null;

            switch (enmType)
            {
            case enmSymmetricAlgorithmType.DES:
                desCrypto = (DESCryptoServiceProvider)DESCryptoServiceProvider.Create();
                return(desCrypto.Key);

            case enmSymmetricAlgorithmType.RIJNDAEL:
                desCrypto         = Rijndael.Create();
                desCrypto.KeySize = 256;
                return(desCrypto.Key);
            }
            return(null);
        }
        /// <summary>
        /// 根据KEY生成DES
        /// </summary>
        /// <param name="key">The key.</param>
        /// <returns>DES</returns>
        public static DES CreateDES(string key)
        {
            DES _des = new DESCryptoServiceProvider();
            DESCryptoServiceProvider _desCrypto = (DESCryptoServiceProvider)DESCryptoServiceProvider.Create();

            if (!string.IsNullOrEmpty(key))
            {
                MD5 _md5 = new MD5CryptoServiceProvider();
                _des.Key = ArrayHelper.Copy <byte>(_md5.ComputeHash(Encoding.UTF8.GetBytes(key)), 0, 8);
            }
            else
            {
                _des.Key = _desCrypto.Key;
            }

            _des.IV = _des.IV;
            return(_des);
        }
Beispiel #29
0
        /// <summary>
        /// 対称アルゴリズムによる
        /// 暗号化サービスプロバイダを生成
        /// </summary>
        /// <param name="esa">
        /// 対称アルゴリズムによる
        /// 暗号化サービスプロバイダの種類
        /// </param>
        /// <returns>
        /// 対称アルゴリズムによる
        /// 暗号化サービスプロバイダ
        /// </returns>
        private static SymmetricAlgorithm CreateSymmetricAlgorithm(EnumSymmetricAlgorithm esa)
        {
            SymmetricAlgorithm sa = null;

            // AesCryptoServiceProvider, AesManagedは3.5からの提供。

            if (esa == EnumSymmetricAlgorithm.AesCryptoServiceProvider)
            {
                // AesCryptoServiceProviderサービスプロバイダ
                sa = AesCryptoServiceProvider.Create(); // devps(1703)
            }
            else if (esa == EnumSymmetricAlgorithm.AesManaged)
            {
                // AesManagedサービスプロバイダ
                sa = AesManaged.Create(); // devps(1703)
            }
            else if (esa == EnumSymmetricAlgorithm.DESCryptoServiceProvider)
            {
                // DESCryptoServiceProviderサービスプロバイダ
                sa = DESCryptoServiceProvider.Create(); // devps(1703)
            }
            else if (esa == EnumSymmetricAlgorithm.RC2CryptoServiceProvider)
            {
                // RC2CryptoServiceProviderサービスプロバイダ
                sa = RC2CryptoServiceProvider.Create(); // devps(1703)
            }
            else if (esa == EnumSymmetricAlgorithm.RijndaelManaged)
            {
                // RijndaelManagedサービスプロバイダ
                sa = RijndaelManaged.Create(); // devps(1703)
            }
            else if (esa == EnumSymmetricAlgorithm.TripleDESCryptoServiceProvider)
            {
                // TripleDESCryptoServiceProviderサービスプロバイダ
                sa = TripleDESCryptoServiceProvider.Create(); // devps(1703)
            }
            else
            {
                throw new ArgumentException(
                          PublicExceptionMessage.ARGUMENT_INJUSTICE, "EnumSymmetricAlgorithm esa");
            }

            return(sa);
        }
Beispiel #30
0
        // Function to Generate a 64 bits Key.
        /// <summary>
        /// 创建加密
        /// </summary>
        /// <returns></returns>
        public static string GenerateKey()
        {
            // Create an instance of Symetric Algorithm. Key and IV is generated automatically.
            DESCryptoServiceProvider desCrypto = (DESCryptoServiceProvider)DESCryptoServiceProvider.Create();

            //string curveName = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + "\\key.txt";
            //StreamWriter sw = new StreamWriter(curveName);
            if (string.IsNullOrEmpty(RWconfig.GetAppSettings("code")))
            {
                string ss = BitConverter.ToString(desCrypto.Key);
                RWconfig.SetAppSettings("code", ss.ToString());
            }
            //sw.WriteLine(BitConverter.ToString(desCrypto.Key));
            //sw.Close();
            //sw.Dispose();
            //RWconfig.SetAppSettings("code",BitConverter.ToString(desCrypto.Key));
            // Use the Automatically generated key for Encryption.
            return(ASCIIEncoding.ASCII.GetString(desCrypto.Key));
        }