i have encrypted string in visual basic. net 2008, functions encrypt , decrypt following:
imports system.security.cryptography public shared function encriptar(byval strvalor string) string dim strencrkey string = "key12345" dim bykey() byte = {} dim iv() byte = {&h12, &h34, &h56, &h78, &h90, &hab, &hcd, &hef} try bykey = system.text.encoding.utf8.getbytes(strencrkey) dim des new descryptoserviceprovider dim inputbytearray() byte = encoding.utf8.getbytes(strvalor) dim ms new memorystream dim cs new cryptostream(ms, des.createencryptor(bykey, iv), cryptostreammode.write) cs.write(inputbytearray, 0, inputbytearray.length) cs.flushfinalblock() return convert.tobase64string(ms.toarray()) catch ex exception return "" end try end function public shared function desencriptar(byval strvalor string) string dim sdecrkey string = "key12345" dim bykey() byte = {} dim iv() byte = {&h12, &h34, &h56, &h78, &h90, &hab, &hcd, &hef} dim inputbytearray(strvalor.length) byte try bykey = system.text.encoding.utf8.getbytes(sdecrkey) dim des new descryptoserviceprovider if trim(strvalor).length = 0 throw new exception("password no debe estar en blanco") end if inputbytearray = convert.frombase64string(strvalor) dim ms new memorystream dim cs new cryptostream(ms, des.createdecryptor(bykey, iv), cryptostreammode.write) cs.write(inputbytearray, 0, inputbytearray.length) cs.flushfinalblock() dim encoding system.text.encoding = system.text.encoding.utf8 return encoding.getstring(ms.toarray(), 0, ms.toarray.count) catch ex exception return "" end try end function
for example word "android" encrypted function gives me result "b3xogi/qfsc="
now need decrypt string "b3xogi/qfsc=" java, same key, "key12345", , result should "android"...anyone know how this?
thanks in advance.
using apache commons codec hex , base64 encoding/decoding, can use following code:
keyspec ks = new deskeyspec("key12345".getbytes("utf-8")); secretkey key = secretkeyfactory.getinstance("des").generatesecret(ks); ivparameterspec iv = new ivparameterspec( hex.decodehex("1234567890abcdef".tochararray())); cipher cipher = cipher.getinstance("des/cbc/pkcs5padding"); cipher.init(cipher.decrypt_mode, key, iv); byte[] decoded = cipher.dofinal(base64.decodebase64("b3xogi/qfsc=")); system.out.println("decoded: " + new string(decoded, "utf-8"));
Comments
Post a Comment