openssl RSA java实现

折腾了很久,使用jdk默认的jsa加密算法,跟andriod无法一致,最终使用openssl解决。 openssl支持andriod,ios,js

而且bouncycastle提供的源码对 了解jca有很大的帮助,,,


第一个版本

package test.com.fdd.openssl;


import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.security.Key;
import java.security.KeyPair;
import java.security.Security;
import java.security.interfaces.RSAPublicKey;
import javax.crypto.Cipher;
import org.apache.commons.codec.binary.Base64;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.openssl.PEMReader;
import org.bouncycastle.openssl.PasswordFinder;


public class Openssl {


    static final String PRIVATE_PATH = "I:\\workspace\\test\\file\\rsa_1024_priv.pem";
    static final String PUBLIC_PATH = "I:\\workspace\\test\\file\\rsa_1024_pub.pem";


    private static byte[] encrypt(Key pubkey, String text) {
        try {
            Cipher rsa;
            rsa = Cipher.getInstance("RSA");
            rsa.init(Cipher.ENCRYPT_MODE, pubkey);
            return rsa.doFinal(text.getBytes());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


    private static String decrypt(Key decryptionKey, byte[] buffer) {
        try {
            Cipher rsa;
            rsa = Cipher.getInstance("RSA");
            rsa.init(Cipher.DECRYPT_MODE, decryptionKey);
            byte[] utf8 = rsa.doFinal(buffer);
            return new String(utf8, "UTF8");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


    private static KeyPair readKeyPair(File privateKey, String keyPassword) throws IOException {
        FileReader fileReader = new FileReader(privateKey);
        PEMReader r = new PEMReader(fileReader, new DefaultPasswordFinder(keyPassword.toCharArray()));
        try {
            return (KeyPair) r.readObject();
        } catch (IOException ex) {
            throw ex;
        } finally {
            r.close();
            fileReader.close();
        }
    }


    private static Key readPublicKey(File privateKey, String keyPassword) throws IOException {
        FileReader fileReader = new FileReader(privateKey);
        PEMReader r = new PEMReader(fileReader, new DefaultPasswordFinder(keyPassword.toCharArray()));
        try {
            return (RSAPublicKey) r.readObject();
        } catch (IOException ex) {
            throw ex;
        } finally {
            r.close();
            fileReader.close();
        }
    }


    private static class DefaultPasswordFinder implements PasswordFinder {


        char result[];


        public DefaultPasswordFinder(char psdarray[]) {
            result = psdarray;
        }


        @Override
        public char[] getPassword() {
            // TODO Auto-generated method stub
            return result;
        }


    }


    public static void main(String[] args) throws IOException {


        Security.addProvider(new BouncyCastleProvider());


        KeyPair keyPair = readKeyPair(new File(PRIVATE_PATH), "pass");
        // if the private key is not encripted, pass can be anything.
        Key publickey = readPublicKey(new File(PUBLIC_PATH), "pass");
        Base64 base64 = new Base64();
        String text = "this is the input text";
        byte[] encripted;
        System.out.println("input:\n" + text);
        encripted = encrypt(publickey, text);
        System.out.println("cipher:\n" + base64.encodeAsString(encripted));
        System.out.println("decrypt:\n" + decrypt(keyPair.getPrivate(), encripted));
    }
}


========================== 以上的代码跟js调过了,但是跟andriod调不通

最终改成 

  rsa = Cipher.getInstance("RSA/ECB/NoPadding");

就可以跟andriod调通了




参考

使用js加密,java解密

http://www.codeproject.com/Articles/198646/Javascript-RSA-Encryption-and-Java-Decryption

简单rsa实现

http://www.java2s.com/Code/Java/Security/SimpleRSApublickeyencryptionalgorithmimplementation.htm

http://www.thedeveloperspoint.com/implementing-rsa-in-java/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值