使用bouncy castle和python PKCS1-OAEP进行java RSA加密。

时间:2020-12-31 18:33:17

For over a week, I've been using this to implement a RSA secured communication with my server that is in python.

在过去的一个多星期里,我一直在使用这个工具来实现与python中的服务器的RSA安全通信。

However, I can not for the life of me figure out the proper imports for all the jars.

然而,我不能为我的生活找到合适的进口所有的罐子。

I have included all the jars found on bouncy castles website and still no dice!

我已经把所有在bouncy城堡网站上找到的罐子都包括进去了,仍然没有骰子!

I read that they moved stuff around. If this code is old or broken, what other implementation of RSA with pkcs1 padding is put there?

我读到他们把东西搬来搬去。如果这段代码是旧的或坏的,那么使用pkcs1填充的RSA还有什么其他的实现呢?

EDIT:

编辑:

pub key is in a file named key.pub. how do I read that file in to be used as the key

pub key在一个名为key.pub的文件中。我如何读取该文件作为键?

-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAv2B0wo+QJ6tCqeyTzhZ3
AtPLgAHEQ/fRYDcR0BkQ+lXEhD277P2fPZwla5AW6szqsjR1olkZEF7IuoI27Hxm
tQHJU0ROhrzstHgK42emz5Ya3BWcm+oq5pLDZnsNDnNlrPncaCT7gHQQJn3YjH8q
aibtB1WCoy7ZJ127QxoKoLfeonBDtt7Qw6P5iXE57IbQ63oLq1EaYUfg8ZpADvJF
b2H3MASJSSDrSDgrtCcKAUYuu3cZw16XShuKCNb5QLsj3tR0QC++7qjM3VcG311K
7gHVjB6zybw+5vX2UWTgZuL6WVtCvRK+WY7nhL3cc5fmXZhkW1Jbx6wLPK3K/JcR
NQIDAQAB
-----END PUBLIC KEY-----

EDIT 2: Adding Broken Code based on an answer

编辑2:根据答案添加破坏的代码。

package Main;


import org.bouncycastle.util.encoders.Base64;

import javax.crypto.Cipher;
import javax.xml.bind.DatatypeConverter;
import java.io.*;
import java.net.Socket;
import java.net.UnknownHostException;
import java.security.GeneralSecurityException;
import java.security.KeyFactory;
import java.security.PublicKey;
import java.security.Security;
import java.security.spec.X509EncodedKeySpec;

public class EncDecRSA {
    public static byte[] pemToDer(String pemKey) throws GeneralSecurityException {
        String[] parts = pemKey.split("-----");
        return DatatypeConverter.parseBase64Binary(parts[parts.length / 2]);
    }

    public static PublicKey derToPublicKey(byte[] asn1key) throws GeneralSecurityException {
        X509EncodedKeySpec spec = new X509EncodedKeySpec(asn1key);
        KeyFactory keyFactory = KeyFactory.getInstance("RSA", "BC");
        return keyFactory.generatePublic(spec);
    }

    public static byte[] encrypt(PublicKey publicKey, String text) throws GeneralSecurityException {
        Cipher rsa = Cipher.getInstance("RSA/ECB/OAEPWithSHA1AndMGF1Padding", "BC");//PKCS1-OAEP
        rsa.init(Cipher.ENCRYPT_MODE, publicKey);
        byte[] cipher =  rsa.doFinal(text.getBytes());
        String s = new String(cipher);
        System.out.print(s);
//        return cipher;
//        return Base64.encode(rsa.doFinal(text.getBytes()));
        cipher = Base64.encode(cipher);
        return cipher;

    }

    static String readFile(String path)
            throws IOException
    {
        String line = null;
        BufferedReader br = new BufferedReader(new FileReader(path));
        try {
            StringBuilder sb = new StringBuilder();
            line = br.readLine();

            while (line != null) {
                sb.append(line);
                sb.append("\n");
                line = br.readLine();
            }
            return sb.toString();
        } finally {
            br.close();

        }

    }
    public static void main(String[] args) throws IOException, GeneralSecurityException {
        Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());

        System.out.println("Working Directory = " +
                System.getProperty("user.dir"));
        String publicKey = readFile("key.public");
        byte[] pem = pemToDer(publicKey);
        PublicKey myKey = derToPublicKey(pem);
        String sendMessage = "{'vid_hash': '917ef7e7be4a84e279b74a257953307f1cff4a2e3d221e363ead528c6b556edb', 'state': 'ballot_response', 'userInfo': {'ssn': '700-33-6870', 'pin': '1234', 'vid': '265jMeges'}}";
        byte[] encryptedDATA = encrypt(myKey, sendMessage);
        Socket smtpSocket = null;
        DataOutputStream os = null;
        DataInputStream is = null;
        try {
            smtpSocket = new Socket("192.168.1.124", 9999);
            os = new DataOutputStream(smtpSocket.getOutputStream());
            is = new DataInputStream(smtpSocket.getInputStream());
        } catch (UnknownHostException e) {
            System.err.println("Don't know about host: hostname");
        } catch (IOException e) {
            System.err.println("Couldn't get I/O for the connection to: hostname");
        }

        if (smtpSocket != null && os != null && is != null) {
            try {
                System.out.println("sending message");
                os.writeBytes(encryptedDATA+"\n");
                os.close();
                is.close();
                smtpSocket.close();
            } catch (UnknownHostException e) {
                System.err.println("Trying to connect to unknown host: " + e);
            } catch (IOException e) {
                System.err.println("IOException:  " + e);
            }
        }
    }
}

Which has the following error:

有以下错误:

使用bouncy castle和python PKCS1-OAEP进行java RSA加密。

Here is the bit of python code that does that:

下面是一些python代码:

def _decrypt_RSA(self, private_key_loc, package):
    '''
    param: public_key_loc Path to your private key
    param: package String to be decrypted
    return decrypted string
    '''
    from Crypto.PublicKey import RSA 
    from Crypto.Cipher import PKCS1_OAEP 
    from base64 import b64decode 
    key = open('key.private', "r").read() 
    rsakey = RSA.importKey(key) 
    rsakey = PKCS1_OAEP.new(rsakey) 

    decrypted = rsakey.decrypt(package)
    # decrypted = rsakey.decrypt(b64decode(package)) 
    return decrypted

Lastly:

最后:

Is this padding scheme with PKCS1-OAEP.

这是PKCS1-OAEP的填充方案。

That is a major requirement for this to work.

这是这项工作的主要要求。

Note that I have tried it with both base64 encode and without

注意,我已经用base64编码和没有进行过测试。

1 个解决方案

#1


1  

This example is for SpongyCastle - Android repackage of BouncyCastle. They differ as you noticed. However, making encryption in Java is as simple as this

这个例子是关于“海绵城堡”的,它是BouncyCastle的安卓系统。他们的区别就像你注意到的一样。然而,在Java中进行加密就像这样简单。

Convert Base64 encoded key string to ASN.1 DER encoded byte array

将Base64编码的密钥字符串转换为ASN.1 DER编码的字节数组。

public static byte[] pemToDer(String pemKey) throws GeneralSecurityException {
    String[] parts = pemKey.split("-----");
    return DatatypeConverter.parseBase64Binary(parts[parts.length / 2]);
}

Convert ASN.1 DER encoded public key to PublicKey object

转换ASN.1 DER编码的公钥给PublicKey对象。

public static PublicKey derToPublicKey(byte[] asn1key) throws GeneralSecurityException {
    X509EncodedKeySpec spec = new X509EncodedKeySpec(asn1key);
    KeyFactory keyFactory = KeyFactory.getInstance("RSA", "BC");
    return keyFactory.generatePublic(spec);
}

Encrypt data

加密数据

public static byte[] encrypt(PublicKey publicKey, String text) throws GeneralSecurityException {
    Cipher rsa = Cipher.getInstance("RSA/ECB/OAEPWithSHA1AndMGF1Padding", "BC");
    rsa.init(Cipher.ENCRYPT_MODE, publicKey);
    return rsa.doFinal(text.getBytes());
}

bcprov-jdk15on-150.jar is the only jar needed.

bcprov - jdk15on - 150。jar是唯一需要的罐子。

#1


1  

This example is for SpongyCastle - Android repackage of BouncyCastle. They differ as you noticed. However, making encryption in Java is as simple as this

这个例子是关于“海绵城堡”的,它是BouncyCastle的安卓系统。他们的区别就像你注意到的一样。然而,在Java中进行加密就像这样简单。

Convert Base64 encoded key string to ASN.1 DER encoded byte array

将Base64编码的密钥字符串转换为ASN.1 DER编码的字节数组。

public static byte[] pemToDer(String pemKey) throws GeneralSecurityException {
    String[] parts = pemKey.split("-----");
    return DatatypeConverter.parseBase64Binary(parts[parts.length / 2]);
}

Convert ASN.1 DER encoded public key to PublicKey object

转换ASN.1 DER编码的公钥给PublicKey对象。

public static PublicKey derToPublicKey(byte[] asn1key) throws GeneralSecurityException {
    X509EncodedKeySpec spec = new X509EncodedKeySpec(asn1key);
    KeyFactory keyFactory = KeyFactory.getInstance("RSA", "BC");
    return keyFactory.generatePublic(spec);
}

Encrypt data

加密数据

public static byte[] encrypt(PublicKey publicKey, String text) throws GeneralSecurityException {
    Cipher rsa = Cipher.getInstance("RSA/ECB/OAEPWithSHA1AndMGF1Padding", "BC");
    rsa.init(Cipher.ENCRYPT_MODE, publicKey);
    return rsa.doFinal(text.getBytes());
}

bcprov-jdk15on-150.jar is the only jar needed.

bcprov - jdk15on - 150。jar是唯一需要的罐子。