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/

你可能感兴趣的:(加密算法)