C#에서 암호화 할시 php에서와 암호화 할시 다르게 나오는 경우가 있어

구글 검색 후 
참고 링크 입니다.

아래 코드 같은 경우에는 흔히 구글에서 볼수 있지만 이 C# 코드와 동일하게 암호화 하기 위해서는 위의 링크에 들어 가서 
php암호화 코드를 사용 하여야만 한다..
사용 결과 잘 됩니다.~

private string Decrypt256(String Input, String key)

{

 RijndaelManaged aes = new RijndaelManaged();

            aes.KeySize = 256;

            aes.BlockSize = 128;

            aes.Mode = CipherMode.CBC;

            aes.Padding = PaddingMode.PKCS7;

            aes.Key = Encoding.UTF8.GetBytes(key);

            aes.IV = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };


            var decrypt = aes.CreateDecryptor();

            byte[] xBuff = null;

            using (var ms = new MemoryStream())

            {

                using (var cs = new CryptoStream(ms, decrypt, CryptoStreamMode.Write))

                {

                    byte[] xXml = Convert.FromBase64String(Input);

                    cs.Write(xXml, 0, xXml.Length);

                }


                xBuff = ms.ToArray();

            }


            String Output = Encoding.UTF8.GetString(xBuff);

            return Output;

}

 

  private String AESEncrypt256(String Input, String key)

{

            RijndaelManaged aes = new RijndaelManaged();

            aes.KeySize = 256;

            aes.BlockSize = 128;

            aes.Mode = CipherMode.CBC;

            aes.Padding = PaddingMode.PKCS7;

            aes.Key = Encoding.UTF8.GetBytes(key);        

            aes.IV = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };


            var encrypt = aes.CreateEncryptor(aes.Key, aes.IV);

            byte[] xBuff = null;

            using (var ms = new MemoryStream())

            {

                using (var cs = new CryptoStream(ms, encrypt, CryptoStreamMode.Write))

                {

                    byte[] xXml = Encoding.UTF8.GetBytes(Input);

                    cs.Write(xXml, 0, xXml.Length);

                }


                xBuff = ms.ToArray();

            }


            String Output = Convert.ToBase64String(xBuff);

            return Output;

}


아래의 extable 은 각 고유 키를 넣어 주면 됩니다. ^^



byte[] extable = {0x1D, 0x1D, 0x1B, 0x3F} ;

byte[] _changeBuf = {0,0,0,0} ;


void Start() {

Debug.Log(intToXOR(1000));

int kTemp = intToXOR(1000);

Debug.Log(xorToINT(kTemp));

Debug.Log(intToXOR(10000));

kTemp = intToXOR(10000);

Debug.Log(xorToINT(kTemp));

}

private void IntToByte(int num, byte[] value)

{

    value[0] = (byte)((num >> 24) ^ extable[0]);

    value[1] = (byte)((num >> 16) ^ extable[1]);

    value[2] = (byte)((num >> 8) ^ extable[2]);

    value[3] = (byte)(num ^ extable[3]);

}

private int ByteToIntNon(byte[] value)

{

    int retV = 0;

    

    retV = (((value[0] & 0xFF)) << 24) |

    (((value[1] & 0xFF)) << 16) |

    (((value[2] & 0xFF)) << 8) |

    ((value[3] & 0xFF));

    

    return retV;

}

private void IntToByteNon(int num, byte[] value)

{

   value[0] = ((byte)(num >> 24));

    value[1] = ((byte)(num >> 16));

    value[2] = ((byte)(num >> 8));

    value[3] = ((byte)(num));

}

private int ByteToInt(byte[] value)

{

    int retV = 0;

    

    retV = (((value[0] & 0xFF) ^ extable[0]) << 24) |

    (((value[1] & 0xFF) ^ extable[1]) << 16) |

    (((value[2] & 0xFF) ^ extable[2]) << 8) |

    ((value[3] & 0xFF) ^ extable[3]);

    

    return retV;

}

public int intToXOR(int _value) {

IntToByte(_value,_changeBuf);

return ByteToIntNon(_changeBuf);

}

public int xorToINT(int _value) {

    IntToByteNon(_value,_changeBuf);

return ByteToInt(_changeBuf);

}

+ Recent posts