java - Grails password encrypt with rsa -
i want create module called user. module consists of name, username, phoneno, , password. want encrypt , decreypt password using rsa algorythm.
this rsa.java
import java.math.biginteger; import java.security.securerandom; /** * simple rsa public key encryption algorithm implementation. */ public class rsa { private biginteger n, d, e; private int bitlen = 1024; /** create instance can encrypt using elses public key. */ public rsa(biginteger newn, biginteger newe) { n = newn; e = newe; } /** create instance can both encrypt , decrypt. */ public rsa(int bits) { bitlen = bits; securerandom r = new securerandom(); biginteger p = new biginteger(bitlen / 2, 100, r); biginteger q = new biginteger(bitlen / 2, 100, r); n = p.multiply(q); biginteger m = (p.subtract(biginteger.one)).multiply(q.subtract(biginteger.one)); e = new biginteger("3"); while (m.gcd(e).intvalue() > 1) { e = e.add(new biginteger("2")); } d = e.modinverse(m); } /** encrypt given plaintext message. */ public synchronized string encrypt(string message) { return (new biginteger(message.getbytes())).modpow(e, n).tostring(); } /** encrypt given plaintext message. */ public synchronized biginteger encrypt(biginteger message) { return message.modpow(e, n); } /** decrypt given ciphertext message. */ public synchronized string decrypt(string message) { return new string((new biginteger(message)).modpow(d, n).tobytearray()); } /** decrypt given ciphertext message. */ public synchronized biginteger decrypt(biginteger message) { return message.modpow(d, n); } /** generate new public , private key set. */ public synchronized void generatekeys() { securerandom r = new securerandom(); biginteger p = new biginteger(bitlen / 2, 100, r); biginteger q = new biginteger(bitlen / 2, 100, r); n = p.multiply(q); biginteger m = (p.subtract(biginteger.one)).multiply(q .subtract(biginteger.one)); e = new biginteger("3"); while (m.gcd(e).intvalue() > 1) { e = e.add(new biginteger("2")); } d = e.modinverse(m); } /** return modulus. */ public synchronized biginteger getn() { return n; } /** return public key. */ public synchronized biginteger gete() { return e; } }
this domain user.groovy :
class user{ string name string username string phoneno string password }
this usercontroller.groovy : (save , update )
class usercontroller { static allowedmethods = [save: "post", update: "post", delete: "post"] def index = { redirect(action: "list", params: params) } def save = { def userinstance = new user(params) if (userinstance .save(flush: true)) { flash.message = "${message(code: 'default.created.message', args: [message(code: 'user.label', default: 'user'), userinstance .id])}" redirect(action: "show", id: userinstance .id) } else { render(view: "create", model: [userinstance : userinstance ]) } } def edit = { def userinstance = user.get(params.id) if (!userinstance ) { flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'user.label', default: 'user'), params.id])}" redirect(action: "list") } else { return [userinstance : userinstance ] } } def update = { def userinstance = user.get(params.id) if (userinstance ) { if (params.version) { def version = params.version.tolong() if (userinstance .version > version) { userinstance .errors.rejectvalue("version", "default.optimistic.locking.failure", [message(code: 'user.label', default: 'user')] object[], "another user has updated user while editing") render(view: "edit", model: [userinstance : userinstance ]) return } } userinstance .properties = params if (!userinstance .haserrors() && userinstance .save(flush: true)) { flash.message = "${message(code: 'default.updated.message', args: [message(code: 'user.label', default: 'user'), userinstance .id])}" redirect(action: "show", id: userinstance .id) } else { render(view: "edit", model: [userinstance : userinstance ]) } } else { flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'user.label', default: 'user'), params.id])}" redirect(action: "list") } } }
what must add in save , edit controller when save form password encrypted when i'll edit form password decrypted? please me, because i'm new in java , grails, thank :)
similar springsecurity plugin, can use beforeinsert
, beforeupdate
event transformation on password. in method implement encryption.
class user { transient springsecurityservice string username string password boolean enabled boolean accountexpired boolean accountlocked boolean passwordexpired static constraints = { username blank: false, unique: true password blank: false } static mapping = { password column: '`password`' } set<role> getauthorities() { userrole.findallbyuser(this).collect { it.role } set } def beforeinsert() { encodepassword() } def beforeupdate() { if (isdirty('password')) { encodepassword() } } protected void encodepassword() { password = springsecurityservice.encodepassword(password) } }
Comments
Post a Comment