2012-03-07 3 views
0

C#에서 PHP를 사용하여 암호화 된 문자열을 해독 할 수 있는지 누구나 알고 있습니까? PHP에서 암호화하기 위해 사용하는 코드는 다음과 같습니다.C#에서 PHP로 암호화 된 문자열의 암호를 해독 하시겠습니까?

$string = "Hello. This is a test string."; 

$key = "testPassword"; 
$encrypted = base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, md5($key), $string, MCRYPT_MODE_CBC, md5(md5($key)))); 

가능하면 C#을 사용하여 암호를 해독하면됩니다. 도와 주셔서 감사합니다!

+0

중복 가능성 [Rijndael을 256 암호화/C# 및 PHP 사이 해독? (HTTP//stackoverflow.com/questions/3431950/rijndael-256-encrypt-decrypt-betwee n-c-sharp-and-php) – Shai

+0

md5는 암호화 할 수 있습니까? – Ismael

+0

아이들이 이런 모든 암호화/해시 메소드를 중첩하는 것을 멈추게 할 수 있습니다. 일반적인 사이트 암호를 사용하여 암호를 암호화하기 위해'mcrypt'와 같은 것을 사용하십시오 (높은 엔트로피, 좋은 보안을 원한다면 복잡합니다). 키의 md5는 처음부터 좋은 키만큼 안전하지 못합니다. – deed02392

답변

1

복호화 부분은 here이라고 대답합니다.

C 번호

public static string EncryptString(string message, string KeyString, string IVString) 
    { 
     byte[] Key = ASCIIEncoding.UTF8.GetBytes(KeyString); 
     byte[] IV = ASCIIEncoding.UTF8.GetBytes(IVString); 

     string encrypted = null; 
     RijndaelManaged rj = new RijndaelManaged(); 
     rj.Key = Key; 
     rj.IV = IV; 
     rj.Mode = CipherMode.CBC; 

     try 
     { 
      MemoryStream ms = new MemoryStream(); 

      using (CryptoStream cs = new CryptoStream(ms, rj.CreateEncryptor(Key, IV), CryptoStreamMode.Write)) 
      { 
       using (StreamWriter sw = new StreamWriter(cs)) 
       { 
        sw.Write(message); 
        sw.Close(); 
       } 
       cs.Close(); 
      } 
      byte[] encoded = ms.ToArray(); 
      encrypted = Convert.ToBase64String(encoded); 

      ms.Close(); 
     } 
     catch (CryptographicException e) 
     { 
      Console.WriteLine("A Cryptographic error occurred: {0}", e.Message); 
      return null; 
     } 
     catch (UnauthorizedAccessException e) 
     { 
      Console.WriteLine("A file error occurred: {0}", e.Message); 
      return null; 
     } 
     catch (Exception e) 
     { 
      Console.WriteLine("An error occurred: {0}", e.Message); 
     } 
     finally 
     { 
      rj.Clear(); 
     } 

     return encrypted; 
    } 

디코딩베이스 64 :의

/// <summary> 
/// The method create a Base64 encoded string from a normal string. 
/// </summary> 
/// <param name="toEncode">The String containing the characters to encode.</param> 
/// <returns>The Base64 encoded string.</returns> 
public static string EncodeTo64(string toEncode) 
{ 

    byte[] toEncodeAsBytes 

      = System.Text.Encoding.Unicode.GetBytes(toEncode); 

    string returnValue 

      = System.Convert.ToBase64String(toEncodeAsBytes); 

    return returnValue; 

} 




/// <summary> 
/// The method to Decode your Base64 strings. 
/// </summary> 
/// <param name="encodedData">The String containing the characters to decode.</param> 
/// <returns>A String containing the results of decoding the specified sequence of bytes.</returns> 
public static string DecodeFrom64(string encodedData) 
{ 

    byte[] encodedDataAsBytes 

     = System.Convert.FromBase64String(encodedData); 

    string returnValue = 

     System.Text.Encoding.Unicode.GetString(encodedDataAsBytes); 

    return returnValue; 

} 
+0

답안에 일부 코드 - 특히 stackoverflow에없는 코드 -를 포함하십시오. 링크가 앞으로도 계속 활성화되는지는 아무도 알 수 없습니다. – ThiefMaster

관련 문제