travist / jsencrypt

A zero-dependency Javascript library to perform OpenSSL RSA Encryption, Decryption, and Key Generation.
http://www.travistidwell.com/jsencrypt
Other
6.65k stars 2.01k forks source link

hello,there is something bothering me.can u help me out .the backserver language is java,and they decrypted the message with pkcs8. what else should i do with jsencrypt.js. thank you #79

Open liusong2013 opened 8 years ago

dqmmpb commented 8 years ago

js client

  var encrypt = new JSEncrypt();
  encrypt.setPublicKey(RSA_KEY);
  var encrypted = encrypt.encrypt('Javascript');
  console.log('Js encrypted:' + encrypted);

java

    public static final Provider pro = new BouncyCastleProvider();

    private static final String charSet = "UTF-8";

    // key seed
    private static final String seedKey = "prepay_seed_1!";

    public static KeyPair generateKeyPair() throws Exception {
        KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA", pro);
        kpg.initialize(1024, new SecureRandom(seedKey.getBytes()));
        KeyPair kp = kpg.generateKeyPair();

        return kp;
    }
    public static PublicKey getPublicRSAKey(String modulus, String exponent) throws Exception {
        RSAPublicKeySpec spec = new RSAPublicKeySpec(new BigInteger(modulus, 16), new BigInteger(exponent, 16));
        KeyFactory kf = KeyFactory.getInstance("RSA", pro);
        return kf.generatePublic(spec);
    }

    public static PublicKey getPublicRSAKey(String key) throws Exception {
        X509EncodedKeySpec x509 = new X509EncodedKeySpec(Base64.decode(key));
        KeyFactory kf = KeyFactory.getInstance("RSA", pro);
        return kf.generatePublic(x509);
    }

    public static PrivateKey getPrivateRSAKey(String key) throws Exception {
        PKCS8EncodedKeySpec pkgs8 = new PKCS8EncodedKeySpec(Base64.decode(key));
        KeyFactory kf = KeyFactory.getInstance("RSA", pro);
        return kf.generatePrivate(pkgs8);
    }

    public static byte[] encrypt(String input, PublicKey publicKey) throws Exception {
        Cipher cipher = Cipher.getInstance("RSA/None/PKCS1Padding", pro);
        cipher.init(Cipher.ENCRYPT_MODE, publicKey);
        byte[] re = cipher.doFinal(input.getBytes(charSet));
        return re;
    }

    public static byte[] decrypt(byte[] encrypted, PrivateKey privateKey) throws Exception {
        Cipher cipher = Cipher.getInstance("RSA/None/PKCS1Padding", pro);
        cipher.init(Cipher.DECRYPT_MODE, privateKey);
        byte[] re = cipher.doFinal(encrypted);
        return re;
    }
    public static void main(String[] args) throws Exception {

        PublicKey publicKey = getPublicRSAKey(pbkStr);
        PrivateKey privateKey = getPrivateRSAKey(prkStr);

        byte[] jsen = Base64.decode(JSencrypted);
        re = decrypt(jsen, privateKey);
        System.out.println("Java decrypted:" + new String(re, charSet));

    }

you only need get cipher instance with RSA/None/PKCS1Padding in Java.

jik1992 commented 7 years ago

@dqmmpb i test it , copy jsencrypt ' text , report exception

Exception in thread "main" javax.crypto.BadPaddingException: unknown block type at org.bouncycastle.jcajce.provider.asymmetric.rsa.CipherSpi.engineDoFinal(Unknown Source) at javax.crypto.Cipher.doFinal(Cipher.java:2087) at com.tech84.util.RSADemo2.decrypt(RSADemo2.java:77) at com.tech84.util.RSADemo2.main(RSADemo2.java:100) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:606) at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)

dqmmpb commented 7 years ago

@jik1992 sorry, i only post a part of my code, here is my rsa-demo, a maven project, i think it can help you

issacnitin commented 6 years ago

I'm facing the same problem.. is there a solution? Decryption happens in pkcs8 format..

zeno-io commented 6 years ago

@dqmmpb still errors, javax.crypto.BadPaddingException: unknown block type

dqmmpb commented 6 years ago

@SvenAugustus you can try this demo rsa-demo