gpt4 book ai didi

java - Python 和 Java 之间的 RSA 加密填充问题

转载 作者:搜寻专家 更新时间:2023-11-01 08:44:33 27 4
gpt4 key购买 nike

我正在尝试使用由 android 中的客户端生成的公钥对 python 中的某些数据进行 RSA 加密。加密成功(至少没有异常(exception))。但是,当尝试在 android 中使用私钥解密数据时,它会抛出错误的填充异常。

相关代码:

在 Android 中生成 RSA key 对。 Bouncy caSTLe 的精简版包含在 android 中:

import javax.crypto.KeyGenerator;

KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
kpg.initialize(2048);
return kpg.generateKeyPair();

Python 中的加密:

from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_OAEP
import base64
from Crypto.Hash import SHA256

# key contains the public key string without any line breaks.
def encrypt(keyString, plainText)
key = key.replace('-----BEGIN PUBLIC KEY-----', '')
key = key.replace('-----END PUBLIC KEY-----', '');
pubkey = RSA.importKey(base64.b64decode(key))
cipher = PKCS1_OAEP.new(pubkey, hashAlgo=SHA256)
encrypted = cipher.encrypt(plaintext)
return base64.b64encode(encrypted)

print encrypt('-----BEGIN PUBLIC KEY-----MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA7fJDt9my7WTnPahyw9JvIwm/q3F9jF9R2qNv01Os5NhBUgWOha67DlalOhQmQYBosnXfTWm6JQF2upnBInFGUOZx+10UVnuUbTpUT004lBfNbHx23rrf66b48Q25OoDnbWM3xWD8+9GPvoLO3ftJSbcjb+euzVJOkJYNo33dnG0yZ4LWHLBMog7OgbnjjobiM+O/0wzmiW8VE7gCrg7ZyGpxHXo9ACJytKjC4Hg976Ryg1wyrD2dmgiV212CcBGdzj7F6xn1Uyk7DTeAOgQJHLDFr+2sHgcXJXyslrjvCp3Om2CbzDY9W8XaU2A/84Q1Ejt8ljtJZBOJCuH+ARqZRwIDAQAB-----END PUBLIC KEY-----', 'this is a test string')

Android 中的解密。 android 中包含 Bouncy caSTLe 的精简版:

相关进口:

import javax.crypto.Cipher;
import javax.crypto.spec.OAEPParameterSpec;
import javax.crypto.spec.PSource;
import java.security.spec.MGF1ParameterSpec;
import java.util.*;
import android.util.Base64;

解密:

private static String RSA_CONFIGURATION = "RSA/ECB/OAEPWithSHA-256AndMGF1Padding";
private static String RSA_PROVIDER = "BC";

public static decrypt(Key key, String encryptedString){
Cipher c = Cipher.getInstance(RSA_CONFIGURATION, RSA_PROVIDER);
c.init(Cipher.DECRYPT_MODE, key, new OAEPParameterSpec("SHA-256", "MGF1", MGF1ParameterSpec.SHA1,
PSource.PSpecified.DEFAULT));
byte[] decodedBytes = c.doFinal(Base64.decode(base64cypherText.getBytes("UTF-8"), Base64.DEFAULT));
clearText = new String(decodedBytes, "UTF-8");
}

异常:

javax.crypto.BadPaddingException: data hash wrong
at com.android.org.bouncycastle.jcajce.provider.asymmetric.rsa.CipherSpi.engineDoFinal(CipherSpi.java:478)
at javax.crypto.Cipher.doFinal(Cipher.java:1204)
at org.ambientdynamix.security.CryptoUtils.decryptRsa(CryptoUtils.java:204)

测试键:

-----BEGIN PRIVATE KEY-----MIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKkwggSlAgEAAoIBAQDt8kO32bLtZOc9qHLD0m8jCb+rcX2MX1Hao2/TU6zk2EFSBY6FrrsOVqU6FCZBgGiydd9NabolAXa6mcEicUZQ5nH7XRRWe5RtOlRPTTiUF81sfHbeut/rpvjxDbk6gOdtYzfFYPz70Y++gs7d+0lJtyNv567NUk6Qlg2jfd2cbTJngtYcsEyiDs6BueOOhuIz47/TDOaJbxUTuAKuDtnIanEdej0AInK0qMLgeD3vpHKDXDKsPZ2aCJXbXYJwEZ3OPsXrGfVTKTsNN4A6BAkcsMWv7aweBxclfKyWuO8Knc6bYJvMNj1bxdpTYD/zhDUSO3yWO0lkE4kK4f4BGplHAgMBAAECggEBAOJGCeOhoCdkVwopT8msKYeWU7DDYfLFCl/yH/tEjRuqEqPfKOMzgCpodwM9+gs8A1QCB3HkYuGh/LgCUslgEtAH9MhmgVqkdkdQur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S1s/YN8gJDhFySAVsUVCYsKaw==-----END PRIVATE KEY-----

-----BEGIN PUBLIC KEY-----MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA7fJDt9my7WTnPahyw9JvIwm/q3F9jF9R2qNv01Os5NhBUgWOha67DlalOhQmQYBosnXfTWm6JQF2upnBInFGUOZx+10UVnuUbTpUT004lBfNbHx23rrf66b48Q25OoDnbWM3xWD8+9GPvoLO3ftJSbcjb+euzVJOkJYNo33dnG0yZ4LWHLBMog7OgbnjjobiM+O/0wzmiW8VE7gCrg7ZyGpxHXo9ACJytKjC4Hg976Ryg1wyrD2dmgiV212CcBGdzj7F6xn1Uyk7DTeAOgQJHLDFr+2sHgcXJXyslrjvCp3Om2CbzDY9W8XaU2A/84Q1Ejt8ljtJZBOJCuH+ARqZRwIDAQAB-----END PUBLIC KEY-----

最佳答案

OAEP 填充有两个参数:

  • 消息摘要算法
  • 掩码生成函数

我看到在 Java 中您使用 SHA-256 和 MGF1 以及 SHA-1 哈希。它们必须与 Python 使用的参数不匹配。

Python 和 Java 的区别在于 Python 代码使用相同的摘要进行消息摘要和掩码摘要。而 Java(Sun 提供者和 BouncyCaSTLe 提供者)默认使用 SHA-1 进行掩码摘要。因此需要明确指定所有参数:

byte[] cipherTextBytes = DatatypeConverter.parseBase64Binary(cipherText);
byte[] privateKeyBytes = DatatypeConverter.parseBase64Binary(privateKeyStr);

KeyFactory kf = KeyFactory.getInstance("RSA");
PKCS8EncodedKeySpec ks = new PKCS8EncodedKeySpec(privateKeyBytes);
PrivateKey privateKey = kf.generatePrivate(ks);

Cipher c = Cipher.getInstance("RSA/ECB/OAEPWithSHA-256AndMGF1Padding");
c.init(Cipher.DECRYPT_MODE, privateKey, new OAEPParameterSpec("SHA-256",
"MGF1", MGF1ParameterSpec.SHA256, PSource.PSpecified.DEFAULT));
byte[] plainTextBytes = c.doFinal(cipherTextBytes);
String plainText = new String(plainTextBytes);

System.out.println(plainText);

关于java - Python 和 Java 之间的 RSA 加密填充问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29668262/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com