android C# 간 AES 암호화 처리

Posted by Albert 2493Day 14Hour 17Min 34Sec ago [2017-06-28]

이번 출시된 앱 심사시 앱통신간 데이타중 중요정보 및 sqlite에 저장되는 데이타중

그대로 저장되면 않되는 부분들이 있어서 암호화 작업을 하게 되었다.

다행히 ios단은 아직 별도로 체크하지않아서 우선 안드로이드 단만하였는데 생각보다 쉬웟다.


안드로이드 단 암호화 부분 클래스


public class EncryptUtility {

public static final String KeyValue = "xxxkey";


public String Dec(String text) {
String result = text;
try {
result = Decrypt(text);
} catch (Exception e) {}
return result;
}

public String Enc(String text) {
String result = text;
try {
result = Encrypt(text);
} catch (Exception e) {}
return result;
}

public String Decrypt(String text) throws Exception
{

Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");

byte[] keyBytes= new byte[16];

byte[] b= KeyValue.getBytes("UTF-8");

int len= b.length;

if (len > keyBytes.length) len = keyBytes.length;

System.arraycopy(b, 0, keyBytes, 0, len);

SecretKeySpec keySpec = new SecretKeySpec(keyBytes, "AES");

IvParameterSpec ivSpec = new IvParameterSpec(keyBytes);

cipher.init(Cipher.DECRYPT_MODE,keySpec,ivSpec);

byte [] results = cipher.doFinal(Base64.decode(text, 0));

return new String(results ,"UTF-8");

}

public static String Encrypt(String text) throws Exception
{

Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");

byte[] keyBytes= new byte[16];

byte[] b= KeyValue.getBytes("UTF-8");

int len= b.length;

if (len > keyBytes.length) len = keyBytes.length;

System.arraycopy(b, 0, keyBytes, 0, len);

SecretKeySpec keySpec = new SecretKeySpec(keyBytes, "AES");

IvParameterSpec ivSpec = new IvParameterSpec(keyBytes);

cipher.init(Cipher.ENCRYPT_MODE,keySpec,ivSpec);

byte[] results = cipher.doFinal(text.getBytes("UTF-8"));

return Base64.encodeToString(results, 0);

}
}


닷넷단 클래스
public static class EncyptUtil
    {
        private const string PasswordKey = "xxxkey";
/// /// AES to Base64 암호화 /// /// /// /// public static string EncryptString(string text) { RijndaelManaged rijndaelCipher = new RijndaelManaged(); rijndaelCipher.Mode = CipherMode.CBC; rijndaelCipher.Padding = PaddingMode.PKCS7; rijndaelCipher.KeySize = 128; rijndaelCipher.BlockSize = 128; byte[] pwdBytes = Encoding.UTF8.GetBytes(PasswordKey); byte[] keyBytes = new byte[16]; int len = pwdBytes.Length; if (len > keyBytes.Length) { len = keyBytes.Length; } Array.Copy(pwdBytes, keyBytes, len); rijndaelCipher.Key = keyBytes; rijndaelCipher.IV = keyBytes; ICryptoTransform transform = rijndaelCipher.CreateEncryptor(); byte[] plainText = Encoding.UTF8.GetBytes(text); return Convert.ToBase64String(transform.TransformFinalBlock(plainText, 0, plainText.Length)); } /// /// AES to Base64 복호화 /// /// /// public static string DescryptString(string text) { RijndaelManaged rijndaelCipher = new RijndaelManaged(); rijndaelCipher.Mode = CipherMode.CBC; rijndaelCipher.Padding = PaddingMode.PKCS7; rijndaelCipher.KeySize = 128; rijndaelCipher.BlockSize = 128; byte[] encryptedData = Convert.FromBase64String(text); byte[] pwdBytes = Encoding.UTF8.GetBytes(PasswordKey); byte[] keyBytes = new byte[16]; int len = pwdBytes.Length; if (len > keyBytes.Length) { len = keyBytes.Length; } Array.Copy(pwdBytes, keyBytes, len); rijndaelCipher.Key = keyBytes; rijndaelCipher.IV = keyBytes; byte[] plainText = rijndaelCipher.CreateDecryptor().TransformFinalBlock(encryptedData, 0, encryptedData.Length); return Encoding.UTF8.GetString(plainText); } }

참고로 이미 ios, c샵, android 에 모두 적용되는 소스도 있엇다.
요즘 찾으면 웬만한건 다있어 개발하기 편하다. 예전처럼 책살필요도 없고 
오늘도 기분좋게 벌써 5시넘어가네 퇴근준비  하하하^^



LIST

Copyright © 2014 visionboy.me All Right Reserved.