3
Android 앱에서 문자열을 암호화하고 ASP.Net 서버에서 해독하려고합니다. 오류는 발생하지 않지만 암호 해독은 실제 결과를 반환하지 않습니다.AES 암호 해독이 잘못된 결과를 나타냅니다.
public void clckBtn(View v) {
try {
SecretKeySpec skeySpec = new SecretKeySpec(
"MyDifficultPassw".getBytes(), "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
byte[] encrypted = cipher.doFinal("tryToEncrypt".getBytes());
System.out.println(toHex(encrypted));
} catch (Exception e) {
System.out.println(e.toString());
}
}
public static String toHex(byte[] buf) {
if (buf == null)
return "";
StringBuffer result = new StringBuffer(2 * buf.length);
for (int i = 0; i < buf.length; i++) {
appendHex(result, buf[i]);
}
return result.toString();
}
private final static String HEX = "ABCDEF";
private static void appendHex(StringBuffer sb, byte b) {
sb.append(HEX.charAt((b >> 4) & 0x0f)).append(HEX.charAt(b & 0x0f));
}
출력은 : CE3E99F50E6D30201E38D4955F07BA7C
Asp.Net 측 :
Asp.Net AES 클래스protected void Page_Load(object sender, EventArgs e)
{
using (Aes myAes = Aes.Create())
{
string asd = DecryptStringFromBytes_Aes(GetBytes("CE3E99F50E6D30201E38D4955F07BA7C"), GetBytes("MyDifficultPassw"), myAes.IV);
int we = 0;
}
}
static string DecryptStringFromBytes_Aes(byte[] cipherText, byte[] Key
, byte[] IV)
{
// Check arguments.
if (cipherText == null || cipherText.Length <= 0)
throw new ArgumentNullException("cipherText");
if (Key == null || Key.Length <= 0)
throw new ArgumentNullException("Key");
if (IV == null || IV.Length <= 0)
throw new ArgumentNullException("Key");
// Declare the string used to hold
// the decrypted text.
string plaintext = null;
// Create an Aes object
// with the specified key and IV.
using (Aes aesAlg = Aes.Create())
{
aesAlg.Key = Key;
aesAlg.IV = IV;
aesAlg.Padding = PaddingMode.None;
// Create a decrytor to perform the stream transform.
ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key
, aesAlg.IV);
// Create the streams used for decryption.
using (MemoryStream msDecrypt = new MemoryStream(cipherText))
{
using (CryptoStream csDecrypt = new CryptoStream(msDecrypt
, decryptor, CryptoStreamMode.Read))
{
using (StreamReader srDecrypt = new StreamReader(
csDecrypt))
{
// Read the decrypted bytes from the decrypting
// and place them in a string.
plaintext = srDecrypt.ReadToEnd();
}
}
}
}
return plaintext;
}
static byte[] GetBytes(string str)
{
byte[] bytes = new byte[str.Length * sizeof(char)];
System.Buffer.BlockCopy(str.ToCharArray(), 0, bytes, 0, bytes.Length);
return bytes;
}
는 IV (초기화 벡터)를 필요로 여기 안드로이드 측이다. 안드로이드에는 그런 것이 없다. 나는 그 문제가 그것에 관한 것이라고 생각한다.
기본적으로 키, IV, 패딩, 모드와 일치하는지 확인해야합니다. 나는 안드로이드를 프로그래밍하지 않았지만 구글에 대한 빠른 검색은 나를 [this] (http://stackoverflow.com/a/16854800/706456)로 지적했다. 어쩌면 거기에서 해결책을 얻을 수 있습니다. – oleksii
Java 코드에서 ECB 모드를 사용하고 .NET 코드에서 CBC 모드를 사용하고 있습니다. 또한, 나는이 코드가 실제로 당신이 무엇을했는지 성취하지 않을 것이라고 생각한다. ([관련 질문] (http://security.stackexchange.com/questions/52584/why-can-we-still-crack-snapchat-photos- 루비의 12 행)). 아마도 [TLS] (http://en.wikipedia.org/wiki/Transport_Layer_Security)가 더 적합할까요? – ntoskrnl