Xamarin.Android绑定Spongy Castle / Bouncy Castle

时间:2016-07-01 13:57:39

标签: android xamarin xamarin.android bouncycastle spongycastle

有人成功绑定SpongyCastle给Xamarin.Android吗?我在绑定项目中使用Metadata.xml碰到了一堆警告。

到目前为止,我有:

<remove-node path="/api/package[@name='org.spongycastle.x509']" />
<remove-node path="/api/package[@name='org.spongycastle.crypto']" />
<remove-node path="/api/package[@name='org.spongycastle.crypto.tls']" />
<remove-node path="/api/package[@name='org.spongycastle.cms']" />
<remove-node path="/api/package[@name='org.spongycastle.crypto.prng']" />
<remove-node path="/api/package[@name='org.spongycastle.openpgp']" />
<remove-node path="/api/package[@name='org.spongycastle.openssl']" />
<remove-node path="/api/package[@name='org.spongycastle.cert.ocsp']" />

<remove-node path="/api/package[@name='org.spongycastle.jcajce']" />
<remove-node path="/api/package[@name='org.spongycastle.jcajce.provider.asymmetric.dh']" />
<remove-node path="/api/package[@name='org.spongycastle.jcajce.provider.asymmetric.ec']" />
<remove-node path="/api/package[@name='org.spongycastle.jcajce.provider.digest']" />
<remove-node path="/api/package[@name='org.spongycastle.jcajce.provider.keystore.bc']" />
<remove-node path="/api/package[@name='org.spongycastle.jcajce.provider.symmetric']" />

<remove-node path="/api/package[@name='org.spongycastle.jcajce.provider.asymmetric.dsa']" />
<remove-node path="/api/package[@name='org.spongycastle.jcajce.provider.asymmetric.util']" />
<remove-node path="/api/package[@name='org.spongycastle.jcajce.provider.symmetric.util']" />
<remove-node path="/api/package[@name='org.spongycastle.jcajce.provider.asymmetric.gost']" />
<remove-node path="/api/package[@name='org.spongycastle.jcajce.provider.asymmetric.ies']" />
<remove-node path="/api/package[@name='org.spongycastle.jcajce.provider.asymmetric.rsa']" />
<remove-node path="/api/package[@name='org.spongycastle.jcajce.provider.asymmetric.x509']" />

<remove-node path="/api/package[@name='org.spongycastle.jce.provider']/class[@name='CertStoreCollectionSpi']" />
<remove-node path="/api/package[@name='org.spongycastle.jce.provider']/class[@name='MultiCertStoreSpi']" />
<remove-node path="/api/package[@name='org.spongycastle.jce.provider']/class[@name='X509CRLEntryObject']" />
<remove-node path="/api/package[@name='org.spongycastle.jce.provider']/class[@name='X509CRLObject']" />
<remove-node path="/api/package[@name='org.spongycastle.jce.provider']/class[@name='X509CertificateObject']" />
<remove-node path="/api/package[@name='org.spongycastle.jce.provider']/class[@name='X509LDAPCertStoreSpi']"/>
<remove-node path="/api/package[@name='org.spongycastle.jce.provider']/class[@name='PKIXPolicyNode']" />

<remove-node path="/api/package[@name='org.spongycastle.pqc.jcajce.provider.rainbow']" />
<remove-node path="/api/package[@name='org.spongycastle.pqc.jcajce.provider.mceliece']"/>
<remove-node path="/api/package[@name='org.spongycastle.pqc.jcajce.provider.util']" />
<remove-node path="/api/package[@name='org.spongycastle.pqc.crypto.ntru']" />
<remove-node path="/api/package[@name='org.spongycastle.pqc.math.ntru.polynomial']" />

所以它编译,但在Xamarin.Android项目中使用绑定项目时需要几分钟。编译然后它失败抱怨Java的HEAP大小。

当我将堆大小设置为1GB时,它会完成,但在设备上以调试模式运行应用程序时会调试。

有没有办法在没有绑定库的情况下使用ARR?我只需要调用我在此ARR中创建的包装器方法并从中获取输出。我不需要通过C#访问完整的库。或者有更好的方法吗?

更新: 当我构建CPU时看起来像这样(看看Java): enter image description here

2 个答案:

答案 0 :(得分:1)

截至ARR,您的意思是AAR吗?如果只使用某些商品,您可以直接使用JNIhttps://developer.xamarin.com/guides/android/advanced_topics/java_integration_overview/working_with_jni/

最简单的方法是<remove-node>所有你不想保留的项目,并处理你想要保留的项目。但是,您可能需要确保保留依赖项,以便它们正确绑定。这很快就会变得讨厌。

我有一个通用指南可能会对这种绑定的某些方面有所帮助:

https://github.com/JonDouglas/xamarin-support-docs/blob/master/Android/android-bindings-troubleshooting.md

正如@jzeferino所提到的那样,你总是可以选择使用比经过自己的绑定更有争议的东西。

BouncyCastle-PCL:https://github.com/onovotny/BouncyCastle-PCL

PCL加密:https://github.com/AArnott/PCLCrypto

这两种方法都提供了相应的加密方法来完成您的任务。对于PCLCrypto,它们要么由Mono的实现提供,要么由平台的实现提供。

答案 1 :(得分:0)

我最终在绑定项目的metadata.xml文件中删除了与BouncyCastle和SpongyCastle相关的几乎所有内容。然后我从Binding Project中生成的files文件夹中复制了部分生成的BouncyCastleProvider。所以我只为我需要调用的部分创建了自己的JNI包装器。

效果很好。现在编译时间减少到几秒钟,并且在调试期间部署很快。我喜欢第三方图书馆。

我从中获取的是首先生成C#类,然后选择在我自己的库中实现JNI的一部分,并在Metadata.xml中添加/删除节点。

using System;
using System.Collections.Generic;
using Android.Runtime;
namespace Org.Spongycastle.Jce.Provider
{

// Metadata.xml XPath class reference: path="/api/package[@name='org.spongycastle.jce.provider']/class[@name='BouncyCastleProvider']"
[global::Android.Runtime.Register("org/spongycastle/jce/provider/BouncyCastleProvider", DoNotGenerateAcw = true)]
public sealed partial class BouncyCastleProvider : global::Java.Security.Provider
{
    // Metadata.xml XPath field reference: path="/api/package[@name='org.spongycastle.jce.provider']/class[@name='BouncyCastleProvider']/field[@name='PROVIDER_NAME']"
    [Register("PROVIDER_NAME")]
    public const string ProviderName = (string)"SC";

    internal static IntPtr java_class_handle;
    internal static IntPtr class_ref
    {
        get
        {
            return JNIEnv.FindClass("org/spongycastle/jce/provider/BouncyCastleProvider", ref java_class_handle);
        }
    }

    protected override IntPtr ThresholdClass
    {
        get { return class_ref; }
    }

    protected override global::System.Type ThresholdType
    {
        get { return typeof(BouncyCastleProvider); }
    }

    internal BouncyCastleProvider(IntPtr javaReference, JniHandleOwnership transfer) : base(javaReference, transfer) { }

    static IntPtr id_ctor;
    // Metadata.xml XPath constructor reference: path="/api/package[@name='org.spongycastle.jce.provider']/class[@name='BouncyCastleProvider']/constructor[@name='BouncyCastleProvider' and count(parameter)=0]"
    [Register(".ctor", "()V", "")]
    public unsafe BouncyCastleProvider()
        : base(IntPtr.Zero, JniHandleOwnership.DoNotTransfer)
    {
        if (Handle != IntPtr.Zero)
            return;

        try
        {
            if (GetType() != typeof(BouncyCastleProvider))
            {
                SetHandle(
                        global::Android.Runtime.JNIEnv.StartCreateInstance(GetType(), "()V"),
                        JniHandleOwnership.TransferLocalRef);
                global::Android.Runtime.JNIEnv.FinishCreateInstance(Handle, "()V");
                return;
            }

            if (id_ctor == IntPtr.Zero)
                id_ctor = JNIEnv.GetMethodID(class_ref, "<init>", "()V");
            SetHandle(
                    global::Android.Runtime.JNIEnv.StartCreateInstance(class_ref, id_ctor),
                    JniHandleOwnership.TransferLocalRef);
            JNIEnv.FinishCreateInstance(Handle, class_ref, id_ctor);
        }
        finally
        {
        }
    }
}
}