RSA encryption DLL

BlitzMax Forums/BlitzMax Programming/RSA encryption DLL

Hi :)

I have made a little DLL to encrypt data to RSA format :)

you can do :

- Generate unlimited size RSA key

- Crypt/Decrypt string

- Crypt/Decrypt files

- Crypt/Decrypt memory bank (useful if you want make your hown file format)

I hope that useful for you :)

http://www.blitz3dfr.com/tempo/RSA_Encryption.rar

Example :

Strict

Const DllName:String = "RSA_Encryption.dll"
Local DllHandle = LoadLibraryA(DllName)

If DLLhandle = 0 Then
	Notify "Unable to initialize dll : "+DllName
	End
EndIf

Global RSA_CreateKey$z(Lenght:Int)"Win32" = GetProcAddress(DllHandle,"RSA_CreateKey")
Global RSA_EncodeMD5Key$z(key$z)"Win32" = GetProcAddress(DllHandle,"RSA_EncodeMD5Key")

Global RSA_EncodeRSAString$z(msg$z,key$z)"Win32" = GetProcAddress(DllHandle,"RSA_EncodeRSAString")
Global RSA_EncodeRSAFile(file$z,ouput$z,key$z)"Win32" = GetProcAddress(DllHandle,"RSA_EncodeRSAFile")

Global RSA_EncodeRSABank:Byte Ptr(Mem:Byte Ptr, MemLen:Int, key$z)"Win32" = GetProcAddress(DllHandle,"RSA_EncodeRSABank")


Local MyString:String="Hello world"
Local MyKey:String="Hi it's my key"
Local MyResult:String



' -------------------------------------------------------------------
' Function to create MD5 key from a string and generate 1024 byte key
' -------------------------------------------------------------------
Local MD5Key:String=RSA_EncodeMD5Key("Hi it's my key")
Print "RSA Key : "+RSA_CreateKey(1024)
Print ""
Print "MD5 Key : "+MD5Key+" / From : "+MyKey
Print ""


' ----------------------------------------------
' Function to encode/decode string in RSA format
' ----------------------------------------------
MyResult=RSA_EncodeRSAString(MyString,MD5Key)

Print "RSA Encryption :"
Print "----------------"
Print "String  : "+MyString
Print "Result  : "+MyResult
Print "Decoded : "+RSA_EncodeRSAString(MyResult,MD5Key)
Print ""

' -------------------------------------
' Function to encode file in RSA format
' -------------------------------------
Print "RSA file Encryption :"
Print "---------------------"
RSA_EncodeRSAFile("document.txt","document.rsa",MD5Key)
RSA_EncodeRSAFile("document.rsa","document_ori.txt",MD5Key)
Print ""


' -----------------------
' Function to encode bank
' -----------------------
Local MyBank:TBank=CreateBank(Len(MyString))

For Local i:Int=0 To Len(MyString)-1
	PokeByte(MyBank,I,Asc(Mid(MyString,I+1,1)))
Next

RSA_EncodeRSABank(BankBuf(MyBank),BankSize(MyBank),MD5Key)

For Local i:Int=0 To Len(MyString)-1
	MyResult=MyResult+Chr(PeekByte(MyBank,I))
Next



Print MyResult ; MyResult=""

RSA_EncodeRSABank(BankBuf(MyBank),BankSize(MyBank),MD5Key)

For Local i:Int=0 To Len(MyString)-1
	MyResult=MyResult+Chr(PeekByte(MyBank,I))
Next

Print MyResult

MyBank=Null


Awesome, very useful. I made it into a module.

here's the source

rsaencryption.bmx
Strict

Rem
	bbdoc: Filax's RSA Encryption Module
End Rem
Module Filax.RSAEncryption

Import "-lRSA_Encryption"

Extern
	Rem
		bbdoc: Coming Soon!
	End Rem
	Function RSA_CreateKey$z(Lenght:Int)
	Rem
		bbdoc: creates a MD5 key from a string and generates a 1024 byte key
	End Rem
	Function RSA_EncodeMD5Key$z(key$z)
	
	Rem
		bbdoc: encodes/decodes a string in RSA format
	End Rem
	Function RSA_EncodeRSAString$z(msg$z,key$z)
	Rem
		bbdoc: encodes a file in RSA format
	End Rem
	Function RSA_EncodeRSAFile(file$z,ouput$z,key$z)
	
	Rem
		bbdoc: encodes a bank
	End Rem
	Function RSA_EncodeRSABank:Byte Ptr(Mem:Byte Ptr, MemLen:Int, key$z)
End Extern


The module needs a RSA_Encryption lib file, so use this batch file to create libRSA_Encryption.a and place that .a file into your BlitzMax\lib folder

dll2a.bat
@echo off
pexports RSA_Encryption.dll >RSA_Encryption.def
dlltool -d RSA_Encryption.def -l libRSA_Encryption.a


thanks for this stuff ! :) i have made a DLL to keep
possibility to use this RSA with blit3D :) but a lib
is a good idea too :)

Well yeah, the only catch is you still need the DLL...what did you write the DLL in? If you compile it as a static library, then you can just import it into the module and no dll is required.

This looks very interesting... But a DLL-less version would be even better, ofcourse

I will try to convert the code to a bmax module. Personaly
i prefer DLL :) look more professional :)))

Not really.
It only means that someone was not able to write the code himself / herself and had to buy others precompiled code ;-)

Otherwise there is no usefull reason for a DLL as it is slower than internal calls.

.

filax, you do not need to write it in BlitzMax. See my post three posts up. :)

My DLL source use LONG and WORD var : For LONG there is no
problem, but for WORD ? blitzmax seem don't have WORD ?

the doc say :

Byte, Float, Short, Long, Double etc

Because the encrypted string result is different between max
and my dll ? :/

Dreamora : for the look of dll :) It was a joke :)

Khomy Prime : Talking about RSA code (to convert in blitzmax) ! :) Thanks for your code !

For WORD i just use a normal integer...seems to work fine

A new version of the DLL coming soon with some usefull feature :)

Hi :) i have a problem with .A of the DLL

i have update Khomy Prime code with the new dll version

Strict

Import "-lRSA_Encryption"


Extern
	Rem
		bbdoc: Coming Soon!
	End Rem
	Function RSA_SetLEDKey(key$z,state:Byte)

	Rem
		bbdoc: Coming Soon!
	End Rem
	Function RSA_GetLEDKey:Byte(key$z)

	Rem
		bbdoc: Coming Soon!
	End Rem	
	Function RSA_GetClipboardText$z()

	Rem
		bbdoc: Coming Soon!
	End Rem		
	Function RSA_SetClipboardText:Int(msg$z)
	
	Rem
		bbdoc: Coming Soon!
	End Rem		
	Function RSA_GetOsVersion$z()
	
	Rem
		bbdoc: Coming Soon!
	End Rem		
	Function RSA_CopyFile:Int(in$z,out$z)
	
	Rem
		bbdoc: Coming Soon!
	End Rem		
	Function RSA_CreateDESKey$z(msg$z,key$z)
	
	Rem
		bbdoc: Coming Soon!
	End Rem		
	Function RSA_CreateHEXKey$z(Lenght:Int)
	
	Rem
		bbdoc: Coming Soon!
	End Rem		
	Function RSA_EncodeMD5Key$z(key$z)
	
	Rem
		bbdoc: Coming Soon!
	End Rem		
	Function RSA_EncodeRSAString$z(msg$z,key$z)
	
	Rem
		bbdoc: Coming Soon!
	End Rem		
	Function RSA_EncodeRSAFile(file$z,ouput$z,key$z)
	
	Rem
		bbdoc: Coming Soon!
	End Rem		
	Function RSA_EncodeRSABank:Byte Ptr(Mem:Byte Ptr, MemLen:Int, key$z)
End Extern


This code compile without problem, but when i try to use
the functions, compiler reply me :
K:/PureBasic/Sources/RSA Encryption/.bmx/TestDLL Included.Bmx.gui.release.win32.x86.o(code+0xd0): undefined reference to `RSA_GetLEDKey'
K:/PureBasic/Sources/RSA Encryption/.bmx/TestDLL Included.Bmx.gui.release.win32.x86.o(code+0xf1): undefined reference to `RSA_GetLEDKey'
K:/PureBasic/Sources/RSA Encryption/.bmx/TestDLL Included.Bmx.gui.release.win32.x86.o(code+0x112): undefined reference to `RSA_GetLEDKey'

Any idea ?

Hi :) this is a little update now you can :

- Play with the keyboard led key :)
- Create/remove virtual hard drive
- Copy/Paste text from clipboard
- Get the os version
- Copy files

http://www.blitz3dfr.com/tempo/RSA_Encryption.rar

I have one question about RSA encryption...

I did a search on RSA appearantly it has "holes", and if there are so many programs out there to encypt/decrypt it wouldnt it also be known how to HACK it!

wouldnt it be better (hold back your stones please...) to write a unknown encryption method that some one cant just google up the details of, wouldnt it be better if the encryption method was custom made and a secret.


Quote: "In february of 1999 a 465-bits RSA key was cracked in six weeks."


Im using encryption its GREAT! ,my trial version retro 8bit compiler spits out encrypted binaries, (great to encourage people to get full version as encrypted binaries are no good for buring to eprom for use on the real 1982 console and encypted binaries dont work on other emulators also.)

I bet no person could hack my encryption becuase the method is not published, but in your case I even know extactly what method you are using so therefore I could maybe even find something already written to hack it, but how do you hack an unknown encryption method?

Ok so im bound to get shit over this post!

Maybe I could start a competion to break my code with a $10,000 reward, any takers?

Except that I will never pay out because I HATE HACKERS!

You are right, but maybe you can made a modified RSA code ? :)
i'm looking quantic encryption method, it's a crazy stuff !

http://www.newsfactor.com/perl/story/13468.html

I chuck the key into the binary ,again details are secret, only registered users (with advanced knowledge of what the result should be) would even know what the result should be in the first place so hacking the code would require a voilation of there license agreement.

The key is hidden in the binary so its hard to find, I admit that im not an encryption expert, but I just dont like the sound of using an encryption method that has a large number of people trying to hack it as part of a competion, sounds dodgy.

.

Hey filax, sorry it took so long but i just checked my mail for the first time in weeks. Are you still getting errors with my bit of code? It sounds to me you haven't updated the libRSA_Encryption.a file. Run the batch file and tell me what happens.