vb.net - RSA Cypher VisualBasic to Java -
i'm having problems encrypt strings in java. need encrypt them same way visualbasic code does:
public function encrypt(byref encryptionkeypair keypair, byval plaintext string) string //use public key encrypt m_objrsa.fromxmlstring(encryptionkeypair.publickey.key) //get modulus size , compare length of plaintext // if length of plaintext > (modulus size - 11), plaintext need broken segments of size (modulus size - 11) //each of these segments encrypted separately // , homecoming encrypted strings equal modulus size (with @ to the lowest degree 11 bytes of padding) //when decrypting, if encryptedtext string > modulus size, split segments of size equal modulus size //each of these encryptedtext segments decrypted individually resulting plaintext segments re-assembled. dim intblocksize integer = getmodulussize(encryptionkeypair.publickey.key) - 11 dim strencryptedtext string = "" while len(plaintext) > 0 if len(plaintext) > intblocksize strencryptedtext = strencryptedtext & encryptblock(left(plaintext, intblocksize)) plaintext = right(plaintext, len(plaintext) - intblocksize) else strencryptedtext = strencryptedtext & encryptblock(plaintext) plaintext = "" end if end while homecoming strencryptedtext end function private function encryptblock(byref thersaprovider rsacryptoserviceprovider, byval strin string) string homecoming bytearrayasstring(thersaprovider.encrypt(stringasbytearray(strin), false)) end function private function getmodulussize(byval intkeysize integer) integer //keysize in bits - split 8 # of bytes homecoming intkeysize / 8 end function
i've searched in net , haven't found this. have public key modulus , exponent , i'm doing this:
byte[] expbytes = base64.decode(exponent.trim()); byte[] modbytes = base64.decode(modulus.trim()); biginteger modules = new biginteger(1, modbytes); biginteger exponents = new biginteger(1, expbytes); keyfactory mill = keyfactory.getinstance("rsa"); cipher cipher = cipher.getinstance("rsa/ecb/pkcs1padding"); rsapublickeyspec pubspec = new rsapublickeyspec(modules, exponents); publickey pubkey = factory.generatepublic(pubspec); cipher.init(cipher.encrypt_mode, pubkey); byte[] encrypted =cipher.dofinal(field.getbytes("utf-16le")); string string = new string(encrypted);
the result not right because i'm doing nil modulus size - 11. please explain me how can in java?
thank you.
the modulus size not problem. problem more expecting same values generated. not, not in vb code or java code (run code snippets twice!). rsa pkcs#1 v1.5 padding contains random numbers, ensuring encryption result in different value. same oaep padding way.
note might want @ oaep mode , hybrid cryptosystem instead of doing now. safer , able handle size of data, although amount of ciphertext larger of course.
java vb.net encryption rsa
No comments:
Post a Comment