News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Encrypt/decrypt text

Started by skywalker, March 29, 2006, 02:23:55 PM

Previous topic - Next topic

skywalker

I was looking to encrypt and decrypt some text, but everything I have is 16 bit.
It doesn't have to be fancy, xor would be fine.

On a separate question. If I want to put in some extended ascii characters in a string, do I have to save
my code as Unicode to see it?

Thanks.

"If you can't be a good example, then you'll just have to serve as a horrible warning." -Catherine Aird

evlncrn8

it really is time you started using the search button and/or google, and i think i got an idea what you're working on..

skywalker

Quote from: evlncrn8 on March 29, 2006, 03:01:25 PM
it really is time you started using the search button and/or google, and i think i got an idea what you're working on..

I have been doing that for quite some time.

And, no you don't know what I'm working on.


Tedd

He's... he's.... HE'S PLANNING TO TAKE OVE THE WORLD!!!! :dazzled:
No snowflake in an avalanche feels responsible.

P1

Quote from: skywalker on March 29, 2006, 02:23:55 PMIt doesn't have to be fancy, xor would be fine.
You are missing out on a lot because you are most of the time trying to modify other people's work.  Do a project for yourself, code it from scratch and you will appreciate the code you have a whole lot more, when you do. 

If a Xor encryption is all you need, then DIY.  Most beginning programmers are required to do one as a first time assignment.  Doing a project like this will help you to learn and understand the process from start to finnish.

Regards,  P1  :8)

IAO

Hi to all:

Mr. skywalker:
Please.
Look in: \masm32\example.
I think that I saw one.

Mr. P1:
You are a very hard moderator. That is very good. :U
But, you scare to me. :eek
Bye('_').
"There is no way to peace. Peace is the way."    Mahatma Gandhi

jckl

Harsh or not he is right tho.. I am teaching myself and ill spend anywhere from 1 to 4 hours on something before i ask for help here.. I have a couple projects i got working and i am happy with each one of them :)

skywalker

Quote from: P1 on March 29, 2006, 06:59:08 PM
Quote from: skywalker on March 29, 2006, 02:23:55 PMIt doesn't have to be fancy, xor would be fine.
You are missing out on a lot because you are most of the time trying to modify other people's work.  Do a project for yourself, code it from scratch and you will appreciate the code you have a whole lot more, when you do. 

If a Xor encryption is all you need, then DIY.  Most beginning programmers are required to do one as a first time assignment.  Doing a project like this will help you to learn and understand the process from start to finnish.

Regards,  P1  :8)

I have done plenty of code by myself. The real issue is your failure to admit your bad attitude.

All you're doing is alienating yourself.




skywalker

Quote from: IAO on March 29, 2006, 07:45:14 PM
Hi to all:

Mr. skywalker:
Please.
Look in: \masm32\example.
I think that I saw one.

Mr. P1:
You are a very hard moderator. That is very good. :U
But, you scare to me. :eek
Bye('_').

Thanks IAO. There isn't an example of it. But thanks for not being afraid to speak the truth.

Have a good week.


redskull

Just for the record, P1:  You kick some major ass :thumbu
Strange women, lying in ponds, distributing swords, is no basis for a system of government

donkey

Hi skywalker,

Here's one I wrote a long long time ago to store a password in the registry, I would not use it for that now, I use mostly TEA or BLOWFISH (or the Crypt API) these days but it's pretty much what you're looking for...


DATA SECTION
; This is a very simple pseudo-encrypted block, it is not meant to
; be secure in any way and is very easy to decrypt by anyone at all.
; It says "SOFTWARE\Microsoft\Windows\CurrentVersion",0,"ProductId"
; It is used in GetKey to generate an encryption key for passwords
; but I didn't want to just leave it in ansi so everyone could see.
; It requires Key# 152715150 to decrypt it
cryptdata: DB 05Ah,04Fh,0C4h,0D8h,052h,053h,0ECh,0FAh,044h,04Bh
DB 09Ah,0B6h,018h,00Fh,0AEh,0AEh,030h,039h,0F0h,0DEh
DB 02Eh,00Dh,080h,0AEh,012h,037h,0F0h,0F6h,016h,035h
DB 0ACh,0BAh,020h,039h,0E4h,0BAh,018h,037h,09Ah,0AEh
DB 020h,0D1h,0E8h,094h,022h,019h,0A2h,0B6h,014h,043h
DB 080h,070h

CODE SECTION
GetKey FRAME
LOCAL KSRegKey[256] :B
LOCAL KeyString[64] :B
LOCAL hRegKey :D
LOCAL Disposition :D
LOCAL uDataCode :D
LOCAL cbRead :D

invoke ZeroMem,OFFSET KSRegKey,256
invoke DecryptString,OFFSET cryptdata,152715150,OFFSET KSRegKey,13

invoke RegCreateKeyEx,HKEY_LOCAL_MACHINE,OFFSET KSRegKey,NULL,NULL,\
REG_OPTION_NON_VOLATILE,KEY_READ,NULL,ADDR hRegKey,ADDR Disposition

or eax,eax
jz >
xor eax,eax
dec eax
ret
:

mov D[cbRead],64
invoke RegQueryValueEx,[hRegKey],OFFSET KSRegKey+42,NULL,ADDR uDataCode,\
OFFSET KeyString,ADDR cbRead

invoke RegCloseKey,[hRegKey]
invoke GenKey,OFFSET KeyString
xor eax,eax
RET

ENDF

GenKey FRAME lpKeyString
uses edi,esi
invoke lstrlen,[lpKeyString]
mov edi,0

mov ecx,eax
mov esi,[lpKeyString]

:
push ecx
dec ecx
mov eax,[esi+ecx]
add edi,eax
pop ecx
dec ecx
or ecx,ecx
jnz <

clc
mov [dwCryptKey],edi
ret

ENDF

EncryptString FRAME lpDataString, CryptKey, lpOutString, cbdata
uses edi,esi
mov ecx,[cbdata]
mov edi,[lpOutString]
mov esi,[lpDataString]

:
push ecx
dec ecx
mov eax,[esi+ecx*4]
rol eax,6
xor eax,[CryptKey]
ror eax,5
mov [edi+ecx*4],eax
pop ecx
dec ecx
or ecx,ecx
jnz <

ret
ENDF

DecryptString FRAME  lpDataString, CryptKey, lpOutString, cbdata
uses edi,esi
mov ecx,[cbdata]
mov edi,[lpOutString]
mov esi,[lpDataString]

:
push ecx
dec ecx
mov eax,[esi+ecx*4]
rol eax,5
xor eax,[CryptKey]
ror eax,6
mov [edi+ecx*4],eax
pop ecx
dec ecx
or ecx,ecx
jnz <
ret

ENDF


QuoteThe real issue is your failure to admit your bad attitude.

By the way, P1 is completely right and fair in this and everywhere else I have ever encountered him. You could have easily written something like this yourself or should have been able to by now. Even though I wrote this years ago, I think it only took me about 15 minutes and that was in MASM which is much more cumbersome to use than GoAsm.

And PS, I have absolutely no interest in explaining how it works, it is far too obvious for that.
"Ahhh, what an awful dream. Ones and zeroes everywhere...[shudder] and I thought I saw a two." -- Bender
"It was just a dream, Bender. There's no such thing as two". -- Fry
-- Futurama

Donkey's Stable

hutch--

Andy,

Tread carefully here.

Quote
I have done plenty of code by myself. The real issue is your failure to admit your bad attitude.

All you're doing is alienating yourself.

Enough people are tired of your lack of willingness to do your own work when much of it is very simple stuff that you should know how to do. Giving lip to one of the moderators is not a winner here as our moderators do a lot of work to help other people who are willing to do their own work.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

skywalker

Quote from: donkey on March 30, 2006, 03:28:26 AM
Hi skywalker,

Here's one I wrote a long long time ago to store a password in the registry, I would not use it for that now, I use mostly TEA or BLOWFISH (or the Crypt API) these days but it's pretty much what you're looking for...


This is what I have so far.
There isn't a getkey proc in here and it's saying that I'm redefining GenKey again. Maybe one of those
is the GetKey ?

It's having a problem with ZeroMemory too.

Thanks.

;FRAME = PROC
;ENDF = ENDP you must insert the proc name before ENDP
;: = @@:
;> or >> = @F
;< or << = @B
   

.code

start:

GenKey PROC
LOCAL KSRegKey[256] :BYTE
LOCAL KeyString[64] :BYTE
LOCAL hRegKey :DWORD
LOCAL Disposition :DWORD
LOCAL uDataCode :DWORD
LOCAL cbRead :DWORD

invoke ZeroMemory,OFFSET KSRegKey,256 ; Was orig. ZeroMem
invoke DecryptString,OFFSET cryptdata,152715150,OFFSET KSRegKey,13

invoke RegCreateKeyEx,HKEY_LOCAL_MACHINE,OFFSET KSRegKey,NULL,NULL,\
REG_OPTION_NON_VOLATILE,KEY_READ,NULL,ADDR hRegKey,ADDR Disposition

or eax,eax
jz @F
xor eax,eax
dec eax
ret

mov D[cbRead],64
invoke RegQueryValueEx,[hRegKey],OFFSET KSRegKey+42,NULL,ADDR uDataCode,\
OFFSET KeyString,ADDR cbRead

invoke RegCloseKey,[hRegKey]
invoke GenKey,OFFSET KeyString
xor eax,eax
RET

GenKey ENDP


GenKey PROC  uses EDI ESI                     ; lpKeyString
                                              ; uses edi,esi
LOCAL lpKeyString:DWORD

invoke lstrlen,[lpKeyString]
mov edi,0

mov ecx,eax
mov esi,[lpKeyString]

push ecx
dec ecx
mov eax,[esi+ecx]
add edi,eax
pop ecx
dec ecx
or ecx,ecx
jnz @B

clc
mov [dwCryptKey],edi
ret

ENDP

EncryptString PROC lpDataString, CryptKey, lpOutString, cbdata
uses edi,esi
mov ecx,[cbdata]
mov edi,[lpOutString]
mov esi,[lpDataString]

push ecx
dec ecx
mov eax,[esi+ecx*4]
rol eax,6
xor eax,[CryptKey]
ror eax,5
mov [edi+ecx*4],eax
pop ecx
dec ecx
or ecx,ecx
jnz @B

ret
EncryptString ENDP

DecryptString PROC  lpDataString, CryptKey, lpOutString, cbdata
uses edi,esi
mov ecx,[cbdata]
mov edi,[lpOutString]
mov esi,[lpDataString]

push ecx
dec ecx
mov eax,[esi+ecx*4]
rol eax,5
xor eax,[CryptKey]
ror eax,6
mov [edi+ecx*4],eax
pop ecx
dec ecx
or ecx,ecx
jnz @B
ret

DecryptString ENDP

invoke ExitProcess,0

end start

PBrennick

#13
Skywalker,
The following code assembles correctly but really doe not do what you want without some additional work.

.386
.model flat, stdcall
option casemap:none

include     \masm32\include\windows.inc
include     \masm32\include\kernel32.inc
include     \masm32\include\user32.inc
include     \masm32\include\advapi32.inc
includelib  \masm32\lib\user32.lib
includelib  \masm32\lib\kernel32.lib
includelib  \masm32\lib\advapi32.lib

GetKey          PROTO
GenKey          PROTO :DWORD
EncryptString   PROTO :DWORD, :DWORD, :DWORD, :DWORD
DecryptString   PROTO :DWORD, :DWORD, :DWORD, :DWORD

.DATA
    ; This is a very simple pseudo-encrypted block, it is not meant to
    ; be secure in any way and is very easy to decrypt by anyone at all.
    ; It says "SOFTWARE\Microsoft\Windows\CurrentVersion",0,"ProductId"
    ; It is used in GetKey to generate an encryption key for passwords
    ; but I didn't want to just leave it in ansi so everyone could see.
    ; It requires Key# 152715150 to decrypt it
    cryptdata   DB  05Ah,04Fh,0C4h,0D8h,052h,053h,0ECh,0FAh,044h,04Bh
                DB  09Ah,0B6h,018h,00Fh,0AEh,0AEh,030h,039h,0F0h,0DEh
                DB  02Eh,00Dh,080h,0AEh,012h,037h,0F0h,0F6h,016h,035h
                DB  0ACh,0BAh,020h,039h,0E4h,0BAh,018h,037h,09Ah,0AEh
                DB  020h,0D1h,0E8h,094h,022h,019h,0A2h,0B6h,014h,043h
                DB  080h,070h

.CODE

start:

GetKey PROC
;--------------------------------------
    LOCAL   KSRegKey[256] :BYTE
    LOCAL   KeyString[64] :BYTE
    LOCAL   hRegKey :DWORD
    LOCAL   Disposition :DWORD
    LOCAL   uDataCode :DWORD
    LOCAL   cbRead :DWORD
;
    invoke  RtlZeroMemory, ADDR KSRegKey, sizeof KSRegKey
    invoke  DecryptString, OFFSET cryptdata, 152715150, ADDR KSRegKey, 13
    invoke  RegCreateKeyEx, HKEY_LOCAL_MACHINE, ADDR KSRegKey, NULL, NULL,\
            REG_OPTION_NON_VOLATILE, KEY_READ, NULL, ADDR hRegKey, ADDR Disposition
    or      eax, eax
    jz      @F
    xor     eax, eax
    dec     eax
    ret
@@:
    mov     DWORD PTR [cbRead], 64
    invoke  RegQueryValueEx, [hRegKey], ADDR KSRegKey+42, NULL, ADDR uDataCode,\
            ADDR KeyString, ADDR cbRead
    invoke  RegCloseKey, [hRegKey]
    invoke  GenKey, ADDR KeyString
    xor     eax, eax
    RET
;--------------------------------------
GetKey ENDP

GenKey PROC uses edi esi lpKeyString:DWORD
; Don't rem out the lpKeyString in the above line
;--------------------------------------
    invoke  lstrlen, [lpKeyString]
    mov     edi, 0
    mov     ecx, eax
    mov     esi, [lpKeyString]
@@:
    push    ecx
    dec     ecx
    mov     eax, [esi+ecx]
    add     edi, eax
    pop     ecx
    dec     ecx
    or      ecx, ecx
    jnz     @B
    clc
;;;;    dwCryptKey in the next line is undefined
;;;;    mov     [dwCryptKey], edi
    ret
;--------------------------------------
GenKey ENDP

EncryptString PROC uses edi esi lpDataString:DWORD, CryptKey:DWORD, lpOutString:DWORD, cbdata:DWORD
;--------------------------------------
    mov     ecx, [cbdata]
    mov     edi, [lpOutString]
    mov     esi, [lpDataString]
@@:
    push    ecx
    dec     ecx
    mov     eax, [esi+ecx*4]
    rol     eax, 6
    xor     eax, [CryptKey]
    ror     eax, 5
    mov     [edi+ecx*4], eax
    pop     ecx
    dec     ecx
    or      ecx, ecx
    jnz     @B
    ret
;--------------------------------------
EncryptString ENDP

DecryptString PROC uses edi esi lpDataString:DWORD, CryptKey:DWORD, lpOutString:DWORD, cbdata:DWORD
;--------------------------------------
    mov     ecx, [cbdata]
    mov     edi, [lpOutString]
    mov     esi, [lpDataString]
@@:
    push    ecx
    dec     ecx
    mov     eax, [esi+ecx*4]
    rol     eax, 5
    xor     eax, [CryptKey]
    ror     eax, 6
    mov     [edi+ecx*4], eax
    pop     ecx
    dec     ecx
    or      ecx, ecx
    jnz     @B
    ret
;--------------------------------------
DecryptString ENDP

END start


I have included a test project to show you that the conversion from GOASM is complete.

hth,
Paul


[attachment deleted by admin]
The GeneSys Project is available from:
The Repository or My crappy website

skywalker

Quote from: PBrennick on April 01, 2006, 06:31:33 AM
Skywalker,
The following code assembles correctly but really doe not do what you want without some additional work.

I have included a test project to show you that the conversion from GOASM is complete.

hth,
Paul


Thanks, I look it over.

Andy