javascript和java使用pubKey加密消息

时间:2017-03-28 06:55:55

标签: java encryption rsa

在RSA中,我知道pubKey,我想使用pubKey来加密javascript和java中的消息,但结果不对。 JavaScript代码如下:

    function getEntryptPwd(pwd){
    var pubKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDC7kw8r6tq43pwApYvkJ5laljaN9BZb21TAIfT/vexbobzH7Q8SUdP5uDPXEBKzOjx2L28y7Xs1d9v3tdPfKI2LR7PAzWBmDMn8riHrDDNpUpJnlAGUqJG9ooPn8j7YNpcxCa1iybOlc2kEhmJn5uwoanQq+CA6agNkqly2H4j6wIDAQAB";
    if(!pwd || !pubKey || !SysConfig.encryptInfo){
        return pwd;
    }
    var encrypt = new JSEncrypt();
    encrypt.setPublicKey(pubKey);
    return encrypt.encrypt(pwd);
}

我使用getEntryptPwd("123456").length方法,结果是172。 java代码如下:

        String publicKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDC7kw8r6tq43pwApYvkJ5laljaN9BZb21TAIfT/vexbobzH7Q8SUdP5uDPXEBKzOjx2L28y7Xs1d9v3tdPfKI2LR7PAzWBmDMn8riHrDDNpUpJnlAGUqJG9ooPn8j7YNpcxCa1iybOlc2kEhmJn5uwoanQq+CA6agNkqly2H4j6wIDAQAB";
    KeyFactory keyFactory = KeyFactory.getInstance("RSA");
    byte[] encodedKey = Base64.decode(publicKey);
    PublicKey pubKey = keyFactory.generatePublic(new X509EncodedKeySpec(encodedKey));

    byte[] encrypt = encrypt(pubKey, "123456".getBytes("utf-8"));

    System.out.println(new BASE64Encoder().encode(encrypt).length());

但是,输出结果为176。 一定有什么不对,我找不到

1 个答案:

答案 0 :(得分:0)

您似乎正在使用旧的import android.support.v4.app.*; import android.view.*; import android.os.*; public class page1 extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View v=inflater.inflate(R.layout.halaman_1, container,false); return v; } } ,并且正在添加一些额外的换行符。将其替换为sun.misc.Base64Encoder以获得172

的输出
java.util.Base64