java - on android, getting xml rsa public key and encrypt a string with it -


i'm developing android app , getting public key server of company work with:

pfjtqutlevzhbhvlpjxnb2r1bhvzpnzocfhkrwvotu5pzdhuotluekrgmvo4mdnvtedrszlqwnnfodldd2tis29gv0tgzmt2qtzkodbnwhhpznhqbfziyu8vywm4yupmc1axwvr1rfnhvis3vexql0puvvpynljqqtdpbflmmitvwexis0u2zw1ryzbkdxloavarl0ftmgzmkzywsnzqekhyeedqqnvibwttcmrqdetfv0jczxjzwwnuqvjyt2zsyz08l01vzhvsdxm+pev4cg9uzw50pkfrqui8l0v4cg9uzw50pjwvulnbs2v5vmfsdwu+ 

the server windows iis 7.5.

base64decode give me xml

<rsakeyvalue><modulus>vnpxdeenmnid8n99tzdf1z803olgqk9jzse89cwkbkofwkffkva6j80mxxofxjlvhao/ac8ajlsp1ytudsgv+7tlp/jnuzr6rpa7ilyf2+uxlbke6emqc0juynip+/as0ff+60jvpzhxxgpbuhmksrdjtkewbbersycnarrofrc=</modulus><exponent>aqab</exponent></rsakeyvalue> 

extracting modulu , exponent xml, base64decode them , making spec public key object:

    publickey pbkey = null;     xmlparser parser = new xmlparser();     document doc = parser.getdomelement(publickeystring);     element rsakeyvalue = doc.getdocumentelement();      string modulusbase64 = parser.getvalue(rsakeyvalue, "modulus");     byte[] modulus = base64.decode(modulusbase64, 0);      string exponentbase64 = parser.getvalue(rsakeyvalue, "exponent");     byte[] exponent = base64.decode(exponentbase64, 0);      biginteger modbiginteger = new biginteger(1,modulus);     biginteger exbiginteger = new biginteger(1,exponent);      rsapublickeyspec spec = new rsapublickeyspec(modbiginteger, exbiginteger);     try {         keyfactory factory = keyfactory.getinstance("rsa");         pbkey = factory.generatepublic(spec);     } catch (exception e) {         e.printstacktrace();     } 

creating cipher , adding plain text encrypt with:

    security.addprovider(new org.bouncycastle.jce.provider.bouncycastleprovider());     cipher cipher = cipher.getinstance("rsa/ecb/pkcs1padding", "bc");      byte[] keybytes =   base64.decode(this.publickey, 0);      /* strtopublickey previews code block */     publickey publickey = strtopublickey(new string(keybytes));     cipher.init( cipher.encrypt_mode , publickey );      // base 64 encode removed.     //byte[] encryptedbytes = base64.encode( cipher.dofinal(plaintext.getbytes()), 0 );     byte[] encryptedbytes = cipher.dofinal(plaintext.getbytes()); 

everything here working server don't accept it, i'm base64 encode , sending byte array.

the server admin saying it's long, supposed 128 bit it's 174 bit... i'm doing wrong?

how called key? rsa xml correct? or it's got other name?

i can pem string server if have idea how it.

edit: forgot put here important part, i'm sending data server byte array, how make string of it:

public static int unsignedtobytes(byte b) {     return b & 0xff; } stringbuilder bytearraystring = new stringbuilder(); int byteslength = bytes.length; int bytescounter = 0; (byte abyte : bytes) {     bytescounter++;     bytearraystring.append(unsignedtobytes(abyte));     if(bytescounter < byteslength){         bytearraystring.append(",");     } } 

solved! - solution: bytes string builder used signed use function unsignedtobytes() make them unsigned , removed base64encription on encrypt() function. problem, hope else.

your variable naming bad. encryptedbytes contains not encrypted bytes encrypted bytes in base64 encoding.

this reason not expected result.

the length of encrypted data before applying base64 encoding 128 byte.

afterwards length 172 bytes.


Comments

Popular posts from this blog

javascript - Karma not able to start PhantomJS on Windows - Error: spawn UNKNOWN -

c# - Display ASPX Popup control in RowDeleteing Event (ASPX Gridview) -

Nuget pack csproj using nuspec -