مرجع تخصصی برنامه نویسان

انجمن تخصصی برنامه نویسان فارسی زبان

کاربر سایت

sg.programmer

عضویت از 1393/06/30

رمزنگاری پسورد

  • پنجشنبه 2 دی 1395
  • 12:07
تشکر میکنم

سلام

من با استفاده از این روش رمزنگاری را انجام دادم ولی در موقع رمزگشایی این خطا صادر میشه. این مشکل برای چی ایجاد شده؟ Invalid length for a Base-64 char array or string.

https://barnamenevisan.org/Articles/Article3159.html

پاسخ های این پرسش

تعداد پاسخ ها : 5 پاسخ
کاربر سایت

ایمان مدائنی

عضویت از 1392/01/20

  • پنجشنبه 2 دی 1395
  • 12:41

نباید تغییری در متد ها ایجاد کنید

اصل کد بر روی EncryptKey هست که نباید تغییری داشته باشه و برای DeCrypt نیز به همین کلید نیاز است

کاربر سایت

sg.programmer

عضویت از 1393/06/30

  • پنجشنبه 2 دی 1395
  • 14:06

تشکر آقای مدائنی از پاسختون

من هیچ تغییر ندادم و با asp.net میخوام کدینگ را انجام بدم.

وقتی کد میکنم در دیتابیس کرکترهای رمز شده را نشون میده ولی در Decryption شدن اون خطا صادر میشه

کاربر سایت

ایمان مدائنی

عضویت از 1392/01/20

  • پنجشنبه 2 دی 1395
  • 16:29

از کلاس زیر استفاده کنید

using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Web;

namespace Bashgah100
{
    public class MD5
    {
        public static string EncryptString(string Message)
        {
            byte[] Results;
            System.Text.UTF8Encoding UTF8 = new System.Text.UTF8Encoding();

            // Step 1. We hash the passphrase using MD5
            // We use the MD5 hash generator as the result is a 128 bit byte array
            // which is a valid length for the TripleDES encoder we use below

            MD5CryptoServiceProvider HashProvider = new MD5CryptoServiceProvider();
            byte[] TDESKey = HashProvider.ComputeHash(UTF8.GetBytes("123"));

            // Step 2. Create a new TripleDESCryptoServiceProvider object
            TripleDESCryptoServiceProvider TDESAlgorithm = new TripleDESCryptoServiceProvider();

            // Step 3. Setup the encoder
            TDESAlgorithm.Key = TDESKey;
            TDESAlgorithm.Mode = CipherMode.ECB;
            TDESAlgorithm.Padding = PaddingMode.PKCS7;

            // Step 4. Convert the input string to a byte[]
            byte[] DataToEncrypt = UTF8.GetBytes(Message);

            // Step 5. Attempt to encrypt the string
            try
            {
                ICryptoTransform Encryptor = TDESAlgorithm.CreateEncryptor();
                Results = Encryptor.TransformFinalBlock(DataToEncrypt, 0, DataToEncrypt.Length);
            }
            finally
            {
                // Clear the TripleDes and Hashprovider services of any sensitive information
                TDESAlgorithm.Clear();
                HashProvider.Clear();
            }

            // Step 6. Return the encrypted string as a base64 encoded string
            return Convert.ToBase64String(Results);
        }
        public static string DecryptString(string Message)
        {
            byte[] Results;
            System.Text.UTF8Encoding UTF8 = new System.Text.UTF8Encoding();

            // Step 1. We hash the passphrase using MD5
            // We use the MD5 hash generator as the result is a 128 bit byte array
            // which is a valid length for the TripleDES encoder we use below

            MD5CryptoServiceProvider HashProvider = new MD5CryptoServiceProvider();
            byte[] TDESKey = HashProvider.ComputeHash(UTF8.GetBytes("123"));

            // Step 2. Create a new TripleDESCryptoServiceProvider object
            TripleDESCryptoServiceProvider TDESAlgorithm = new TripleDESCryptoServiceProvider();

            // Step 3. Setup the decoder
            TDESAlgorithm.Key = TDESKey;
            TDESAlgorithm.Mode = CipherMode.ECB;
            TDESAlgorithm.Padding = PaddingMode.PKCS7;

            // Step 4. Convert the input string to a byte[]
            byte[] DataToDecrypt = Convert.FromBase64String(Message);

            // Step 5. Attempt to decrypt the string
            try
            {
                ICryptoTransform Decryptor = TDESAlgorithm.CreateDecryptor();
                Results = Decryptor.TransformFinalBlock(DataToDecrypt, 0, DataToDecrypt.Length);
            }
            finally
            {
                // Clear the TripleDes and Hashprovider services of any sensitive information
                TDESAlgorithm.Clear();
                HashProvider.Clear();
            }

            // Step 6. Return the decrypted string in UTF8 format
            return UTF8.GetString( Results );
        }

  
    }
    }

کاربر سایت

sg.programmer

عضویت از 1393/06/30

  • شنبه 4 دی 1395
  • 19:26

تشکر مهندس عالی بود

مهندس یک سوال دیگه فقط کدوم یک از این دو الگوریتم بهتر می باشد؟

الگوریتم یک

public string encrypt(string encryptString)   
{  
    string EncryptionKey = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";  
    byte[] clearBytes = Encoding.Unicode.GetBytes(encryptString);  
    using(Aes encryptor = Aes.Create())   
    {  
        Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(EncryptionKey, new byte[] {  
            0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76  
        });  
        encryptor.Key = pdb.GetBytes(32);  
        encryptor.IV = pdb.GetBytes(16);  
        using(MemoryStream ms = new MemoryStream())  
        {  
            using(CryptoStream cs = new CryptoStream(ms, encryptor.CreateEncryptor(), CryptoStreamMode.Write)) {  
                cs.Write(clearBytes, 0, clearBytes.Length);  
                cs.Close();  
            }  
            encryptString = Convert.ToBase64String(ms.ToArray());  
        }  
    }  
    return encryptString;  
}  
   
public string Decrypt(string cipherText)   
{  
    string EncryptionKey = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";  
    cipherText = cipherText.Replace(" ", "+");  
    byte[] cipherBytes = Convert.FromBase64String(cipherText);  
    using(Aes encryptor = Aes.Create())   
    {  
        Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(EncryptionKey, new byte[] {  
            0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76  
        });  
        encryptor.Key = pdb.GetBytes(32);  
        encryptor.IV = pdb.GetBytes(16);  
        using(MemoryStream ms = new MemoryStream())   
        {  
            using(CryptoStream cs = new CryptoStream(ms, encryptor.CreateDecryptor(), CryptoStreamMode.Write)) {  
                cs.Write(cipherBytes, 0, cipherBytes.Length);  
                cs.Close();  
            }  
            cipherText = Encoding.Unicode.GetString(ms.ToArray());  
        }  
    }  
    return cipherText;  
}

الگوریتم دوم

        public static string EncryptString(string Message)
        {
            byte[] Results;
            System.Text.UTF8Encoding UTF8 = new System.Text.UTF8Encoding();
 
            // Step 1. We hash the passphrase using MD5
            // We use the MD5 hash generator as the result is a 128 bit byte array
            // which is a valid length for the TripleDES encoder we use below
 
            MD5CryptoServiceProvider HashProvider = new MD5CryptoServiceProvider();
            byte[] TDESKey = HashProvider.ComputeHash(UTF8.GetBytes("123"));
 
            // Step 2. Create a new TripleDESCryptoServiceProvider object
            TripleDESCryptoServiceProvider TDESAlgorithm = new TripleDESCryptoServiceProvider();
 
            // Step 3. Setup the encoder
            TDESAlgorithm.Key = TDESKey;
            TDESAlgorithm.Mode = CipherMode.ECB;
            TDESAlgorithm.Padding = PaddingMode.PKCS7;
 
            // Step 4. Convert the input string to a byte[]
            byte[] DataToEncrypt = UTF8.GetBytes(Message);
 
            // Step 5. Attempt to encrypt the string
            try
            {
                ICryptoTransform Encryptor = TDESAlgorithm.CreateEncryptor();
                Results = Encryptor.TransformFinalBlock(DataToEncrypt, 0, DataToEncrypt.Length);
            }
            finally
            {
                // Clear the TripleDes and Hashprovider services of any sensitive information
                TDESAlgorithm.Clear();
                HashProvider.Clear();
            }
 
            // Step 6. Return the encrypted string as a base64 encoded string
            return Convert.ToBase64String(Results);
        }
        public static string DecryptString(string Message)
        {
            byte[] Results;
            System.Text.UTF8Encoding UTF8 = new System.Text.UTF8Encoding();
 
            // Step 1. We hash the passphrase using MD5
            // We use the MD5 hash generator as the result is a 128 bit byte array
            // which is a valid length for the TripleDES encoder we use below
 
            MD5CryptoServiceProvider HashProvider = new MD5CryptoServiceProvider();
            byte[] TDESKey = HashProvider.ComputeHash(UTF8.GetBytes("123"));
 
            // Step 2. Create a new TripleDESCryptoServiceProvider object
            TripleDESCryptoServiceProvider TDESAlgorithm = new TripleDESCryptoServiceProvider();
 
            // Step 3. Setup the decoder
            TDESAlgorithm.Key = TDESKey;
            TDESAlgorithm.Mode = CipherMode.ECB;
            TDESAlgorithm.Padding = PaddingMode.PKCS7;
 
            // Step 4. Convert the input string to a byte[]
            byte[] DataToDecrypt = Convert.FromBase64String(Message);
 
            // Step 5. Attempt to decrypt the string
            try
            {
                ICryptoTransform Decryptor = TDESAlgorithm.CreateDecryptor();
                Results = Decryptor.TransformFinalBlock(DataToDecrypt, 0, DataToDecrypt.Length);
            }
            finally
            {
                // Clear the TripleDes and Hashprovider services of any sensitive information
                TDESAlgorithm.Clear();
                HashProvider.Clear();
            }
 
            // Step 6. Return the decrypted string in UTF8 format
            return UTF8.GetString( Results );
        }

کاربر سایت

ایمان مدائنی

عضویت از 1392/01/20

  • یکشنبه 5 دی 1395
  • 11:01

هر دو تقریبا یکی هستند ولی اولی کلید قدرمتند تری داره

میتونید در دومی هم از اون کلید استفاده کنید

کاربرانی که از این پست تشکر کرده اند

هیچ کاربری تا کنون از این پست تشکر نکرده است

اگر نیاز به یک مشاور در زمینه طراحی سایت ، برنامه نویسی و بازاریابی الکترونیکی دارید

با ما تماس بگیرید تا در این مسیر همراهتان باشیم :)