RSA JS encrypted Java decryption

Source: Internet
Author: User

1. First you must have a pair of public key, private key; "' Pubkeystr =" migfma0gcsqgsib3dqebaquaa4gnadcbiqkbgqc1gr+rifylanunlifsk/ knb54nqrcrtrmodujtwkpqklo4pnyj2stmrywetefxocftct/7ixturu2rggjkiolyc3144h0djkdtpxw9+ Mfyw1vwwvtfoisuektebz1tshghecdevvq6qlsqukilaezabiwfee30tq6g979x6yxhnqidaqab ";p rikeystr =" Miiceqibadanbgkqhkig9w0baqefaascammwggjfageaaogbalwcv6sh9ivo1q0uiwwr8qdvnidcsjfnew526npcsmooujik1ipzk2avjyrn4xe4iw0k3 /ulfnsttzeyaoqg6vglfxjihr0koo09fd36yxjbvxba+1+ ijjr4pmrvpw1iecerx0s9wrqqvjc6sisarlpulb8qtfrndqd3v1fphegdagmbaaecgyearmr1w3zfcxoxpvitjuuv589akl/ rs7temygomdqzrel1cplczrxinsmvq3otlzja5genncpx2eyunl7ydf+t2wgcvpplm+turbnti9gkywlxiwpuwou8ozolhatuqqulfnjtz+ 6cyjha3s4xnrjaevvs8xgy6/uajwxupm/dqaecqqdlw9lporwaavfuzqftvtwhtjhvmbz/aeuxcmmmf2kbgaujbxjuvhve5rzw9/ 6tqowinuk4j/uolcfl3vttepgzakeaypggjwgyq3uz0kx8yschv/ guza56l5grofmripiek4uvt2jm1kqgiq9t9pionv7dz7burk2gxi3glgeuhdr2jqjbajpaypz7qmbfdnkdosyoqztdegcr+ fqj3azrq0m3knr4gy0nlz8jw4qgjmkdcjmvkhdh+dae1ywzx7icmof6hgkcqqcto+ Lkiivx7growahi5j5lbvtwbqcyepbhbx8z5z8pj1mwwxxdbmtk4gc9momw1qwwqg9bdiqvfgw+2n2bv5xbakeahuaidfoywrib3vuzwtqeilvfqxhd8a9acelltgijk8wiu9gngifkwuvoveoq0gcftvqwo5tlac+ oedhccimlyq== ";"
The generation method is slightly;

2. JS Encryption * Gets the public keyexponent/modulus for use in JS"' Publickey.getpublicexponent (). ToString ();p ublickey.getmodulus (). ToString (16); ' * ' * Dependent package address http:// www.ohdave.com/rsa/introduction of dependent files "var rsa_m =" b582bfab21f62568d50d2e216c2bf2a76f9e2742b0914d130e76e8d3c24a6a28ba38a4d623d92b66af25844de17138216d0adffb8b14d4ad4d911868e 420e9580b7d78e21d1d24a0ed3d7c3dfa61725b55705afb5fa2249478a4c46f3d6d48782111c744bd5abaaa5490ba488b00465a6e2c1f104df44d0ea0    f7bf57e985e19d ";    var rsa_e = "10001"; Setmaxdigits (131); 131 = hexadecimal digits of n =/2+3 var key = new Rsakeypair (rsa_e, ', rsa_m); E,m is obtained from Java code.
var password = "123456"; Password = encryptedstring (key, password); Do not support kanji//pass password to the background, use Java decryption in the background "
3. Java decryption because JS encryption uses the nopadding mode (the same as every encryption result), and Java default is PADDING15, each encryption result is different, so rely on third-party package to decrypt *//http://mvnrepository.com /artifact/org.bouncycastle/bcprov-jdk15on compile group: ' Org.bouncycastle ', Name: ' Bcprov-jdk15on ', version: ' 1.54 ' * /' ' Rsautil.decryptjsrsa (password);
"Java Code Tool Class: ' Package Com.wch.encrypt; import java.security.keyfactory;import Java.security.PrivateKey; Import Java.security.interfaces.rsapublickey;import Java.security.spec.pkcs8encodedkeyspec;import Java.security.spec.x509encodedkeyspec;import Java.util.base64; import Javax.crypto.cipher; import Org.bouncycastle.jce.provider.bouncycastleprovider;import Org.bouncycastle.util.encoders.hex; import com.netfinworks.util.rsa; /** * RSA Tool class. Provides encryption, decryption, and generation of key equivalence methods. Bcprov-jdk16-140.jar package is required. * JS Dependent http://www.ohdave.com/rsa/*//Http://mvnrepository.com/artifact/org.bouncycastle/bcprov-jdk15on Compile Group: ' Org.bouncycastle ', Name: ' Bcprov-jdk15on ', version: ' 1.54 '   */public class Rsautil {    private static final String pubkeystr = "migfma0gcsqgsib3dqebaquaa4gnadcbiqkbgqc1gr+rifylanunlifsk/ knb54nqrcrtrmodujtwkpqklo4pnyj2stmrywetefxocftct/7ixturu2rggjkiolyc3144h0djkdtpxw9+ Mfyw1vwwvtfoisuektebz1tshghecdevvq6qlsqukilaezabiwfee30tq6g979x6yxhnqidaqab ";     private static final String Prikeystr = " Miiceqibadanbgkqhkig9w0baqefaascammwggjfageaaogbalwcv6sh9ivo1q0uiwwr8qdvnidcsjfnew526npcsmooujik1ipzk2avjyrn4xe4iw0k3 /ulfnsttzeyaoqg6vglfxjihr0koo09fd36yxjbvxba+1+ ijjr4pmrvpw1iecerx0s9wrqqvjc6sisarlpulb8qtfrndqd3v1fphegdagmbaaecgyearmr1w3zfcxoxpvitjuuv589akl/ rs7temygomdqzrel1cplczrxinsmvq3otlzja5genncpx2eyunl7ydf+t2wgcvpplm+turbnti9gkywlxiwpuwou8ozolhatuqqulfnjtz+ 6cyjha3s4xnrjaevvs8xgy6/uajwxupm/dqaecqqdlw9lporwaavfuzqftvtwhtjhvmbz/aeuxcmmmf2kbgaujbxjuvhve5rzw9/ 6tqowinuk4j/uolcfl3vttepgzakeaypggjwgyq3uz0kx8yschv/ guza56l5grofmripiek4uvt2jm1kqgiq9t9pionv7dz7burk2gxi3glgeuhdr2jqjbajpaypz7qmbfdnkdosyoqztdegcr+ fqj3azrq0m3knr4gy0nlz8jw4qgjmkdcjmvkhdh+dae1ywzx7icmof6hgkcqqcto+ lkiivx7growahi5j5lbvtwbqcyepbhbx8z5z8pj1mwwxxdbmtk4gc9momw1qwwqg9bdiqvfgw+ 2n2bv5xbakeahuaidfoywrib3vuzwtqeilvfqxhd8a9acelltgijk8wiu9gngifkwuvoveoq0gcftvqwo5tlac+oedhccimlyq== ";     /**     * Decrypt JS encrypted rsa     *      * @param str     *             JS Encrypted string;     *             JS Encryption Reference http://blog.csdn.net/songxiaobing/article/details     *            /17505237     * @return       */    public static string Decryptjsrsa (String str) {        Privatekey privatekey;        Try {             Privatekey = Rsa.getprivatekey (prikeystr);             Cipher Cipher = cipher.getinstance ("rsa/none/nopadding",                     New BouncycastleproVider ());            Cipher.init (Cipher.DECRYPT_MODE, Privatekey);            StringBuilder sb = new StringBuilder (New String (Cipher.dofinal (hex                    . Decode (str)));             return Sb.reverse (). toString ();       } catch (Exception e) { & nbsp;          e.printstacktrace ();        }        return null;   }    / **     * Native plus decryption code      *      * @throws exception      */    public static void Rsatest () throws Exception {        Keyfactory factory = keyfactory.getinstance ("RSA",                 new Bouncycastleprovider ());          byte[] bytes = Base64.getdecoder (). Decode (PUBKEYSTR);         X509ENCODEDKEYSPEC spec = new X509encodedkeyspec (bytes);        Rsapublickey PublicKey = (rsapublickey) factory.generatepublic (spec);        System.out.println (Publickey.getpublicexponent (). toString ());        System.out.println (Publickey.getmodulus (). toString ());         byte[] Pirvatebytes = Base64.getdecoder (). Decode (PRIKEYSTR);        Pkcs8encodedkeyspec Privatespec = new Pkcs8encodedkeyspec (pirvatebytes);        Privatekey Privatekey= Factory.generateprivate (Privatespec);         byte[] encode = RSA.encrypt ("WCh". GetBytes (), Privatekey);        System.out.println (Base64.getencoder () . encodetostring (encode));        System.out.println (New String (Rsa.decrypt ( Encode, PublicKey)));         byte[] Publicencode = RSA.encrypt ("pubic". GetBytes (), PublicKey);        System.out.println (Base64.getencoder (). Encodetostring (Publicencode));        System.out.println (New String ( Rsa.decrypt (Publicencode, Privatekey)));         byte[] sign = RSA.sign (" WCh ". GetBytes (), Privatekey);        System.out.println (Base64.getencoder (). Encodetostring (sign));         System.out.println (rsa.verify ("WCh"). GetBytes (),Sign, PublicKey));   }     public static void Main (string[] args) throws Exception {         rsatest ();   } } "    


From for notes (Wiz)

RSA JS encrypted Java decryption

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.