c# - Java RSA/ECB/PKCS1Padding encryption with .NET -
i need encrypt password on web site using public key. public key delivered through java web service information: key rsa key used rsa/ecb/pkcs1padding algorithm. public key delivered json in form:
{ "kid":"pwd", "kty":"rsa", "use":"enc", "n":"mta0otgznjg0otmxmze2njkwntu4mjg3ndiwmdg1nty0odeymjg1mdk2ntcwnzu5ndiznzm0o da3ota2mza0mdczntu0ndq2njg3ody2odk2ntk0njyzntaxmzg0nze1otexmja0mju1mzmzotizmja xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx zcwmja3mzqxotcwnzc4ndawnzm3mty2ndmynziwnjmwmdqwotmwotq0mta2ote1ndezmdawntmymte 5odm0mta2mjazmdiyodewmjyymdm3mdq0nzkxnzizntu1mjqynjyxmze2odc4otc5nzy1otgzmjg4m zq0ndc3otywntg3mze2ntuwmdgx", "e":"nju1mzc" }
i tried encrypt password using public key key generated not correct. here code:
encryptedpassword = encrypit("password01", "mta1mzqxnziwoda3njuwnzg5nd y4odu2mjc0nda3odiwmjq1odq5nde1mdk1mdizmtm3mjm0nzawnzyznjc2mtgwnjg3odmxmja3 nty0ntcxmjg2mzm4njq1nzewmdcymjy2mtqyndizmtczmjkwmdk0mtc0mta5mtc5mzi0njywmjq4nzi3nzm0mtq5ndy0mjuwodkwo xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx c2otaznjc3nzqzodm3nzm0mje2odm0njy4mjm4mtq0ota3mdq3mtk1njc1nzu3ote2njeynzkzmzm2mzi3mduynjg0ndi5ndexnji 2mza0mzm5", "nju1mzc"); public static string encrypit(string password, string publickey, string exponent) { unicodeencoding byteconverter = new unicodeencoding(); byte[] publickeybyte = byteconverter.getbytes(publickey); byte[] passwordbyte = byteconverter.getbytes(password); rsacryptoserviceprovider rsa = new rsacryptoserviceprovider(); rsaparameters rsakeyinfo = new rsaparameters(); rsakeyinfo = rsa.exportparameters(false); //export public key //set rsakeyinfo public key values. rsakeyinfo.modulus = publickeybyte; //rsakeyinfo.exponent = exponentbytes; //i tried set exponent have error rsa.importparameters(rsakeyinfo); byte[] encryptedpassword = rsa.encrypt(passwordbyte, false); homecoming convert.tobase64string(encryptedpassword); }
(the public key different between json , code don't pay attending it, copied info different sources)
the encrypted password obtained far long: encrypted password should 172 characters (i know because have little java programme allows me encrypt passwords correctly) 1100 characters. i not utilize exponent: should ? could not utilize json straight configure rsacryptoserviceprovider correctly ?the reply owlstead helped me encrypted password right string size service using encrypted string reject message: javax.crypto.illegalblocksizeexception: info must not longer 128 bytes
i otained code of java programme wich doing right encryption (see below). need accomplish same encryption using .net.
public class encryptionserviceimpl { private static final charset utf8 = charset.forname("utf-8"); @resource(name = "briqueauthentificationclient") private briqueauthentificationclientimpl briqueauthentificationclient; protected static final string algorithm_rsa = "rsa"; protected static final string transformation_rsa_ecb_pkcs1padding = "rsa/ecb/pkcs1padding"; private static final logger log = loggerfactory.getlogger(encryptionserviceimpl.class); public encryptionserviceimpl() { log.info("constructeur encryptionserviceimpl"); } /** * @param briqueauthentificationclient briqueauthentificationclient set */ public void setbriqueauthentificationclient(final briqueauthentificationclientimpl briqueauthentificationclient) { this.briqueauthentificationclient = briqueauthentificationclient; } public string encrypt(final string input) throws generalsecurityexception { if (stringutils.isnotblank(input)) { final certificatedto certificate = this.briqueauthentificationclient.getcurrentcertificate(); if (certificate != null) { homecoming new string(this.encryptandencode(input.getbytes(), certificate), encryptionserviceimpl.utf8); } else { throw new runtimeexception("certificate null"); } } homecoming null; } protected byte[] encryptandencode(final byte[] input, final certificatedto currentcertificate) throws generalsecurityexception { // création de la clé publique final publickey publickey = this.buildpublickey(currentcertificate); // chiffre final byte[] inputencrypted = this.encrypte(input, publickey); // encode homecoming this.encodebase64url(inputencrypted); } protected publickey buildpublickey(final certificatedto currentcertificate) throws generalsecurityexception { if ("rsa".equals(currentcertificate.getkeytype())) { homecoming this.buildrsapublickey(currentcertificate); } log.error(string.format("tentative de création d'une clé publique avec united nations algorithme non connu [%s]", currentcertificate.getkeytype())); homecoming null; } protected publickey buildrsapublickey(final certificatedto currentcertificate) throws generalsecurityexception { final biginteger modulus = new biginteger(new string(base64.decodebase64(currentcertificate.getmodulus()), encryptionserviceimpl.utf8)); final biginteger publicexponent = new biginteger(new string(base64.decodebase64(currentcertificate .getpublicexponent()), encryptionserviceimpl.utf8)); seek { homecoming keyfactory.getinstance(algorithm_rsa).generatepublic(new rsapublickeyspec(modulus, publicexponent)); } grab (invalidkeyspecexception e) { throw e; } grab (nosuchalgorithmexception e) { throw e; } } protected byte[] encrypte(final byte[] input, final rsapublickeyspec rsapublickeyspec) throws generalsecurityexception { publickey publickey; seek { publickey = keyfactory.getinstance(algorithm_rsa).generatepublic( new rsapublickeyspec(rsapublickeyspec.getmodulus(), rsapublickeyspec.getpublicexponent())); } grab (invalidkeyspecexception e) { throw e; } grab (nosuchalgorithmexception e) { throw e; } homecoming this.encrypte(input, publickey); } protected byte[] encrypte(final byte[] input, final publickey publickey) throws generalsecurityexception { seek { final cipher cipher = cipher.getinstance(transformation_rsa_ecb_pkcs1padding); cipher.init(cipher.encrypt_mode, publickey); homecoming cipher.dofinal(input); } grab (nosuchalgorithmexception e) { throw e; } grab (nosuchpaddingexception e) { throw e; } grab (illegalblocksizeexception e) { throw e; } grab (badpaddingexception e) { throw e; } } protected byte[] decrypte(final byte[] input, final rsaprivatekeyspec rsaprivatekeyspec) throws generalsecurityexception { final biginteger modulus = rsaprivatekeyspec.getmodulus(); final biginteger privateexponent = rsaprivatekeyspec.getprivateexponent(); seek { final privatekey privatekey = keyfactory.getinstance(algorithm_rsa).generateprivate( new rsaprivatekeyspec(modulus, privateexponent)); final cipher cipher = cipher.getinstance(transformation_rsa_ecb_pkcs1padding); cipher.init(cipher.decrypt_mode, privatekey); homecoming cipher.dofinal(input); } grab (nosuchalgorithmexception e) { throw e; } grab (nosuchpaddingexception e) { throw e; } grab (illegalblocksizeexception e) { throw e; } grab (badpaddingexception e) { throw e; } grab (invalidkeyspecexception e) { throw e; } grab (invalidkeyexception e) { throw e; } } protected byte[] encodebase64url(final byte[] input) { homecoming base64.encodebase64(input, false); } protected byte[] decodebase64url(final byte[] input) { homecoming base64.decodebase64(input); } /** * method connect url * * @param httpclient http connection * @return response getmethod * @throws oauthexception in cas of connection error */ private getmethod connect(final httpclient httpclient, final string url) { final getmethod httpget = new getmethod(url); seek { httpclient.executemethod(httpget); } grab (final unknownhostexception e) { throw new runtimeexception("connection error - host not determined.", e); } grab (final ioexception e) { throw new runtimeexception("connection error - input/output error.", e); } homecoming httpget; } }
the steps accomplished help of owlstead in reply below. when utilize java programme encode string password01 obtain string like:
sy5/xelhvuya4rbq8obydlymt82r+z77jy6mu2snal7venzpebargy7p3zwgygg13cypk+zbnnb5l37fvuhgogcqcyltikzxjbnmpyzuk9+beihjkyonzwifdzq44hxtexcz0bmf9b5dlfy9qs/n5m28viybsgy8k2b7eb2ff38=
this encrypted password can decrypted on java side
when utilize .net code string like:
aczxyj/kudyxkbb510sxsoukavwssmeum6jpreh8jttrih9egb18gidkbu7rxzmulybahyrebflbr87zw/2dna4tn97folqr6k1jppj/sss/9fgdmvsoaqbwjsxksdh7fru9dcvk0m0exftgfxg7yua9bb1m0dtniwczuzm0lnem
this encrypted password failed decrypted on java side. failed error message:
javax.crypto.illegalblocksizeexception: info must not longer 128 bytes
you first need perform base of operations 64 decoding using convert.frombase64string
of n
, e
, convert result ascii encoding string , parse result using biginteger.parse
. can convert using tobytearray
bytes, beware biginteger
little endian, and rsaparameters
expects big endian, have reverse bytes.
your .net rsa ciphertext (which shouldn't reverse) preceded 00h valued byte, makes invalid ciphertext. rsa ciphertext must of exact length in bytes of key.
c# .net encryption
No comments:
Post a Comment