• Home
  • Script library
  • AltME Archive
  • Mailing list
  • Articles Index
  • Site search
 

World: r4wp

[Rebol School] REBOL School

BrianH
1-Feb-2013
[1643x2]
Rebol isn't class-based. In class-based languages, that separation 
of code and data happens automatically - code is class definitions, 
data is instance creation. In Rebol, if you need to keep your code 
and data separate you have to do it yourself.
In this case, you keep your code and data separate because code is 
best saved with MOLD and reconstructed with DO, but data of types 
that don't have a normal literal form (but aren't affected by binding) 
are often better saved with MOLD/all and restored with LOAD. The 
main thing is that we don't have a literal syntax to declare word 
bindings; instead, we have a way to construct them with code. Same 
goes for cyclic or DAG structures that aren't strictly nested. So, 
if you need to create such things, you need to run code. And you 
need to keep your untrustworthy data that you can't safely DO separate 
from that code.
caelum
2-Feb-2013
[1645]
Thanks BrianH. I am aware of the need to "keep your untrustworthy 
data that you can't safely DO separate from that code."


I am creating a small Rebol server capable of communicating with 
clients, using RSA key exchange and the blowfish algorithm, both 
of which work to reasonably high encryption levels in Rebol, 4096 
for RSA and 512 for Blowfish (yes I know the effective upper limit 
for Blowfish is 448 bits, but that is good enough for my purposes).


I want to save the RSA key as a block so it can be loaded back into 
the program and used again, hence my question. It will be encrypted, 
wherever it gets saved, so there will be no chance of it being messed 
with.


Actually, I am writing a much simpler version of Rebol Services, 
since I could not get that to work and my ability to code in Rebol 
was not sufficiently developed yet to see how to get it working.


I am in a steep learning curve right now with Rebol and the time 
I am investing is starting to pay off. Thanks for the information 
about keeping code and data separate. It's always good to be reminded 
of 'obvious' truths.
Reichart
2-Feb-2013
[1646]
Francis, if you search through the Qtask source, you will find a 
448 encryptor writte in JS that runs in the browser, you might enjoy 
how we did it.
caelum
8-Feb-2013
[1647]
So I have 

mailbox: open [
    scheme: 'pop
    user: "[user-:-domain-:-org]"
    pass: "password"
    host: "mail.domain.org"
]


and I am getting my email as expected. Two questions. Is my password 
being encrypted by Rebol when it communicates with my mail server? 
How do I know?
GrahamC
8-Feb-2013
[1648x3]
trace/net on
and then see the exchange
From memory, pop supports apop.
If you want encryption, use pops or spop
caelum
8-Feb-2013
[1651]
Replacing pop with apop, spop or pops in mailbox all produce the 
same error:
    ** Access Error: Invalid port spec: scheme apop
GrahamC
9-Feb-2013
[1652]
See here http://stackoverflow.com/questions/1128826/downloading-mail-from-hotmail
caelum
23-Feb-2013
[1653]
So I have a question about RSA encryption. When I run the following 
code:

  rsa-key: rsa-make-key
  rsa-generate-key rsa-key 1024 3

  crypt-key: copy/part checksum/secure mold now/precise 16
  print crypt-key

  crypt-key: rsa-encrypt rsa-key crypt-key
  print crypt-key

  crypt-key: rsa-encrypt/private/decrypt rsa-key crypt-key
  print crypt-key


it runs perfectly, encrypts the crypt-key and then decrypts it sucessfully.


As you probably know, the purpose of the RSA algorithm is to allow 
someone else to encrypt data that only you can decrypt using your 
private key. I tried this with a different public key using the following 
code:

  rsa-key1: rsa-make-key
  rsa-generate-key rsa-key1 1024 3

  rsa-key2: rsa-make-key
  rsa-key2/n: rsa-key1/n

  crypt-key: copy/part checksum/secure mold now/precise 16
  print crypt-key

  crypt-key: rsa-encrypt rsa-key2 crypt-key
  print crypt-key

  crypt-key: rsa-encrypt/private/decrypt rsa-key2 crypt-key
  print crypt-key


So I put the public key from rsa-key1 into another object, rsa-key2 
and tried using it to encrypt the data and get the following error.

	#{DD44AC1810E9A7020FAD72A7CFA54100}
	Segmentation fault


How do I get the public key from the first object into the second 
object so that it can be used to encrypt data?
Cyphre
23-Feb-2013
[1654x4]
caelum, I corrected your non-working exaple:
;key1 - contains public and private keys
rsa-key1: rsa-make-key
rsa-generate-key rsa-key1 1024 3

;key2 - contains only public key
rsa-key2: rsa-make-key
rsa-key2/e: 3
rsa-key2/n: rsa-key1/n

;data to ecrypt encrypt 
data: copy/part checksum/secure mold now/precise 16

;encrypt data using the key2 (with pub key only)
crypt-key: rsa-encrypt rsa-key2 data


;decrypt data(that have been encrypted using key2) using the key1(needs 
to contain private key)
data2: rsa-encrypt/private/decrypt rsa-key1 crypt-key

either equal? data data2 [
	print "decrypted data match the original - decription passed"
][

 print "decrypted data differs from the original - decryption failed"
]
the problem was you forgot to set the generator value (key/e) when 
makeing rsa-key2
and also you tried to decrypt the data using rsa-key2 which doesn't 
contain the private key
caelum
23-Feb-2013
[1658]
Got it! Thanks Cyphre, much appreciated.
caelum
27-Feb-2013
[1659]
Another question. On linux (linux mint debian) these segments of 
code are part of a larger program. I separated them out because they 
are causing trouble when I try to 'enface' them.


    set-net [[me-:-mydomain-:-net] mail.mydomain.net pop.mydomain.net none 
    none none "[me-:-mydomain-:-net]" "PASSWORD"]


    open [scheme: 'pop user: "[me-:-mydomain-:-net]" pass: "PASSWORD" host: 
    "mail.mydomain.net"]


The code encapsulates without errors, but when I run the program 
after 'enface' it gives the following errors:

    Set-Net not provided.

    ** Access Error: Invalid port spec: scheme pop user [me-:-mydomain-:-net] 
    pass PASSWORD host mail.mydomain.net

    ** Near: open [scheme: 'pop user: "[me-:-mydomain-:-net]" pass: "PASSWORD" 
    host: "mail.mydomain.net"]


I would like to be able to 'enface' these code snippets. Any suggestions 
as to how to fix this would be most welcome?
GrahamC
27-Feb-2013
[1660]
if set-net is not defined, get the source and add it to your script
caelum
27-Feb-2013
[1661x2]
set-net is in the source exactly as above, 'enface' is refusing to 
compile it, but it compiles (almost) the rest of the program just 
fine.
I was wrong. Simple programs like (print "Hello World") compile and 
execute, but programs with a lot of code compile but produce a series 
of errors when executed. I noticed a pattern. Wherever a word is 
not followed by ':' causes errors, examples below. Perhaps it's just 
this linux version of enface?

    stylize [
        fld80: field 80x28 font-size 17 white ivory center
        .......
    ]

produces the error:

    ** Script Error: stylize has no value
    ** Near: new-styles: stylize [
        fld80: field 80x28 font-size 17 white ivory center 
        fld400: field 400x28 font-size 17 whit...

I'll try this on windows when I have the opportunity.
GrahamC
27-Feb-2013
[1663x2]
You haven't included view.r
without this no view or vid stuff will run after being encapped
caelum
27-Feb-2013
[1665]
Thanks GrahamC. Where do I find view.r? I just searched my whole 
HD for it. Not there.
GrahamC
27-Feb-2013
[1666x3]
it should be in the <sdk>/source directory
this is mine
#include %gfx-colors.r
#include %gfx-funcs.r

#include %view-funcs.r
#include %view-vid.r
#include %view-edit.r
#include %view-feel.r
#include %view-images.r
#include %view-styles.r
#include %view-request.r


;-- Must be done prior to loading anything that requires fonts on 
Linux.

layout [text "imanXwin kludge"] ;-throw this one away soon-- okay?

open-events
caelum
27-Feb-2013
[1669]
I just copied all the SDK stuff back onto my main HD and I found 
view.r. Thanks for that GrahamC. I'll retry encapping my program 
with the includes..
caelum
28-Feb-2013
[1670]
I have included view.r and now have the graphical elements working 
after 'enface', thanks GrahamC. But I'm still getting the other errors: 
This is all to do with setting up the email parameters:

    Set-Net not provided.

    ** Access Error: Invalid port spec: scheme pop user [me-:-mydomain-:-net] 
    pass PASSWORD host mail.mydomain.net

    ** Near: open [scheme: 'pop user: "[me-:-mydomain-:-net]" pass: "PASSWORD" 
    host: "mail.mydomain.net"]
GrahamC
28-Feb-2013
[1671]
If you did a grep on your source directory you'll find it ... in 
prot-setnet.r
caelum
28-Feb-2013
[1672]
I found set-net in prot-setnet.r and included it with the same includes 
of GrahamC above, and now I get this error:

** Script Error: Invalid path value: pop
** Where: forskip
** Near: forall settings [
    if (item: first settings) = 'none [item: none] 
    if word? item [item: form item] 
    do first ...


Not sure where to go from here as I don't know what I am looking 
at in terms of errors.
GrahamC
28-Feb-2013
[1673]
show us your include files
caelum
28-Feb-2013
[1674]
Same as yours only with prot-setnet.r added.

#include %gfx-colors.r
#include %gfx-funcs.r

#include %view-funcs.r
#include %view-vid.r
#include %view-edit.r
#include %view-feel.r
#include %view-images.r
#include %view-styles.r
#include %view-request.r

#include %prot-setnet.r


;-- Must be done prior to loading anything that requires fonts on 
Linux.

layout [text "imanXwin kludge"] ;-throw this one away soon-- okay?

open-events
GrahamC
28-Feb-2013
[1675x3]
you don't have any protocols
you need to include prot.r
and then remove prot-setnet.r or just add the protocols you need
caelum
28-Feb-2013
[1678]
Got it! Now I see how it all works and I have 'enface' working on 
linux. Many Thanks for your help!
NickA
10-Mar-2013
[1679]
What's the proper (fast) way to do this?

    REBOL [title: "Anagram List"]
    a: copy []
    mix: func [str prev] [   
        repeat i length? str [
            picked: pick str i
            rest: head remove at copy str i
            append a rejoin [prev picked rest]
            mix rest join prev picked
        ]
    ]
    mix input: ask "Text:  " ""
    editor unique a
Sunanda
11-Mar-2013
[1680]
Don't know if it is faster or more proper, but it's another way:
   http://www.rebol.org/view-script.r?script=lexpem.r
Steeve
11-Mar-2013
[1681]
res: copy [ ] 
    mix: func [str /local i][
    	if 1 = i: length? str [append res copy head str exit]
    	loop i [
    		append str str/1
    		mix next remove str
    	]
    ]
MaxV
11-Mar-2013
[1682x4]
Just use math:
a: copy []

mix: func [ myword /local n t] [
	n:  length? myword
	t: 1
	loop n [ 
		t: n * t 
		n: n - 1
		]	
	while [(length? a) < t] [
		append a random myword	
		unique a
	
		]
	a	
	]
mix "test"

editor a
I think that it's quicker, since you don't mess with series altering, 
insted you just create mixed words enough to reach the maximum available 
permutations.
For short words...
:-P
NickA
11-Mar-2013
[1686x2]
Thank you guys.  Steve, that's much quicker.  There's no way that 
succeeds without the exponential increase in evaluation, with each 
added character?  I was hoping there might be some magic to learn...
Steve, that code doesn't appear to create all the possible anagrams.
Steeve
11-Mar-2013
[1688x2]
for instance ?
/me pouring a glass of whisky
NickA
11-Mar-2013
[1690x2]
REBOL [title: "Anagram List"]


a:[]m: func[s p][repeat i length? s[append a rejoin[p k: s/:i r: 
head remove at copy s i]m r join p k]]m ask""""editor unique a probe 
length? a


a:[]m: func[s /local i][if 1 = i: length? s[append a copy head s 
exit]loop i[append s s/1 m next remove s]]m ask""editor a probe length? 
a

halt
Mine is the first one, yours is the second.
Steeve
11-Mar-2013
[1692]
can you give a test case where mine is faling ?