티스토리 뷰

코니79 IT 정보

VB6 및 Java에서 AES 암호화

코니79 코니와함께 2018.03.13 17:46

VB CODE

strProvider = "Microsoft Enhanced RSA and AES Cryptographic Provider (Prototype)" &     vbNullChar  

lngHashType = CALG_MD5  

lngAlgoType = CALG_AES_128 

strPassword = "secretPassphrase"

If CBool(CryptAcquireContext(g_lngCryptoContext, ByVal strTemp, _

         ByVal strProvider, PROV_RSA_AES, CRYPT_VERIFYCONTEXT)) Then           

    GetProvider = True

else 

    GoTo CleanUp

End If

If Not CBool(CryptCreateHash(g_lngCryptoContext, lngHashType, ByVal 0&, _ByVal 0&, lngHashHnd)) Then

    MsgBox "Error: " & CStr(GetLastError) & " during CryptCreateHash!", _vbExclamation Or vbOKOnly, "Encryption Errors"

    GoTo CleanUp

End If

If Not CBool(CryptHashData(lngHashHnd, strPassword, Len(strPassword), ByVal 0&)) Then

    MsgBox "Error: " & CStr(GetLastError) & " during CryptHashData!", _

           vbExclamation Or vbOKOnly, "Encryption Errors"

    GoTo CleanUp

End If

If Not CBool(CryptDeriveKey(g_lngCryptoContext, lngAlgoType, _lngHashHnd, ByVal CRYPT_NO_SALT, lngkey)) Then

    MsgBox "Error: " & CStr(GetLastError) & " during CryptDeriveKey!", _

           vbExclamation Or vbOKOnly, "Encryption Errors"

    GoTo CleanUp

End If

If lngHashHnd <> 0 Then

    lngRetCode = CryptDestroyHash(lngHashHnd)

End If

lngHashHnd = 0

lngEncDataLength = Len(g_strInData)         

lngEnctBuffLen = lngEncDataLength * 2

strEncBuffer = String$(lngEnctBuffLen, vbNullChar)

LSet strEncBuffer = g_strInData

If Not CBool(CryptEncrypt(lngkey, ByVal 0&, ByVal 1&, ByVal 0&, _strEncBuffer, lngEncDataLength, lngEnctBuffLen)) Then

    MsgBox "Bytes required:" & CStr(lngEnctBuffLen) & vbCrLf & vbCrLf & _

           "Error: " & CStr(GetLastError) & " during CryptEncrypt!", _

           vbExclamation Or vbOKOnly, "Encryption Errors"

    GoTo CleanUp

End If

strOutData = Mid$(strEncBuffer, 1, lngEncDataLength)

g_abytOutData = StringToByteArray(strOutData)

여기까가 VB CODE 샘플 코드 입니다.생각보다 간단하시죠..그럼 더 간단한 JAVA CODE 한번 알아볼게요.




JAVA CODE 

String password = "secretPassphrase";  

String text = "text to encrypt";  

MessageDigest md = MessageDigest.getInstance("MD5");  

md.update(password.getBytes("UTF-8"), 0, password.length());  

byte[] rawKey = md.digest();  

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

IvParameterSpec ivSpec = new IvParameterSpec(iv);  

SecretKeySpec skeySpec = new SecretKeySpec(rawKey, "AES");  

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

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

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

System.out.println(toHex(encrypted));



댓글
댓글쓰기 폼
광고위치
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
60,985
Today
50
Yesterday
177
링크
«   2018/08   »
      1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31  
글 보관함