0

I have this binary content of file it's RSA public key generated with Java.

¬í sr java.math.BigIntegerŒüŸ©;û I bitCountI bitLengthI firstNonzeroByteNumI lowestSetBitI signum[ magnitudet [Bxr java.lang.Number†¬•”à‹ xpÿÿÿÿÿÿÿÿÿÿÿþÿÿÿþ ur [B¬óøTà xp °^¾ùŠÖÂá}ÈþŽ–†ÁÃêsÀ»púŒaré…íè8NKŒ lH réöˆMj'cmQë>À?y‘Ǩj£ã<ʇ²ÞƒÕ4<Qga÷£#I“’[ØÜ€äºbwYòEûŸ1™àô©ñº…ýÙ£?³â+¶E @>ò̆ëÜ"nàú¾Ñt”²:r { gÁ:ÿò¶ÌHZ`®âàtG;÷ŠzýIô÷c×ä5—Mx¾+Ë3Û#3>c¶jŒ GvbD¿SGI.˜ã;™sC¾ÝÇôôJ õ½-‹î”2ò÷…®‡¢ZƒtÃÑ@âÉÑ/|¹²5ñ’µZcxsq ~ ÿÿÿÿÿÿÿÿÿÿÿþÿÿÿþ uq ~ x

how to get Modulus and exponent from this content using c# and dotnet framework (not core).

Yes, It is a Java public key. I could not found any function to read this key and get thye RSAparameters.

I used this Java code to get the Modulus and exponenet with success. but when use it in c# to create public key I always get "Parameter Incorrect" on windows 2012 server.

Java Code

BigInteger modulo = null, exposant = null;
try {
    ObjectInputStream ois = new ObjectInputStream(new BufferedInputStream(new FileInputStream(nomFichier)));        
    modulo = (BigInteger) ois.readObject();
    exposant = (BigInteger) ois.readObject();
} catch(IOException e) {
    System.err.println("Erreur lors de la lecture de la clé : " + e);
    System.exit(-1);
} catch(ClassNotFoundException e) {
    System.err.println("Fichier de clé incorrect : " + e);
    System.exit(-1);
}

PublicKey clePublique = null;
try {
    RSAPublicKeySpec specification = new RSAPublicKeySpec(modulo, exposant);
    KeyFactory usine = KeyFactory.getInstance("RSA");
    clePublique = usine.generatePublic(specification);
} catch(NoSuchAlgorithmException e) {
    System.err.println("Algorithme RSA inconnu : " + e);
    System.exit(-1);
} catch(InvalidKeySpecException e) {
    System.err.println("Spécification incorrecte : " + e);
    System.exit(-1);
}
return clePublique;

c# code

CspParameters parms = new CspParameters();
        parms.Flags = CspProviderFlags.NoFlags;
        parms.KeyContainerName = Guid.NewGuid().ToString().ToUpperInvariant();
        parms.ProviderType = ((Environment.OSVersion.Version.Major > 5) || ((Environment.OSVersion.Version.Major == 5) && (Environment.OSVersion.Version.Minor >= 1))) ? 0x18 : 1;
        // Modulus and Exponent are from Java
        BigInteger modulusInt = BigInteger.Parse("22264662665581503581958564757951433642071042765180847835607911485241107337546440921333481060699040617948699619143852514883961242740412934026407809467632924273132875711353979878192151539324021302381664216461707890134565390428705286450284770084749935209949622966373476605854194876749604260497977835251836811598929536162941397609744200129752240619098969605506158549850626894587090735501922815967991426385372742427057780208805955531394006831299305270496208054419166650641070163691999735656859181256967560193451396805797552205282299420633106177676710034732661564145564177377055134205294304271572837276495701834203634424419");
        BigInteger ExponentInt = BigInteger.Parse("65537");

        csp = new RSACryptoServiceProvider(parms);
        _publicKey = new RSAParameters();
        _publicKey = csp.ExportParameters(false);
        byte[] exponent = ExponentInt.ToByteArray();
        byte[] modulus = modulusInt.ToByteArray();
        _publicKey.Modulus = modulus;
        _publicKey.Exponent = exponent;
        csp.PersistKeyInCsp = true;
        csp.ImportParameters(_publicKey);
        byte[] token = csp.Encrypt(Convert.FromBase64String(txtPrivateKey.Text), true);

Could you please help me to read the binary Java key and import parameters to my RSA with c# (because the key every month will be changed).

this is Modulus and exponenet Base64 Modulus key: Y1q1kvE1srkafC/RyeJA0cN0GRqDWqKHrguF9/IylO6LLb31AEr0DvTHARl/3b5Dcw6ZO+OYLklHU79EYnZHCYxqtgtjPjMOI9sYM8srvnhNlzXk12P39En9eor3O0d04JDirmBaSMwBtoHy/zrBZyANe6ByOrKUdNG++uBuItwd64bM8j4RQBUgRbYr4rM/o9n9hbrxqfTgmTGf+0XyWRt3YrrkgNwX2FuSGpNJI6MR939hZ1E8NNWDj96yh8oDPOOjagKox5F5P4/APutRbWMnak2I9ulyIEhsCRiMS0446O2F6XJhF4z6cLvAc+rDwYaWAo7+yH3hwtaK+b5esAA=

Exponent key: AQAB

the error I got it on the Windows server 2012 R2 is from here

csp.ImportParameters(_publicKey);

this function work on Windows 10

Bennani
  • 1
  • 1
  • is doing something like this not working for you https://learn.microsoft.com/en-us/dotnet/api/system.security.cryptography.rsacryptoserviceprovider?view=net-5.0 ? or https://stackoverflow.com/questions/15702718/public-key-encryption-with-rsacryptoserviceprovider – avikalb Jul 19 '21 at 23:25
  • 1
    Can you post your key base 64 encoded, please – zaitsman Jul 19 '21 at 23:55
  • 1
    Looks like a serialized Java public key to me. Deserialize it using Java and then convert it to a SubjectPublicKeyInfo structure by calling `getEncoded()`. – Maarten Bodewes Jul 19 '21 at 23:59
  • @Bennani you are doing yourself a disservice not adding Java to the tags. – AsPas Jul 20 '21 at 10:03
  • I added this Java code to show that in java we can read this bin content. if there are an equivalent code in c# it is welcome. – Bennani Jul 20 '21 at 11:55
  • https://stackoverflow.com/questions/12827744/ikvm-net-object-serialization-with-objectoutputstream-differences Looks like Java's object serialization is unstable between versions and there are no other ways to deserialize it except using the same version of Java VM which serialized it. – yume_chan Jul 20 '21 at 15:44

0 Answers0