Sunday, 15 May 2011

encryption - Java - Encrypt String with existing public key file -



encryption - Java - Encrypt String with existing public key file -

i've been researching past 4-5 hours , can't seem find reply works despite finding 'answers' used few methods entire ~100 line class. can't imagine there isn't simple function such trivial thing :p

i have pre-existing set of public / private keys (actually, 2 sets - 1 generated ssh-keygen , openssl .. whatever format works cool).

all after simple java equivalent write in python -

key_object = somemodule.keyobject(nameofpublickeyfile) def encrypt (someplaintext) : homecoming someothermodule.encrypt(key_object, someplaintext)

any help awesome!

these openssl commands in shell create rsa key pair , write public , private keys der formatted files.

here, private key file not password-protected (-nocrypt) maintain things simple.

$ openssl genrsa -out keypair.pem 2048 generating rsa private key, 2048 bit long modulus ............+++ ................................+++ e 65537 (0x10001) $ openssl rsa -in keypair.pem -outform der -pubout -out public.der writing rsa key $ openssl pkcs8 -topk8 -nocrypt -in keypair.pem -outform der -out private.der

now have der files, can read them in java , utilize keyspec , keyfactory create publickey , privatekey objects.

public byte[] readfilebytes(string filename) throws ioexception { path path = paths.get(filename); homecoming files.readallbytes(path); } public publickey readpublickey(string filename) throws ioexception, nosuchalgorithmexception, invalidkeyspecexception { x509encodedkeyspec publicspec = new x509encodedkeyspec(readfilebytes(filename)); keyfactory keyfactory = keyfactory.getinstance("rsa"); homecoming keyfactory.generatepublic(publicspec); } public privatekey readprivatekey(string filename) throws ioexception, nosuchalgorithmexception, invalidkeyspecexception { pkcs8encodedkeyspec keyspec = new pkcs8encodedkeyspec(readfilebytes(filename)); keyfactory keyfactory = keyfactory.getinstance("rsa"); homecoming keyfactory.generateprivate(keyspec); }

with public , private keys, can encrypt , decrypt little amounts of info (that fit within rsa modulus.) recommend oaep padding.

public byte[] encrypt(publickey key, byte[] plaintext) throws nosuchalgorithmexception, nosuchpaddingexception, invalidkeyexception, illegalblocksizeexception, badpaddingexception { cipher cipher = cipher.getinstance("rsa/ecb/oaepwithsha1andmgf1padding"); cipher.init(cipher.encrypt_mode, key); homecoming cipher.dofinal(plaintext); } public byte[] decrypt(privatekey key, byte[] ciphertext) throws nosuchalgorithmexception, nosuchpaddingexception, invalidkeyexception, illegalblocksizeexception, badpaddingexception { cipher cipher = cipher.getinstance("rsa/ecb/oaepwithsha1andmgf1padding"); cipher.init(cipher.decrypt_mode, key); homecoming cipher.dofinal(ciphertext); }

here tied simple encryption , decryption:

public void hello() { seek { publickey publickey = readpublickey("public.der"); privatekey privatekey = readprivatekey("private.der"); byte[] message = "hello world".getbytes("utf8"); byte[] secret = encrypt(publickey, message); byte[] recovered_message = decrypt(privatekey, secret); system.out.println(new string(recovered_message, "utf8")); } grab (exception e) { e.printstacktrace(); } }

java encryption rsa

No comments:

Post a Comment