.Net RSA Encryption, Java RSA Decryption [closed]

Squeeninjette

New Member
We have a .Net application that encrypts and decrypts text and store it in the database. I found that the .Net developer have stored the RSA key in the XML format. When they have done with the encryption the text is stored in the database with the following pattern:\[code\]0D-E4-1D-91-44-2B-B5-3B-03-34...and so on (total of 383 characters)\[/code\]We are porting the existing .Net application to the Java platform. For that I want to use the existing encoded data to be decoded.I have written the following code in Java (copy pasted from Internet - I do not have any understanding about RSA or any kind of cryptography)\[code\]public String decryptText (byte[] encryptedText) throws Exception { String decryptedText = null; if (rsaData != null) { byte[] modBytes = Base64.decodeBase64(rsaData.getModulus().trim()); byte[] dBytes = Base64.decodeBase64(rsaData.getD().trim()); BigInteger modules = new BigInteger(1, modBytes); BigInteger d = new BigInteger(1, dBytes); KeyFactory factory = KeyFactory.getInstance("RSA"); Cipher cipher = Cipher.getInstance("RSA"); RSAPrivateKeySpec privSpec = new RSAPrivateKeySpec(modules, d); PrivateKey privKey = factory.generatePrivate(privSpec); cipher.init(Cipher.DECRYPT_MODE, privKey); byte[] decrypted = cipher.doFinal(encryptedText); decryptedText = new String(decrypted); } return decryptedText;}\[/code\]where rsaData = http://stackoverflow.com/questions/10691899/reference that holds the XML data. My XML looks like this:\[code\]<RSAKeyValue><Modulus> ynKHV6Tm/tV7ZsTRMLNjMqlAkbftMXA/OkM5zi6+ih371Jgi6ZNX2T4ml0DmK1hojwumHadiX3YoPlaJ/xiys4NRxbDH9VuJJeTdZvoB8heKxACaIw9l1CCTEkx3jYVJ+h6M10fV3wChD0BOtiLg3rMO64LpR2DAGZNbVjXL3h0=</Modulus><Exponent>AQAB</Exponent><P>5QUpIuwCOqR03GqI0K7Y4yWn0hvYa7b9RkCA9Y1QQ0IyBqfiTYOT87cKBRsu4FxH6tdY0khfFCskFgumx22gOw==</P><Q>4kv69rnEMxSOzmg4ZkAuxrUc6wZCuIJAgSL9gGBFukQSIB6UVWXYhNUDzklcMtGE/lRmwJvwSIlpZBlVtlYthw==</Q><DP>ae7LKlYUad+sFlBI3I4j0F2YlL1AjAJmgNpRTEODPrkdvqplKQmVpAOkZNxAAJNuyJe3g/zpzcBuvqvBBzoUBQ==</DP><DQ>M+iL27aG+9SWYWBkt4e3cxsuU/burRYrp7OYBK+QrwZYRgfdrK0c+nNGWTZYsMuAvzorC7l5Z5olk7GACMBB1w==</DQ><InverseQ> UuLEf34YsvFxzgzKhdONvFhsSDRleC5rPR/XvpJl9NEikwXQHTr4547hYM+4soHRdBaiUFdzNtxLMlRW481L1A==</InverseQ><D> aNSFYMzzEGJbVAv2htFdPI6H8Ozc1gzZsMG+3RU3dS2xiZR9/5VcmBGmygvBJBDTk77kddaHCgeVLzKAoqeXYJGJgiNQgByE+jQz70HkOaAy0muNbAxLOk9UY6lkJ9kVbtSu1LyR83yj4+kSGNo4T63LBKV07TirAhDMJAk3o8E=</D></RSAKeyValue>\[/code\]When I call the decryptText() method by passing in the encoded text I get the following error:\[code\]Exception in thread "main" javax.crypto.IllegalBlockSizeException: Data must not be longer than 128 bytesat com.sun.crypto.provider.RSACipher.a(DashoA13*..)at com.sun.crypto.provider.RSACipher.engineDoFinal(DashoA13*..)\[/code\]Can someone please help in resolving this issue? Or point to some helpful link?So after struggling for few more hours I did the following:I updated two lines in my decryptText() method as follows:\[code\]KeyFactory factory = KeyFactory.getInstance("RSA");Cipher cipher = Cipher.getInstance("RSA/ECB/NOPADDING");\[/code\]I realized that the encrypted string was HEX coded so I reversed it back. So the original string (after removing the dash) became:\[code\]1322829145684318159352601201165341161130212111239120172192552382321681005365571520823020721395242114149106119101747920169161831211748781201152821423521012222201691166150158120221052590252851921052281199918638104578159158115138861911531100176209133541522422481711310198179214164975320920468140732925421720220119120115314722510613321\[/code\]Which after reverse hex became:\[code\]
 
Back
Top