转换两个字符串 - > PublicKey& Base64加密它

时间:2016-07-07 05:23:18

标签: java encryption base64 rsa steam

所以我试图连接到一个网站服务(Steam),它要求程序有一个Base64加密密钥,其中包含我尝试登录的帐户的密码。我有RSA密钥但是使用密码加密它有点困难。这是我的代码:

if(jsonObject.getBoolean("success")){
        String timeStamp = jsonObject.getString("timestamp");
        String publickey_mod = jsonObject.getString("publickey_mod");
        String keyexp = jsonObject.getString("publickey_exp");
        String modulus_preHex = new BigInteger(1, publickey_mod.getBytes("UTF-8")).toString(16);
        String exponent_preHex = new BigInteger(1, keyexp.getBytes("UTF-8")).toString(16);
        BigInteger modulus_post = new BigInteger(modulus_preHex);
        BigInteger exponent_post = new BigInteger(exponent_preHex);
        PublicKey key = getEncrpytedKey(modulus_post, exponent_post);

        Cipher cipher = Cipher.getInstance("RSA/ECB/OAEPWithSHA1AndMGF1Padding");
        cipher.init(Cipher.ENCRYPT_MODE, key);

        byte[] passArray = pass.getBytes("UTF-8");
        String encode = Base64.encodeBase64(cipher.doFinal(passArray)).toString();
        System.out.println(encode + " - encode");


    }

这是在成功发布到我检索RSAKEY(publickey_mod)的URL以及它到期时(keyexp)的POST之后发生的。加密后,PublicKey I Base64对字符串进行编码。

    public PublicKey getEncrpytedKey(BigInteger m, BigInteger e) throws Exception{
        RSAPublicKeySpec keySpec = new RSAPublicKeySpec(m, e);
        KeyFactory fact = KeyFactory.getInstance("RSA");
        PublicKey pubKey = fact.generatePublic(keySpec);
        return pubKey;
}

^^^看到用于获取PublicKey的方法。

当我运行这个程序时,'encode'字符串不会改变,即使我更改了运行的密码,它当前是:

字符串编码= [B@32d992b2

当RSAKey为AB8AA7A6C2513D3CFFCA147F64E7B9C5400CCE5B36D3F881387659A51701D76BB383C1E50F52EF8DE28AD20F6257B8E0013720E4704F6ABDBCC19E477BD58C534A93895408157BE7874152A0153499773BC3AE4E536543A74341D58F7EA9CF74487C9560625BACE2315FB0983BD9E78BD812AE7DDC356427CC7626DCB8D25900658FE5E309D35BF794445F6EF9279882699E1CCBEE2F4193BA2634D0F7B24CFD7EC6A2AA8B35A48969C43DBF759095FCBD087C4D2D8123F598A4F43AECE0CE4E8FA536ECD84782092A329DF96FF88A25677AA386669B4ACAA41D7B6AAD456307357C70E25FD027970F5F0CF952894A065A37BA1BD2222A3AC2CD8FB69632560F

请注意,我并不熟悉加密技术,因为这是我第一次尝试加密。

感谢您的帮助, 添

注意:我正在使用org.apache.commons.codec.binary.Base64;用于Base64。

1 个答案:

答案 0 :(得分:0)

Base64.encodeBase64会返回byte数组,因此您需要的是

encode = new String (Base64.encodeBase64(cipher.doFinal(passArray));