자바와 C++ 암호화 복호화 해보신분

Lightstar의 이미지

자바(안드로이드)에서 데이터를 암호화해서 c++로 보내주고 c++에서 복호화하려고합니다.

자바쪽 코드입니다.
public static void main(String[] args) {
// TODO Auto-generated method stub
encryp ec=new encryp();
String name="abcd1234";
System.out.println(name);
try {
System.out.println(ec.encrypt(name));
System.out.println(ec.decrypt(ec.encrypt(name)));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}

class encryp {
static byte []key=new byte[]{0x00, 0x11, 0x22, 0x00, 0x11, 0x22, 0x00, 0x11, 0x22, 0x00, 0x11, 0x22, 0x00, 0x11, 0x22, 0x00};
public static String encrypt(String message) throws Exception {
SecretKeySpec skeySpec = new SecretKeySpec(key, "AES");
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
byte[] encrypted = cipher.doFinal(message.getBytes());
return byteArrayToHex(encrypted);
}

c++입니다
extern "C"{
#include "libcrypt\aes.h"
} //*/
#include
#include
#include
#include
using namespace std;
void main(){

aes_context ctx;
WCHAR *in=L"abcd1234";
WCHAR *out;
// WCHAR *secret_key=L"abcdefgh12345678";
BYTE secret_key[]={0x00, 0x11, 0x22, 0x00, 0x11, 0x22, 0x00, 0x11, 0x22, 0x00, 0x11, 0x22, 0x00, 0x11, 0x22, 0x00};
BYTE * buf=(BYTE*)malloc(18);
BYTE * keybuff=(BYTE*)malloc(16);

memset(buf,0, 18);
memset(keybuff,0,16);

memcpy( buf, in, 15);
memcpy( keybuff, secret_key, 16);
aes_set_key( &ctx, keybuff, 128);

aes_encrypt( &ctx, buf, buf );
out=(WCHAR*)buf;
for (int i = 0 ; i < 16 ; ++i)
std::cout << std::hex << static_cast(buf[i]);
std::cout << std::endl;
aes_decrypt( &ctx, buf, buf );

out=(WCHAR*)buf;
MessageBox(NULL, (WCHAR*)out, NULL, 0); //*/

system("pause");
}

각각 암복호화는 잘 되는데 서로 같은 값을 암호화했을 때 값이 다르게 나옵니다.
모드라던지 패딩같은 것들을 맞춰줘야할 것 같은데 c++에서 위 libcrypt(crypto++?) 라이브러리를 썼을 때 어떤 식으로 매칭시켜줘야하나요?

pokev25의 이미지

a287848의 이미지

간단한 방법으로는

OpenSSL 기반으로 SSL 기반 Client/Server Socket Program을 짜서 테스트 한 후에

Java 족에 OpenSSL을 JNI로 Import 했던 기억이 있네요.

Dig it.

winner의 이미지

Android 는 해본 적 없지만 검색해보니 default encoding 이 UTF-8 이라고 나오네요.
C++ 는 Windows 에서 한 것 같은데 WCHAR 을 쓴 것을 보면 UTF-16 인가요?
용량처리도 잘못하신 것 같고... aes_encrypt 와 aes_decrypt 를 어떻게 하셨는지 모르겠습니다만 padding 처리도...

elsoft의 이미지

값은 다르게 나오나 자바에서 암호화 한것을 c++ 복호화 하면 같은 문자가 나오고
c++에서 암호화 한것을 반대로 자바에서 복호화 하여도 같은 결과가 나타납니다.

elsoft의 이미지

값은 다르게 나오나 자바에서 암호화 한것을 c++ 복호화 하면 같은 문자가 나오고
c++에서 암호화 한것을 반대로 자바에서 복호화 하여도 같은 결과가 나타납니다.

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.