2016-08-30 15 views
11

tôi cố gắng để mã hóa một số dữ liệu trong matlab sử dụng một khóa công khai tôi tạo ra với opensslKhông thể giải mã dữ liệu RSA với SSL mở

Tôi tạo ra các phím sử dụng:

openssl genrsa -des3 -out private.pem 1024 
openssl rsa -in private.pem -pubout -outform DER -out public.der 

tôi mã hóa dữ liệu của tôi sử dụng mã này matlab (với các thư viện Java):

import java.security.spec.RSAPublicKeySpec 
import javax.crypto.Cipher; 
import java.security.KeyFactory 
import java.math.BigInteger 

fid = fopen('public.der'); 
a = fread(fid); 
key = java.security.spec.X509EncodedKeySpec(a); 
kf = KeyFactory.getInstance('RSA'); 
KEY = kf.generatePublic(key); 

cipher = Cipher.getInstance('RSA/ECB/PKCS1Padding'); 
cipher.init(Cipher.ENCRYPT_MODE, KEY) 

plaintextBytes = [24]; 
ciphertext = cipher.doFinal(plaintextBytes)' ; 

fid2 = fopen('msg.txt','w'); 
fwrite(fid2,ciphertext); 
fclose(fid2); 

tôi cố gắng để giải mã nó bằng cách sử:

openssl rsautl -decrypt -inkey private.pem -in msg.txt -keyform PEM -pkcs 

Sau đó, tôi nhận được lỗi này:

RSA operation error 
80305:error:0407109F:rsa routines:RSA_padding_check_PKCS1_type_2:pkcs decoding error:/BuildRoot/Library/Caches/com.apple.xbs/Sources/OpenSSL098/OpenSSL098-59.40.2/src/crypto/rsa/rsa_pk1.c:267: 
80305:error:04065072:rsa routines:RSA_EAY_PRIVATE_DECRYPT:padding check failed:/BuildRoot/Library/Caches/com.apple.xbs/Sources/OpenSSL098/OpenSSL098-59.40.2/src/crypto/rsa/rsa_eay.c:614: 

Trả lời

2

Phần lớn thời gian cho như vậy "lỗi RSA_padding_check_PKCS1_type_2 ..." - bạn có xu hướng thấy điều này với (1) lỗi Encoding: thay vì giải mã dữ liệu nhị phân, giải mã được thực hiện trên (có thể) dữ liệu được mã hóa Base64. (2) Cặp khóa hoặc khóa không khớp: Khóa công khai không khớp với khóa Riêng tư để giải mã. http://hustoknow.blogspot.ca/2013/01/rsa-block-type-is-not-02-error.html

Có lẽ chúng ta có thể đảm bảo rằng cặp không khớp (2) trước khi cho biết tải mật mã không chính xác (1). Giống như dưới đây trong ref để https://www.sslshopper.com/ssl-converter.html

Chuyển đổi PEM thành DER: openssl x509 -outform der -in certificate.pem -out certificate.der hoặc nếu cert đã có định dạng "der", cũng có thể được chuyển thành "pem " ví dụ Chuyển đổi DER thành PEM: openssl x509 -inform der -in certificate.cer -out certificate.pem

Chuyển đổi PEM thành PFX: openssl pkcs12 -export -out certificate.pfx -inkey privateKey.key -in certificate.crt -certfile CACert.crt hoặc nếu chỉ có "pfx" thì có thể nhận "pem" ví dụ: Chuyển đổi PFX để PEM: openssl pkcs12 -in certificate.pfx out certificate.cer -nodes

Sau khi thực hiện bảo chúng tôi có "pem", chúng tôi có thể thử các mã hóa và giải mã như đã nêu trong http://openssl.6102.n7.nabble.com/unable-to-decrypt-using-using-private-key-td15204.html

ví dụ 1) openssl enc -base64 -d -in -out được tạo và có nội dung nhị phân. ví dụ: 2) openssl rsautl -decrypt -inkey -out -pkcs nhưng trong trường hợp này, hãy xem xét cố gắng sử dụng -raw thay vì -pkcs để giải mã dữ liệu với máy chủ riêng khóa

Các vấn đề liên quan