AES256 암호화, 복호화

유니티 C# 2018. 11. 7. 11:12

암호화

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
private AES256Encrypt(string _Data)
{
    byte[] plainBytes = System.Text.Encoding.UTF8.GetBytes(_Data);
    System.Security.Cryptography.RijndaelManaged rm = new System.Security.Cryptography.RijndaelManaged
    {
    Mode = System.Security.Cryptography.CipherMode.CBC,
    Padding = System.Security.Cryptography.PaddingMode.PKCS7,
    KeySize = 256
    };
 
    MemoryStream memoryStream = new MemoryStream();
    System.Security.Cryptography.ICryptoTransform encryptor = rm.CreateEncryptor(
    System.Text.Encoding.UTF8.GetBytes("01234567890123456789012345678901".Substring(0, 256 / 8)),
    System.Text.Encoding.UTF8.GetBytes("01234567890123456789012345678901".Substring(0, 128 / 8)));
    System.Security.Cryptography.CryptoStream cryptoStream = new System.Security.Cryptography.CryptoStream(memoryStream, encryptor, System.Security.Cryptography.CryptoStreamMode.Write);
    cryptoStream.Write(plainBytes, 0, plainBytes.Length);
    cryptoStream.FlushFinalBlock();
    byte[] encryptBytes = memoryStream.ToArray();
    string encryptString = System.Convert.ToBase64String(encryptBytes);
    cryptoStream.Close();
    memoryStream.Close();
    return encryptString;
}
cs

 

복호화

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
private string AES256Decrypt()
{
    byte[] encryptBytes = System.Convert.FromBase64String(o);
    System.Security.Cryptography.RijndaelManaged rm = new System.Security.Cryptography.RijndaelManaged
    {
        Mode = System.Security.Cryptography.CipherMode.CBC,
        Padding = System.Security.Cryptography.PaddingMode.PKCS7,
        KeySize = 256
    };
 
    MemoryStream memoryStream = new MemoryStream(encryptBytes);
    System.Security.Cryptography.ICryptoTransform decryptor = rm.CreateDecryptor(
    System.Text.Encoding.UTF8.GetBytes("01234567890123456789012345678901".Substring(0, 256 / 8)),
    System.Text.Encoding.UTF8.GetBytes("01234567890123456789012345678901".Substring(0, 128 / 8)));
    System.Security.Cryptography.CryptoStream cryptoStream = new System.Security.Cryptography.CryptoStream(memoryStream, decryptor, System.Security.Cryptography.CryptoStreamMode.Read);
    byte[] plainBytes = new byte[encryptBytes.Length];
    int plainCount = cryptoStream.Read(plainBytes, 0, plainBytes.Length);
    string plainString = System.Text.Encoding.UTF8.GetString(plainBytes, 0, plainCount);
    cryptoStream.Close();
    memoryStream.Close();
    return plainString;
}
cs

 

클래스 리스트를 클래스 안에있는 정보로 정렬하는법

유니티 C# 2018. 10. 26. 11:15

List<TestClass> Test = new List<TestClass>();


Test.Sort(delegate(TestClass t1, TestClass t2) { return t1.date.CompareTo(t2.date); });


유니티에서 파이어베이스 아이콘 설정

기타 팁 2018. 10. 19. 12:38

아이콘을 Assets/Plugins/Android/firebase/res/drawable 폴더에 넣는다.


Assets/Plugins/Android/AndroidManifest.xml을 수정한다.


1
2
3
4
5
6
7
8
<manifest>
  <application ...>
    <meta-data android:name="com.google.firebase.messaging.default_notification_icon"
               android:resource="@drawable/my_sweet_icon" />
    <activity ... >
    </activity>
  </application>
<manifest>
cs


2D상에서의 각도를 기반으로 회전값 구하기

유니티 C# 2018. 1. 29. 16:40

void Start()


{


// z축 +180은 이미지 방향에 따라 수정하여 적용.


// target1은 자신의 객체가 아닌 비교할 해당 객체.


transform.eulerAngles = new Vector3(0, 0, -getAngle(transform.position.x, transform.position.y, target1.position.x, target1.position.y) + 180.0f);


}




private float getAngle(float x1, float y1, float x2, float y2)


{


        float dx = x2 - x1;


        float dy = y2 - y1;


        


        float rad = Mathf.Atan2(dx, dy);


        float degree = rad * Mathf.Rad2Deg;




        return degree;



출처: http://redccoma.tistory.com/110

유니티에서 비동기적으로 scene을 불러오며 progress를 얻는 방법

유니티 C# 2018. 1. 3. 16:06

using UnityEngine;

using System.Collections;


public delegate void ProgressDelegate(float progress);


public class MyScript {


public void OnStartMyScene() {

StartCoroutine(LoadSceneAsyncByName("MySceneName", OnLoadLevelProgressUpdate));

}

public static IEnumerator LoadSceneAsyncByName(string nextLevel, ProgressDelegate progressDelegate) {

AsyncOperation async = UnityEngine.SceneManagement.SceneManager.LoadSceneAsync(nextLevel);

while (!async.isDone) {

progressDelegate (async.progress);

async.allowSceneActivation = async.progress > 0.8;

yield return null;

}

}


private void OnLoadLevelProgressUpdate(float progress) {

Debug.Log ("async.progress: " + progress);

}

}





출처: http://crowjdh.blogspot.kr/2016/07/scene-progress-c.html