gpt4 book ai didi

java - 跨平台 RSA 加密 C# 到 Java 和 Java 到 C#

转载 作者:行者123 更新时间:2023-12-02 02:18:21 26 4
gpt4 key购买 nike

我在 Windows 表单应用程序中生成了大小为 1024 的 RSA key 。测试应用程序的代码如下:

namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
private static CspParameters cspParameters;
const string keyName = "TestKey1";
private static RSACryptoServiceProvider devrsa;
private RSAParameters publicKey;
private RSAParameters privateKey;

public Form1()
{
InitializeComponent();
}

private void BtnGenerate_Click(object sender, EventArgs e)
{
cspParameters = new CspParameters();

cspParameters.KeyContainerName = keyName;
devrsa = new RSACryptoServiceProvider(1024, cspParameters);
devrsa.PersistKeyInCsp = true;

publicKey = devrsa.ExportParameters(false);
privateKey = devrsa.ExportParameters(true);

var mod = publicKey.Modulus;
var exp = publicKey.Exponent;

var pubKey = new PublicKey
{
modulus = Array.ConvertAll(mod, b => unchecked((sbyte)b)),
exponent = Array.ConvertAll(exp, b => unchecked((sbyte)b)),
};

var sPubKey = JsonConvert.SerializeObject(pubKey);

var bytePubKey = Encoding.ASCII.GetBytes(sPubKey);

PrintByteArray(bytePubKey);
}


public void PrintByteArray(Byte[] bytes)
{
var sb = new StringBuilder("new byte[] { ");
foreach (var b in bytes)
{
sb.Append(b + ", ");
}
sb.Append("}\n");
Console.WriteLine(sb.ToString());
}
}

public class PublicKey
{
[JsonProperty("modulus")]
public SByte[] modulus { get; set; }
[JsonProperty("exponent")]
public SByte[] exponent { get; set; }
public PublicKey()
{

}
public PublicKey(SByte[] modulus, SByte[] exponent)
{
this.modulus = modulus;
this.exponent = exponent;
}
}

我获取了控制台打印出的字节数组的副本,并将其用作我的测试 Android 应用程序的输入。测试 Android 应用程序应该将公钥作为输入,并使用导入的公钥来加密示例文本字符串。

Android应用程序的代码如下:

class MainActivity : AppCompatActivity() {


override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

setContentView(R.layout.activity_main)

retrieveElements()
}


fun retrieveElements() {
val request = ByteHelper.pubKey
var key = ByteHelper.retrieveKey(request)

val cipher = createCipher(key)

encrypt(cipher)

}

fun createCipher(publicKey: PublicKey): Cipher {
val modulus = BigInteger(publicKey.modulus)
val exponent = BigInteger(publicKey.exponent)

var spec = RSAPublicKeySpec(modulus, exponent)
var fact = KeyFactory.getInstance(KeyProperties.KEY_ALGORITHM_RSA)

var publicKey = fact.generatePublic(spec)

var cipher = Cipher.getInstance("RSA/ECB/OAEPWithSHA-256AndMGF1Padding")
cipher.init(Cipher.ENCRYPT_MODE, publicKey)
return cipher
}

fun encrypt(cipher: Cipher)
{
val testString = "This is a test string"

val stringBytes = testString.toByteArray(StandardCharsets.US_ASCII)

val encryptedBytes = cipher.doFinal(stringBytes)

}
}

class ByteHelper
{
companion object
{
val pubKey = byteArrayOf( 123, 34, 109, 111, 100, 117, 108, 117, 115, 34, 58, 91, 45, 55, 49, 44, 45, 51, 50, 44, 45, 55, 44, 45, 49,
48, 55, 44, 45, 51, 54, 44, 45, 51, 54, 44, 51, 54, 44, 45, 56, 44, 45, 51, 48, 44, 45, 49, 50, 53, 44, 45, 52, 44, 49,
48, 48, 44, 45, 54, 49, 44, 45, 56, 48, 44, 45, 55, 53, 44, 50, 44, 52, 44, 49, 50, 51, 44, 52, 50, 44, 53, 54, 44, 45,
53, 49, 44, 54, 55, 44, 45, 55, 44, 57, 55, 44, 49, 48, 54, 44, 45, 49, 49, 55, 44, 45, 49, 49, 57, 44, 45, 49, 49, 54,
44, 45, 49, 50, 50, 44, 45, 50, 53, 44, 45, 55, 51, 44, 45, 51, 51, 44, 55, 49, 44, 45, 54, 53, 44, 53, 48, 44, 51, 50,
44, 45, 51, 49, 44, 45, 49, 48, 56, 44, 45, 55, 49, 44, 45, 49, 49, 51, 44, 53, 53, 44, 45, 49, 49, 48, 44, 53, 55, 44,
45, 55, 50, 44, 50, 44, 51, 49, 44, 51, 52, 44, 45, 49, 49, 48, 44, 55, 48, 44, 45, 57, 54, 44, 45, 57, 48, 44, 52, 52,
44, 49, 49, 48, 44, 45, 49, 48, 55, 44, 45, 50, 53, 44, 45, 49, 56, 44, 49, 53, 44, 45, 51, 50, 44, 45, 56, 52, 44, 45,
53, 44, 51, 53, 44, 45, 49, 48, 44, 45, 54, 49, 44, 45, 53, 51, 44, 49, 50, 49, 44, 45, 50, 50, 44, 56, 53, 44, 45, 49,
48, 48, 44, 45, 52, 55, 44, 57, 51, 44, 45, 49, 49, 54, 44, 57, 44, 49, 51, 44, 55, 50, 44, 49, 50, 49, 44, 45, 52, 48,
44, 45, 54, 53, 44, 49, 48, 53, 44, 45, 52, 52, 44, 45, 51, 50, 44, 51, 53, 44, 49, 49, 54, 44, 49, 49, 57, 44, 50, 56,
44, 45, 55, 53, 44, 52, 53, 44, 56, 52, 44, 45, 53, 55, 44, 49, 55, 44, 49, 49, 44, 45, 49, 53, 44, 45, 52, 51, 44, 45,
56, 55, 44, 49, 51, 44, 45, 52, 57, 44, 51, 53, 44, 55, 49, 44, 45, 51, 48, 44, 45, 49, 48, 57, 44, 45, 50, 52, 44, 51,
49, 44, 45, 51, 50, 44, 50, 54, 44, 45, 51, 56, 44, 49, 49, 56, 44, 45, 52, 49, 44, 45, 51, 52, 44, 49, 57, 44, 45, 49,
50, 50, 44, 45, 54, 51, 44, 52, 48, 44, 50, 53, 44, 57, 54, 44, 45, 56, 55, 44, 52, 53, 44, 45, 53, 48, 44, 45, 53, 54,
44, 45, 55, 48, 44, 45, 49, 48, 55, 44, 45, 56, 54, 44, 52, 55, 44, 49, 48, 53, 44, 45, 51, 44, 45, 53, 57, 44, 53, 52,
44, 45, 49, 50, 49, 44, 56, 52, 44, 45, 51, 93, 44, 34, 101, 120, 112, 111, 110, 101, 110, 116, 34, 58, 91, 49, 44, 48,
44, 49, 93, 125)

fun retrieveKey(bytes: ByteArray) : PublicKey
{
var jsonString = String(bytes, Charset.forName("US-ASCII"))

var publicKey = Gson().fromJson(jsonString, PublicKey::class.java)

return publicKey
}
}
}

data class PublicKey(
@SerializedName("modulus") var modulus: ByteArray,
@SerializedName("exponent") var exponent: ByteArray
) {
constructor() : this(ByteArray(0), ByteArray(0))
}

但是,当我运行 Android 版本的应用程序时。我收到以下错误。

com.android.org.bouncycastle.crypto.DataLengthException: input too large for RSA cipher.
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2666)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2727)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1478)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6121)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:889)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:779)
Caused by: com.android.org.bouncycastle.crypto.DataLengthException: input too large for RSA cipher.
at com.android.org.bouncycastle.crypto.engines.RSACoreEngine.convertInput(RSACoreEngine.java:115)
at com.android.org.bouncycastle.crypto.engines.RSABlindedEngine.processBlock(RSABlindedEngine.java:95)
at com.android.org.bouncycastle.crypto.encodings.OAEPEncoding.encodeBlock(OAEPEncoding.java:199)
at com.android.org.bouncycastle.crypto.encodings.OAEPEncoding.processBlock(OAEPEncoding.java:131)
at com.android.org.bouncycastle.jcajce.provider.asymmetric.rsa.CipherSpi.engineDoFinal(CipherSpi.java:475)
at javax.crypto.Cipher.doFinal(Cipher.java:2056)
at com.touchsides.myapplication.MainActivity.encrypt(MainActivity.kt:54)

最佳答案

在 createCipher 函数中,问题与 ByteArray 到 BigInteger 的转换有关。

val modulus = BigInteger(1, publicKey.modulus)
val exponent = BigInteger(1, publicKey.exponent)

var spec = RSAPublicKeySpec(modulus, exponent)
var fact = KeyFactory.getInstance(KeyProperties.KEY_ALGORITHM_RSA)

var publicKey = fact.generatePublic(spec)

var cipher = Cipher.getInstance("RSA/ECB/OAEPWithSHA-1AndMGF1Padding")
cipher.init(Cipher.ENCRYPT_MODE, publicKey)

为了允许在另一端解密,需要更改密码实例:

RSA/ECB/OAEPWithSHA-256AndMGF1Padding => RSA/ECB/OAEPWithSHA-1AndMGF1Padding

关于java - 跨平台 RSA 加密 C# 到 Java 和 Java 到 C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57288592/

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