从C#中的X,Y参数生成ECPublicKey

时间:2016-07-28 00:31:35

标签: c# cryptography

我试图将在java中实现的安全代码移植到C#。 在java中使用了spongycastle,因此我使用等效的bouncy城​​堡用于c#但是我无法在.Net中获得以下库文件的等效实现,这些库文件在下面提到的示例代码中使用。有人可以指向我在county城堡中的正确库文件c#或.Net

import java.security.interfaces.ECPrivateKey;
import java.security.interfaces.ECPublicKey;
import java.security.spec.ECParameterSpec;
import java.security.spec.ECPoint;
import java.security.spec.ECPublicKeySpec;

All i'm using in these files is 

public static ECPublicKey createECPublicKeyFromXY(ECParameterSpec paramtrs,      final byte[] pubkey)
{
    int keySizeBytes = paramtrs.getOrder().bitLength() / Byte.SIZE;

    int offset = 1;
    BigInteger x = new BigInteger(1, Arrays.copyOfRange(pubkey, offset,
            offset + keySizeBytes));

    offset += keySizeBytes;
    BigInteger y = new BigInteger(1, Arrays.copyOfRange(pubkey, offset,
           offset + keySizeBytes));

    ECPoint w = new ECPoint(x, y);

    ECPublicKeySpec otherKeySpec = new ECPublicKeySpec(w, paramtrs);

    try {
        KeyFactory keyFactory = KeyFactory.getInstance("ECDH", "SC");
        return (ECPublicKey) keyFactory.generatePublic(otherKeySpec);

    }
    catch (Exception ex) {
        return null;
    }
}

0 个答案:

没有答案