2017-08-17 48 views
0

我進口從Java公鑰到Python我使用插座RSA Python的加密郵件從導入的Java公鑰

在Java中,我使用RSA/ECB/PKCS1Padding

,並在Python我使用Crypto

在變量server_public_key即時導入了公鑰

cipher我使用PKCS1_OAEP加密消息

ciphertext

我加密消息

然後我將其轉換爲字節組

,然後我把它回Java

但Java將這個錯誤Exception in thread "main" javax.crypto.BadPaddingException: Decryption error

這裏是我的代碼

message = "SENDING TO JAVA" 
s= socket.socket() 
    s.connect((address,9000)) 
     data = s.recv(1024) 
     data = data[2:] 
     server_public_key = RSA.importKey(data) 
     cipher = PKCS1_OAEP.new(server_public_key) 
     ciphertext = cipher.encrypt(mensaje) 
     b = bytearray() 
     b.extend(ciphertext) 
     b = bytearray() 
     b.extend(ciphertext) 
     s.sendall(b)  
+0

您應該在Java端使用OAEP填充。嘗試在java端使用'RSA/ECB/OAEPWithSHA-1AndMGF1Padding'。 –

+0

我得到一個BadPaddingException:解密錯誤@JamesKPolk – blazedosan002

+0

請顯示您的代碼。 –

回答

0

當我移動這是發送密文的莫名其妙的第二個副本,並清理並更正了python代碼,它適用於我。這是我使用的Python代碼。

import socket 
import struct 

from Crypto.PublicKey import RSA 
from Crypto.Cipher import PKCS1_OAEP 

# 
# the following is an alternative recvall function that can be used 
# if your platform does not provide the MSG_WAITALL socket flag. 
# 
def recvall2(s, size): 
    received_chunks = [] 
    buf_size = 4096 
    remaining = size 
    while remaining > 0: 
     received = s.recv(min(remaining, buf_size)) 
     if not received: 
      raise Exception('unexcepted EOF') 
     received_chunks.append(received) 
     remaining -= len(received) 
    return b''.join(received_chunks) 

def recvall(s, size): 
    return s.recv(size, socket.MSG_WAITALL) 

def oaep_example(): 
    message = b"SENDING TO JAVA" 
    s = socket.socket() 
    s.connect(('127.0.0.1', 9000)) 
    pubkey_size = struct.unpack(">H", recvall(s, 2))[0] 
    pubkey_der = recvall(s, pubkey_size) 
    server_public_key = RSA.importKey(pubkey_der) 
    cipher = PKCS1_OAEP.new(server_public_key) 
    cipher_text = cipher.encrypt(message) 
    s.sendall(cipher_text) 
    s.close() 

if __name__ == '__main__': 
    oaep_example() 

和小Java服務器來證明這是

import com.google.common.io.ByteStreams; 

import javax.crypto.Cipher; 
import java.io.ByteArrayOutputStream; 
import java.io.DataOutputStream; 
import java.net.ServerSocket; 
import java.net.Socket; 
import java.security.KeyPair; 
import java.security.KeyPairGenerator; 

public class Main { 

    public static void main(String[] args) throws Exception { 
     KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA"); 
     kpg.initialize(1024); 
     KeyPair rsaKeyPair = kpg.generateKeyPair(); 
     ServerSocket serverSocket = new ServerSocket(9000); 
     Socket socket = serverSocket.accept(); 
     DataOutputStream dos = new DataOutputStream(socket.getOutputStream()); 
     byte[] encodedPubKey = rsaKeyPair.getPublic().getEncoded(); 
     dos.writeShort(encodedPubKey.length); 
     dos.write(encodedPubKey); 
     byte[] cipher = ByteStreams.toByteArray(socket.getInputStream()); 
     socket.close(); 
     Cipher c = Cipher.getInstance("RSA/ECB/OAEPWithSHA-1AndMGF1Padding"); 
     c.init(Cipher.DECRYPT_MODE, rsaKeyPair.getPrivate()); 
     byte[] plain = c.doFinal(cipher); 
     System.out.println(new String(plain, StandardCharsets.UTF_8)); 
    } 
} 

注:ByteStreams來自谷歌Guava庫。