我项目的加密/解密算法 [英] Encryption/Decryption algorithm for my project

查看:49
本文介绍了我项目的加密/解密算法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨!
我的项目需要加密/解密算法.它应该加密给定的值,该值可以同时包含字母和数字字符,并且还应该能够解密.

我应该使用哪种算法?

感谢您的帮助!

解决方案

您可能应该阅读一些入门资料.起点可能是《 NetAction使用加密软件指南》 [ 使用系统; 使用 System.Data; 使用 System.Configuration; 使用 System.Web; 使用 System.Web.Security; 使用 System.Web.UI; 使用使用System.Web.UI.WebControls; 使用 System.Web.UI.WebControls.WebParts; 使用 System.Web.UI.HtmlControls; 使用 System.IO; 使用 System.Text; 使用使用System.Security.Cryptography; 命名空间 MSIS.SPH.Common { 公共 EncryptionDecryption { 私有 静态 字符串 Encrypt(字符串 plainText,字符串 passPhrase,字符串 saltValue,字符串 hashAlgorithm, int passwordIterations,字符串 initVector, int keySize) { // 将字符串转换为字节数组. // 让我们假设字符串仅包含ASCII码. // 如果字符串包含Unicode字符,请使用Unicode,UTF7或UTF8 // 编码. 字节 [] initVectorBytes = ; initVectorBytes = Encoding.ASCII.GetBytes(initVector); 字节 [] saltValueBytes = ; saltValueBytes = Encoding.ASCII.GetBytes(saltValue); // 将我们的纯文本转换为字节数组. // 让我们假设纯文本包含UTF8编码的字符. 字节 [] plainTextBytes = ; plainTextBytes = Encoding.UTF8.GetBytes(plainText); PasswordDeriveBytes password = 默认(PasswordDeriveBytes); 密码= PasswordDeriveBytes(passPhrase,saltValueBytes,hashAlgorithm,passwordIterations); // 使用密码生成用于加密的伪随机字节 // 键.以字节(而不是位)为单位指定密钥的大小. 字节 [] keyBytes = ; keyBytes = password.GetBytes(keySize/ 8 ); // 创建未初始化的Rijndael加密对象. RijndaelManaged symmetricKey = 默认(RijndaelManaged); symmetricKey = RijndaelManaged(); // 将加密模式设置为密码块链接是合理的 // (CBC).对其他对称键参数使用默认选项. symmetricKey.Mode = CipherMode.CBC; // 根据现有密钥字节和初始化生成加密器 // 向量.密钥大小将根据密钥数进行定义 // 字节. ICryptoTransform encryptionor = 默认(ICryptoTransform); cryptoor = symmetricKey.CreateEncryptor(keyBytes,initVectorBytes); // 定义将用于保存加密数据的内存流. MemoryStream memoryStream = 默认(MemoryStream); memoryStream = MemoryStream(); // 定义加密流(始终使用写入模式进行加密). CryptoStream cryptoStream = 默认(CryptoStream); cryptoStream = CryptoStream(内存流,加密器,CryptoStreamMode.Write); // 开始加密. cryptoStream.Write(plainTextBytes, 0 ,plainTextBytes.Length); // 完成加密. cryptoStream.FlushFinalBlock(); // 将我们的加密数据从内存流转换为字节数组. 字节 [] cipherTextBytes = ; cipherTextBytes = memoryStream.ToArray(); // 关闭两个流. memoryStream.Close(); cryptoStream.Close(); // 将加密的数据转换为base64编码的字符串. 字符串 cipherText = ; cipherText = Convert.ToBase64String(cipherTextBytes); // 返回加密的字符串. 返回 cipherText; } 私有 静态 字符串 Decrypt(字符串 cipherText,字符串 passPhrase,字符串 saltValue,字符串 hashAlgorithm, int passwordIterations,字符串 initVector, int keySize) { // 将定义加密密钥特征的字符串转换为字节 // 数组.让我们假设字符串仅包含ASCII码. // 如果字符串包含Unicode字符,请使用Unicode,UTF7或UTF8 // 编码. 字节 [] initVectorBytes = ; initVectorBytes = Encoding.ASCII.GetBytes(initVector); 字节 [] saltValueBytes = ; saltValueBytes = Encoding.ASCII.GetBytes(saltValue); // 将我们的密文转换为字节数组. 字节 [] cipherTextBytes = ; cipherTextBytes = Convert.FromBase64String(cipherText); // 首先,我们必须创建一个密码,该密码将来自 // 派生.该密码将从指定的生成 // 密码和盐值.密码将使用创建 // 指定的哈希算法.密码创建可以在中完成 // 多次迭代. PasswordDeriveBytes password = 默认(PasswordDeriveBytes); 密码= PasswordDeriveBytes(passPhrase,saltValueBytes,hashAlgorithm,passwordIterations); // 使用密码生成用于加密的伪随机字节 // 键.以字节(而不是位)为单位指定密钥的大小. 字节 [] keyBytes = ; keyBytes = password.GetBytes(keySize/ 8 ); // 创建未初始化的Rijndael加密对象. RijndaelManaged symmetricKey = 默认(RijndaelManaged); symmetricKey = RijndaelManaged(); // 将加密模式设置为密码块链接是合理的 // (CBC).对其他对称键参数使用默认选项. symmetricKey.Mode = CipherMode.CBC; // 根据现有密钥字节和初始化生成解密器 // 向量.密钥大小将根据密钥数进行定义 // 字节. ICryptoTransform解密器= 默认(ICryptoTransform); 解密器= symmetricKey.CreateDecryptor(keyBytes,initVectorBytes); // 定义将用于保存加密数据的内存流. MemoryStream memoryStream = 默认(MemoryStream); memoryStream = MemoryStream(cipherTextBytes); // 定义将用于保存加密数据的内存流. CryptoStream cryptoStream = 默认(CryptoStream); cryptoStream = CryptoStream(memoryStream,解密器,CryptoStreamMode.Read); // 由于目前我们还不知道解密数据的大小 // 将分配足够长的缓冲区以容纳密文; // 纯文本永远不会比密文长. 字节 [] plainTextBytes = 字节 [ cipherTextBytes.Length]; int decryptedByteCount = 0 ; unlockedByteCount = cryptoStream.Read(plainTextBytes, 0 ,plainTextBytes.Length); // 关闭两个流. memoryStream.Close(); cryptoStream.Close(); // 将解密的数据转换为字符串. // 让我们假设原始明文字符串是UTF8编码的. 字符串 plainText = ; plainText = Encoding.UTF8.GetString(plainTextBytes, 0 ,decryptedByteCount); // 返回已解密的字符串. 返回 plainText; } 公共 静态 字符串 EncryptValue(字符串 strString) { 如果(字符串.IsNullOrEmpty(strString.Trim()))返回 " ; 字符串 strplainText = 字符串 strcipherText = ; 字符串 strpassPhrase = 字符串 strsaltValue = 字符串 strhashAlgorithm = ; int intpasswordIterations = 0 ; 字符串 strinitVector = int intkeySize = 0 ; strplainText = strString; // 你好,世界!-abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ 1234567890-=!@#


%^& *()_ + [];,./<>?:'{} | \'原始明文 strpassPhrase = " ; // 可以是任何字符串 strsaltValue = " ; // 可以是任何字符串 strhashAlgorithm = " ; // 可以是"MD5" intpasswordIterations = 2 ; // 可以是任何数字 strinitVector = " ; // 必须为16个字节 intkeySize = 256 ; // 可以是192或128 // Response.Write(String.Format("Plaintext:{0}",plainText)) // Response.Write(< br>") strcipherText =加密(strplainText,strpassPhrase,strsaltValue,strhashAlgorithm,intpasswordIterations,strinitVector,intkeySize); // Response.Write(String.Format("Encrypted:{0}",cipherText)) // Response.Write(< br>") 返回 strcipherText; } 公共 静态 字符串 DecryptValue(字符串 strString) { 如果(字符串.IsNullOrEmpty(strString.Trim()))返回 " ; 字符串 strplainText = 字符串 strcipherText = ; 字符串 strpassPhrase = 字符串 strsaltValue = 字符串 strhashAlgorithm = ; int intpasswordIterations = 0 ; 字符串 strinitVector = int intkeySize = 0 ; strplainText = strString; // 你好,世界!-abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ 1234567890-=!@#


Hi!
I need an encryption/decryption algorithm for my project. It should encrypt a given value which may contain both alphabetic as well as numberic characters and should also be able to decrypt.

Which algorithm should I use?

Thanks for your help!

You should probably read some introductory material. A starting point might be NetAction''s Guide to Using Encryption Software[^].


Hi Keyrun

plz copy and paste algo written below and call it. will work fine

any problem contact on
REMOVED@gmail.com
regards,

satan singh

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Text;
using System.Security.Cryptography;

namespace MSIS.SPH.Common
{


    public class EncryptionDecryption
    {
       

        private static string Encrypt(string plainText, string passPhrase, string saltValue, string hashAlgorithm, int passwordIterations, string initVector, int keySize)
        {

            // Convert strings into byte arrays. 
            // Let us assume that strings only contain ASCII codes. 
            // If strings include Unicode characters, use Unicode, UTF7, or UTF8 
            // encoding. 
            byte[] initVectorBytes = null;
            initVectorBytes = Encoding.ASCII.GetBytes(initVector);

            byte[] saltValueBytes = null;
            saltValueBytes = Encoding.ASCII.GetBytes(saltValue);

            // Convert our plaintext into a byte array. 
            // Let us assume that plaintext contains UTF8-encoded characters. 
            byte[] plainTextBytes = null;
            plainTextBytes = Encoding.UTF8.GetBytes(plainText);

            
            PasswordDeriveBytes password = default(PasswordDeriveBytes);
            password = new PasswordDeriveBytes(passPhrase, saltValueBytes, hashAlgorithm, passwordIterations);

            // Use the password to generate pseudo-random bytes for the encryption 
            // key. Specify the size of the key in bytes (instead of bits). 
            byte[] keyBytes = null;
            keyBytes = password.GetBytes(keySize / 8);

            // Create uninitialized Rijndael encryption object. 
            RijndaelManaged symmetricKey = default(RijndaelManaged);
            symmetricKey = new RijndaelManaged();

            // It is reasonable to set encryption mode to Cipher Block Chaining 
            // (CBC). Use default options for other symmetric key parameters. 
            symmetricKey.Mode = CipherMode.CBC;

            // Generate encryptor from the existing key bytes and initialization 
            // vector. Key size will be defined based on the number of the key 
            // bytes. 
            ICryptoTransform encryptor = default(ICryptoTransform);
            encryptor = symmetricKey.CreateEncryptor(keyBytes, initVectorBytes);

            // Define memory stream which will be used to hold encrypted data. 
            MemoryStream memoryStream = default(MemoryStream);
            memoryStream = new MemoryStream();

            // Define cryptographic stream (always use Write mode for encryption). 
            CryptoStream cryptoStream = default(CryptoStream);
            cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write);
            // Start encrypting. 
            cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length);

            // Finish encrypting. 
            cryptoStream.FlushFinalBlock();

            // Convert our encrypted data from a memory stream into a byte array. 
            byte[] cipherTextBytes = null;
            cipherTextBytes = memoryStream.ToArray();

            // Close both streams. 
            memoryStream.Close();
            cryptoStream.Close();

            // Convert encrypted data into a base64-encoded string. 
            string cipherText = null;
            cipherText = Convert.ToBase64String(cipherTextBytes);

            // Return encrypted string. 
            return cipherText;
        }

        
        private static string Decrypt(string cipherText, string passPhrase, string saltValue, string hashAlgorithm, int passwordIterations, string initVector, int keySize)
        {

            // Convert strings defining encryption key characteristics into byte 
            // arrays. Let us assume that strings only contain ASCII codes. 
            // If strings include Unicode characters, use Unicode, UTF7, or UTF8 
            // encoding. 
            byte[] initVectorBytes = null;
            initVectorBytes = Encoding.ASCII.GetBytes(initVector);

            byte[] saltValueBytes = null;
            saltValueBytes = Encoding.ASCII.GetBytes(saltValue);

            // Convert our ciphertext into a byte array. 
            byte[] cipherTextBytes = null;
            cipherTextBytes = Convert.FromBase64String(cipherText);

            // First, we must create a password, from which the key will be 
            // derived. This password will be generated from the specified 
            // passphrase and salt value. The password will be created using 
            // the specified hash algorithm. Password creation can be done in 
            // several iterations. 
            PasswordDeriveBytes password = default(PasswordDeriveBytes);
            password = new PasswordDeriveBytes(passPhrase, saltValueBytes, hashAlgorithm, passwordIterations);

            // Use the password to generate pseudo-random bytes for the encryption 
            // key. Specify the size of the key in bytes (instead of bits). 
            byte[] keyBytes = null;
            keyBytes = password.GetBytes(keySize / 8);

            // Create uninitialized Rijndael encryption object. 
            RijndaelManaged symmetricKey = default(RijndaelManaged);
            symmetricKey = new RijndaelManaged();

            // It is reasonable to set encryption mode to Cipher Block Chaining 
            // (CBC). Use default options for other symmetric key parameters. 
            symmetricKey.Mode = CipherMode.CBC;

            // Generate decryptor from the existing key bytes and initialization 
            // vector. Key size will be defined based on the number of the key 
            // bytes. 
            ICryptoTransform decryptor = default(ICryptoTransform);
            decryptor = symmetricKey.CreateDecryptor(keyBytes, initVectorBytes);

            // Define memory stream which will be used to hold encrypted data. 
            MemoryStream memoryStream = default(MemoryStream);
            memoryStream = new MemoryStream(cipherTextBytes);

            // Define memory stream which will be used to hold encrypted data. 
            CryptoStream cryptoStream = default(CryptoStream);
            cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read);

            // Since at this point we don't know what the size of decrypted data 
            // will be, allocate the buffer long enough to hold ciphertext; 
            // plaintext is never longer than ciphertext. 
            byte[] plainTextBytes = new byte[cipherTextBytes.Length];


            int decryptedByteCount = 0;
            decryptedByteCount = cryptoStream.Read(plainTextBytes, 0, plainTextBytes.Length);



            // Close both streams. 
            memoryStream.Close();
            cryptoStream.Close();

            // Convert decrypted data into a string. 
            // Let us assume that the original plaintext string was UTF8-encoded. 
            string plainText = null;
            plainText = Encoding.UTF8.GetString(plainTextBytes, 0, decryptedByteCount);

            // Return decrypted string. 
            return plainText;
        }

        public static string EncryptValue(string strString)
        {
            if (string.IsNullOrEmpty(strString.Trim())) return "";
            string strplainText = null;
            string strcipherText = null;

            string strpassPhrase = null;
            string strsaltValue = null;
            string strhashAlgorithm = null;
            int intpasswordIterations = 0;
            string strinitVector = null;
            int intkeySize = 0;

            strplainText = strString;
            //"Hello, World! - abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ 1234567890-=!@#


%^&*()_+[];,./<>?:'{}|\" ' original plaintext strpassPhrase = "Pas5pr@se"; // can be any string strsaltValue = "s@1tValue"; // can be any string strhashAlgorithm = "SHA1"; // can be "MD5" intpasswordIterations = 2; // can be any number strinitVector = "@1B2c3D4e5F6g7H8"; // must be 16 bytes intkeySize = 256; // can be 192 or 128 //Response.Write(String.Format("Plaintext : {0}", plainText)) //Response.Write("<br>") strcipherText = Encrypt(strplainText, strpassPhrase, strsaltValue, strhashAlgorithm, intpasswordIterations, strinitVector, intkeySize); //Response.Write(String.Format("Encrypted : {0}", cipherText)) //Response.Write("<br>") return strcipherText; } public static string DecryptValue(string strString) { if (string.IsNullOrEmpty(strString.Trim())) return ""; string strplainText = null; string strcipherText = null; string strpassPhrase = null; string strsaltValue = null; string strhashAlgorithm = null; int intpasswordIterations = 0; string strinitVector = null; int intkeySize = 0; strplainText = strString; //"Hello, World! - abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ 1234567890-=!@#


这篇关于我项目的加密/解密算法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆