Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

CSharp to Java RSA Conversion

Options
  • 04-08-2011 3:25pm
    #1
    Registered Users Posts: 1,686 ✭✭✭


    Hi all,

    I am trying to convert a code from c# to java but I am so lost with java, well a little.

    The problem:
    in C# I can manually put in what I want.
    byte[] key = //array
    
    RSAParameters RSAkey = new RSAParameters();
    RSAkey.D = key;
    RSAkey.Modulus = 
    RSAkey.P =
    RSAkey.Q =
    RSAkey.DP = 
    RSAkey.DQ =
    

    In java I dont seem to have that option and I don't want to generate random keys.

    Thanks in advance.


Comments

  • Registered Users Posts: 7,157 ✭✭✭srsly78


    Library dependent.


  • Registered Users Posts: 1,686 ✭✭✭RealistSpy


    srsly78 wrote: »
    Library dependent.

    what Library ?


  • Registered Users Posts: 1,686 ✭✭✭RealistSpy


    This is the Library/Package that I need : Link
    but how do I import it?


  • Registered Users Posts: 7,157 ✭✭✭srsly78


    import org.cheez.burger;

    Your questions are extremely vague. Specify what IDE etc you are using.


  • Registered Users Posts: 1,311 ✭✭✭Procasinator


    You don't need additional libraries, it is all built into Java. You just got too look for the correct interfaces/classes.

    For instance, look at the constructor for this class:
    http://download.oracle.com/javase/1.4.2/docs/api/java/security/spec/RSAPrivateCrtKeySpec.html


  • Advertisement
  • Registered Users Posts: 1,686 ✭✭✭RealistSpy


    You don't need additional libraries, it is all built into Java. You just got too look for the correct interfaces/classes.

    For instance, look at the constructor for this class:
    http://download.oracle.com/javase/1.4.2/docs/api/java/security/spec/RSAPrivateCrtKeySpec.html
    i don't know how I miss that well I know but [FacePalm]

    Anyways c# Code
    RSAParameters RSAkey = new RSAParameters();
    RSAkey.D = key;
    RSAkey.Modulus = 
    RSAkey.P =
    RSAkey.Q =
    RSAkey.DP = 
    RSAkey.DQ =
    
    //System.Security.Cryptography.RSAParameters
    

    Java Code representation
    RSAkey.D = PrivateExponent
    RSAkey.Modulus = Modulus
    RSAkey.P = PrimeP
    RSAkey.Q = PrimeQ
    RSAkey.DP = PrimeExponentP
    RSAkey.DQ = PrimeExponentQ
    RSAkey.Exponent = PublicExponent
    RSAkey.InverseQ = CrtCoeficent
    
    //java.security.spec.RSAPrivateCrtKeySpec
    


  • Registered Users Posts: 1,686 ✭✭✭RealistSpy


    Hi,
    I am back again :)
    How can I recode this to java
    public static byte[] signatureGenerate(RSAParameters param, byte[] hash)
            {
                RSACryptoServiceProvider cryptoRSA = new RSACryptoServiceProvider();
                RSAPKCS1SignatureFormatter sigFormatRSA = new RSAPKCS1SignatureFormatter();
                cryptoRSA .ImportParameters(param); 
    
                sigFormatRSA .SetHashAlgorithm("SHA1");
                sigFormatRSA .SetKey(cryptoRSA );
    

    This is what I have so far
    public static byte[] SignatureGenerate(RSAPrivateCrtKeySpec param, byte[] hash)
        {
            try
            {
                //works but I want to use RSAPrivateCrtKeySpec  and I dont want to generate one
                KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
                
                KeyPair keyPair = kpg.generateKeyPair();
                PrivateKey priv1 = keyPair.getPrivate();
                
                Signature signature1 = Signature.getInstance("SHA1withRSA");
                signature1.initSign(priv1);
                signature1.update(hash);
                
                return signature1.sign();
    
                //Dont work
                //KeyFactory kf = KeyFactory.getInstance("RSA");
                //PrivateKey priv = kf.generatePrivate(param);
                
                //Signature signature = Signature.getInstance("SHA1withRSA");
                //signature.initSign(priv);
                
                //return signature.sign();
            }
    


  • Registered Users Posts: 1,994 ✭✭✭lynchie


    I think you can use the KeyFactory class to generate a PrivateKey instance from your RSAPrivateCrtKeySpec which you can then use in the initSign() method.
    KeyFactory kf = KeyFactory.getInstance("RSA");
    PrivateKey myPrivateKey = kf.generatePrivate(param);
    


  • Registered Users Posts: 1,686 ✭✭✭RealistSpy


    lynchie wrote: »
    I think you can use the KeyFactory class to generate a PrivateKey instance from your RSAPrivateCrtKeySpec which you can then use in the initSign() method.
    KeyFactory kf = KeyFactory.getInstance("RSA");
    PrivateKey myPrivateKey = kf.generatePrivate(param);
    

    Doesn't work. Data cannot sign error.


  • Registered Users Posts: 1,686 ✭✭✭RealistSpy


    My goal is to:
    Generate a PKS1 Signature of SHA1 digest(hash)
    using specified RSAParameters (param) to generate a private key
    (param) contains: BigInteger
    D = PrivateExponent
    Modulus = Modulus
    P = PrimeP
    Q = PrimeQ
    DP = PrimeExponentP
    DQ = PrimeExponentQ
    Exponent = PublicExponent
    InverseQ = CrtCoeficent ????

    I manage to get everything working fine in c# but I cant seem to get it working on java. Java generates a key but I get error (Cannot sign Data) when I try to sign the signature.

    If I use random key generation in java I get no error but the file gets corrupted


  • Advertisement
  • Registered Users Posts: 1,311 ✭✭✭Procasinator


    Make sure you are creating the key correctly. Test with known working inputs for each of the RSAPrivateCrtKeySpec.

    Also, consider using RSAPrivateKeySpec as you probably only need the private exponent and the modulus anyhow.


  • Registered Users Posts: 1,686 ✭✭✭RealistSpy


    Make sure you are creating the key correctly. Test with known working inputs for each of the RSAPrivateCrtKeySpec.

    Also, consider using RSAPrivateKeySpec as you probably only need the private exponent and the modulus anyhow.

    Still can't get it to sign. I will keep trying fingers crossed


Advertisement