bungle / lua-resty-nettle

LuaJIT FFI bindings for Nettle (a low-level cryptographic library)
BSD 2-Clause "Simplified" License
182 stars 45 forks source link

How to use des? #3

Closed hcaihao closed 8 years ago

hcaihao commented 8 years ago

Could you give a sample?

bungle commented 8 years ago

How about this:

local function hex(str,spacer)
    return (string.gsub(str,"(.)", function (c)
        return string.format("%02X%s",string.byte(c), spacer or "")
    end))
end

do
    local des = require "resty.nettle.des"
    print("DES check   ", "testtest", des.check_parity("testtest"))
    print("DES fix     ", "testtest", des.fix_parity("testtest"))
    print("DES check   ", des.fix_parity("testtest"), des.check_parity(des.fix_parity("testtest")))
end

print()

do
    local des = require "resty.nettle.des"
    local ds, wk = des.new("testtest")
    local ciphertext = ds:encrypt("a")
    print("DES enc     ", wk, #ciphertext, hex(ciphertext))
    local ds, wk = des.new("testtest")
    local plaintext = ds:decrypt(ciphertext)
    print("DES dec     ", wk, #plaintext, plaintext)
end

print()

do
    local des = require "resty.nettle.des"
    local ds, wk = des.new("testtest", "cbc", "kalakala")
    local ciphertext = ds:encrypt("testtestkalakala")
    print("DES cbc enc ", wk, #ciphertext, hex(ciphertext))
    local ds, wk = des.new("testtest", "cbc", "kalakala")
    local plaintext = ds:decrypt(ciphertext)
    print("DES cbc dec ", wk, #plaintext, plaintext)
end

print()

do
    local des = require "resty.nettle.des"
    local ds, wk = des.new("testtest", "ctr", "kalakala")
    local ciphertext = ds:encrypt("testtestkalakala")
    print("DES ctr enc ", wk, #ciphertext, hex(ciphertext))
    local ds, wk = des.new("testtest", "ctr", "kalakala")
    local plaintext = ds:decrypt(ciphertext)
    print("DES ctr dec ", wk, #plaintext, plaintext)
end

print()

do
    local des = require "resty.nettle.des"
    print("DES3 check   ", "testtestkalakalatesttest", des.check_parity("testtestkalakalatesttest"))
    print("DES3 fix     ", "testtestkalakalatesttest", des.fix_parity("testtestkalakalatesttest"))
    print("DES3 check   ", des.fix_parity("testtestkalakalatesttest"), des.check_parity(des.fix_parity("testtestkalakalatesttest")))
end

print()

do
    local des = require "resty.nettle.des"
    local ds, wk = des.new("testtestkalakalatesttest")
    local ciphertext = ds:encrypt("a")
    print("DES3 enc     ", wk, #ciphertext, hex(ciphertext))
    local ds, wk = des.new("testtestkalakalatesttest")
    local plaintext = ds:decrypt(ciphertext)
    print("DES3 dec     ", wk, #plaintext, plaintext)
end

print()

do
    local des = require "resty.nettle.des"
    local ds, wk = des.new("testtestkalakalatesttest", "cbc", "kalakala")
    local ciphertext = ds:encrypt("testtestkalakala")
    print("DES3 cbc enc", wk, #ciphertext, hex(ciphertext))
    local ds, wk = des.new("testtestkalakalatesttest", "cbc", "kalakala")
    local plaintext = ds:decrypt(ciphertext)
    print("DES3 cbc dec", wk, #plaintext, plaintext)
end

print()

do
    local des = require "resty.nettle.des"
    local ds, wk = des.new("testtestkalakalatesttest", "ctr", "kalakala")
    local ciphertext = ds:encrypt("testtestkalakala")
    print("DES3 ctr enc", wk, #ciphertext, hex(ciphertext))
    local ds, wk = des.new("testtestkalakalatesttest", "ctr", "kalakala")
    local plaintext = ds:decrypt(ciphertext)
    print("DES3 ctr dec", wk, #plaintext, plaintext)
end
bungle commented 8 years ago

I added more examples here: https://github.com/bungle/lua-resty-nettle#synopsis

I didn't add rsa (or dsa for that matter) examples as the API is still work in progress.

hcaihao commented 8 years ago

thank you! for your example!

Does nettle support des with zero padding?

Mode: ECB with Zero Padding Key: testtest Plain: a Chiper: 236BF47A8D784246 Plain: 123456789 Chiper: A79B8B6A1764D154A40150E08F8F1346

bungle commented 8 years ago

@hcaihao well, it seems it doesn't but it is easy to do.

bungle commented 8 years ago

@hcaihao I just added zero padding support for DES in this commit: https://github.com/bungle/lua-resty-nettle/commit/d64400550b9dcbc732767e153c0ec1e588a3291d

bungle commented 8 years ago

@hcaihao

And here is how you would use it:

do
    local des = require "resty.nettle.des"
    local ds, wk = des.new("testtest")
    local ciphertext = ds:encrypt("123456789", true)
    print("DES enc     ", wk, #ciphertext, hex(ciphertext))
    local ds, wk = des.new("testtest")
    local plaintext = ds:decrypt(ciphertext)
    print("DES dec     ", wk, #plaintext, plaintext)
end

See the added true parameter in encrypt. It will print:

DES enc         false   16  A79B8B6A1764D154A40150E08F8F1346
DES dec         false   9   123456789
bungle commented 8 years ago

Though, I'm not sure if we should add other padding methods as well. But well, you could always pad by yourself. I'm not making a new release just yet, because I need to think this more.

hcaihao commented 8 years ago

Great work! Tks!

bungle commented 8 years ago

I'll close this. Please reopen if you still have problems.