How to generate auto secret key in asp.net

Last post 03-01-2009 6:32 AM by TATWORTH. 5 replies.

Sort Posts:

  • How to generate auto secret key in asp.net

    02-25-2009, 4:38 AM

    Dear Experts,

      I had one function in converted from java for generate the auto secret key purpose for encrypt and decrypt the using using AES algorithm..when i used this function in .net its showing SecretKeySpec and KeyGenerator  is not defined.Even i used System.Security.Cryptography header name space also.

    I give the my codes.If any body know help me  

     

    Private Function keygeneration() As SecretKeySpec

    Dim skeySpec As SecretKeySpec = Nothing

    Try

    Dim kgen As KeyGenerator = KeyGenerator.getInstance("AES")

    kgen.init(128)

    Dim skey As SecretKey = kgen.generateKey()

    Dim key() As SByte = skey.getEncoded()

    skeySpec = New SecretKeySpec(key, "AES")Catch e As Exception

    Response.Write(e.Message)

    End Try

    Return skeySpec

    End Function

     

     

    Reply me

    Thanx & Regards

    Thanigaimani

     

    Filed under:
  • Re: How to generate auto secret key in asp.net

    02-25-2009, 5:14 AM
    • All-Star
      23,801 point All-Star
    • qwe123kids
    • Member since 03-27-2008, 5:49 AM
    • Posts 4,075

    Hi,


    import java.security.Key;
    import java.security.Security;

    import javax.crypto.Cipher;
    import javax.crypto.KeyGenerator;

    public class MainClass {

      public static void main(String[] args) throws Exception {
        Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());

        KeyGenerator generator = KeyGenerator.getInstance("AES", "BC");
        generator.init(128);

        Key keyToBeWrapped = generator.generateKey();

        System.out.println("input    : " + new String(keyToBeWrapped.getEncoded()));
        Cipher cipher = Cipher.getInstance("AESWrap", "BC");

        KeyGenerator KeyGen = KeyGenerator.getInstance("AES", "BC");
        KeyGen.init(256);

        Key wrapKey = KeyGen.generateKey();
        cipher.init(Cipher.WRAP_MODE, wrapKey);
        byte[] wrappedKey = cipher.wrap(keyToBeWrapped);

        System.out.println("wrapped : " + new String(wrappedKey));

        cipher.init(Cipher.UNWRAP_MODE, wrapKey);
        Key key = cipher.unwrap(wrappedKey, "AES", Cipher.SECRET_KEY);
        System.out.println("unwrapped: " + new String(key.getEncoded()));
      }
    }

    Before That U have Import Java Libary

    Below link may help you

    http://forums.asp.net/t/90461.aspx 

    For importing java Libary.

     

    Hope Below Code Help U

    Thanks,

    Thanks
    Avinash Tiwari

    Remember to click “Mark as Answer” on the post, if it helps you.

    MY Blog

    Hacking Inside .net exe
  • Re: How to generate auto secret key in asp.net

    02-25-2009, 7:28 AM

    Hi

    Thanx for reply.I got it.

    Now i want encrypt the input string using ciper class.I have some codes which is converted from java.I want use this function in .net
    i give the function as follows as

    'Method for encrypt the input value using the secret key

    Private Function Encrypt(ByVal msg As String, ByVal skeySpec As SecretKeySpec) As String

    Dim hexString As String = ""

    Try

    Dim cipher As Cipher = cipher.getInstance("AES")

    cipher.init(cipher.ENCRYPT_MODE, skeySpec)

    Dim encrypted() As SByte = cipher.doFinal(msg.getBytes())

    hexString = asHex(encrypted)

    Catch ex As Exception

    Response.Write(ex.Message)

    End Try

    Return hexString

    End Function

    Here
    msg as input string for example msg="Sample Input Text"
    and secretkeyspec as autogeneratekey for example skeySpec="GbxDwxcjvjtpi0+lQuyfHg=="
    i need encrypt string as "fc8caff8581f9a3920b788490c3569ffb345e54c7f72a73ddb76256ff81fbfd6"
    problem is ciper class doesnt support in .net

    Help me

    Thanx

    Regards
    Mani

  • Re: How to generate auto secret key in asp.net

    02-25-2009, 10:08 AM
    • All-Star
      23,801 point All-Star
    • qwe123kids
    • Member since 03-27-2008, 5:49 AM
    • Posts 4,075

     Hi,

    Kindly Go Through The Follwing Links

    http://coding.derkeiler.com/Archive/Java/comp.lang.java.programmer/2008-11/msg01656.html

     

    http://snippets.dzone.com/tag/csharp

     http://snippets.dzone.com/posts/show/5535

     

    Hope It Help You

     

    Thanks
    Avinash Tiwari

    Remember to click “Mark as Answer” on the post, if it helps you.

    MY Blog

    Hacking Inside .net exe
  • How to solve System.Security.Cryptography.CryptographicException: Bad Data error

    02-26-2009, 6:36 AM

    Dear Experts Thanx for reply.I gor idea from that links.One more doubt when i decrypt the ecncrypt string using auto generate key its showing error as System.Security.Cryptography.CryptographicException: Bad Data.

     I give the my function

    private void DecryptFunction()

    {

    try

    {

    //string strEncrypt = "b0154bc4a0f77d3056ff0009563ec9ab";

    string strEncrypt = "2f8ae3760636f348e0cb711c5109b78f";

    TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();

    //tdes.Key = Encoding.UTF8.GetBytes("ErXXz+VpJpQ7K/bmpzAkDg==");

    tdes.Key = Encoding.UTF8.GetBytes("22tqlER6RPmVvA2omvJJxg==");

    tdes.Mode = CipherMode.ECB;

    tdes.Padding = PaddingMode.PKCS7;

    ICryptoTransform crypt = tdes.CreateDecryptor();

    byte[] cipher = FromHex(strEncrypt);

    byte[] plain = crypt.TransformFinalBlock(cipher, 0, cipher.Length); ( Here Error Showing )

    Response.Write("Decryped string : " + System.Text.Encoding.UTF8.GetString(plain) + "<br/>");

    }

    catch (Exception ex)

    {

    Response.Write(ex.Message);

    }

    }

     

    I would appreciate some help

     

    Thanx

    Regards

    mani

    Filed under:
  • Re: How to solve System.Security.Cryptography.CryptographicException: Bad Data error

    03-01-2009, 6:32 AM
    • All-Star
      62,477 point All-Star
    • TATWORTH
    • Member since 02-04-2003, 8:34 AM
    • England
    • Posts 12,197
    • TrustedFriends-MVPs

     For some tolerably robust encrypt/decrypt routines, please see the encypt/decrypt rotines within the CommonData library at http://www.CodePlex.Com/CommonData. (In their current form, they are not protected against storage/transmission errors)

    Don't forget to click "Mark as Answer" on the post that helped you.
    This credits that member, earns you a point and marks your thread as Resolved so we will all know you have been helped.
Page 1 of 1 (6 items)