C # implements JAVA AES encryption and decryption [original]

Source: Internet
Author: User
Tags base64 decrypt pkcs7

The following is a commonly available online Java AES encryption and decryption method.

Because it uses keygenerator and securerandom, there are no 2 classes in. NET. Cannot generate key using secure random number.

After we receive the AES encrypted string sent by Java, there is no corresponding keygenerator and securerandom in. NET to generate the key value of AES, resulting in the inability to decrypt directly.

1 ImportJava.security.SecureRandom;2 Importjava.util.Base64;3 4 ImportJavax.crypto.Cipher;5 ImportJavax.crypto.KeyGenerator;6 ImportJavax.crypto.SecretKey;7 ImportJavax.crypto.spec.SecretKeySpec;8 9  Public classJavaaes {Ten      Public Static voidMain (string[] args)throwsexception{ One         byte[] A = Aesencrypt ("123456". GetBytes (), "abcd1234"); ASystem.out.println (NewString (Base64.getencoder (). encodetostring (a))); -     } -     //AES Encryption the      Public Static byte[] Aesencrypt (byte[] bytecontent, String password)throwsException { -          -Keygenerator KGen = keygenerator.getinstance ("AES"); -SecureRandom securerandom = securerandom.getinstance ("Sha1prng"); + Securerandom.setseed (Password.getbytes ()); -Kgen.init (128, securerandom); +  ASecretkey Secretkey =Kgen.generatekey (); at         byte[] Encodeformat = secretkey.getencoded ();//AES encrypts the actual key value -         //if direct encodeformat=password.getbytes (), then. NET can be decrypted directly -  -Secretkeyspec key =NewSecretkeyspec (Encodeformat, "AES"); -  -Cipher Cipher = cipher.getinstance ("AES"); in  - Cipher.init (Cipher.encrypt_mode, key); to         returncipher.dofinal (bytecontent); +     } -     //AES Decryption the      Public Static byte[] Aesdecrypt (byte[] bytecontent, String password)throwsException { *Keygenerator KGen = keygenerator.getinstance ("AES"); $SecureRandom securerandom = securerandom.getinstance ("Sha1prng");Panax Notoginseng Securerandom.setseed (Password.getbytes ()); -Kgen.init (128, securerandom); the          +Secretkey Secretkey =Kgen.generatekey (); A         byte[] Encodeformat =secretkey.getencoded (); the          +Secretkeyspec key =NewSecretkeyspec (Encodeformat, "AES"); -Cipher Cipher = cipher.getinstance ("AES"); $ Cipher.init (Cipher.decrypt_mode, key); $         returncipher.dofinal (bytecontent); -     } -  the}


The first approach is to generate the actual key value directly from the AES key in Java and decrypt it with this actual key in. Net.

1      Public Static voidMain (string[] args)throwsexception{2String password = "1234567890";//Key for AES3Keygenerator KGen = keygenerator.getinstance ("AES");4SecureRandom securerandom = securerandom.getinstance ("Sha1prng");5 Securerandom.setseed (Password.getbytes ());6Kgen.init (128, securerandom);7 8Secretkey Secretkey =Kgen.generatekey ();9         byte[] Encodeformat = secretkey.getencoded ();//AES encrypts the actual key valueTenSystem.out.println (NewString (Base64.getencoder (). encodetostring (Encodeformat))); One}
1         /// <summary>2         ///AES Decryption3         /// </summary>4         /// <param name= "Data" ></param>5         /// <param name= "key" ></param>6         /// <returns></returns>7          Public Static stringDeaes (byte[]Contentstringkey)8         {9             using(Aescryptoserviceprovider Aesprovider =NewAescryptoserviceprovider ())Ten             { OneAesprovider.key =convert.frombase64string (key); AAesprovider.mode =CIPHERMODE.ECB; -Aesprovider.padding =PADDINGMODE.PKCS7; -                 using(ICryptoTransform cryptotransform =aesprovider.createdecryptor ()) the                 { -                     byte[] Inputbuffers =content; -                     byte[] results = Cryptotransform.transformfinalblock (Inputbuffers,0, inputbuffers.length); - aesprovider.clear (); +                     returnEncoding.UTF8.GetString (results); -                 } +             } A}


The second method is to download the IKVM and extract the IKVM.OpenJDK.Core.dll, IKVM in the bin directory in the VS project after decompression. OpenJDK.Security.dll

1 usingjava.security;2 usingJavax.crypto;3 usingSystem.Security.Cryptography;4 usingSystem.Text;5 usingSystem;6 7  Public class Program8 {9      Public Static voidMain ()Ten     { One         byte[] A = convert.frombase64string ("w0fsdgixanavxmiddpxa/q=="); A         stringresult = Deaes (A,"abcd1234"); - Console.WriteLine (result); - Console.read (); the     } -     /// <summary> -     ///AES Decryption -     /// </summary> +     /// <param name= "Data" ></param> -     /// <param name= "key" ></param> +     /// <returns></returns> A      Public Static stringDeaes (byte[] content,stringkey) at     { -Keygenerator KGen = keygenerator.getinstance ("AES"); -SecureRandom SecureRandom = securerandom.getinstance ("sha1prng"); - securerandom.setseed (Encoding.ASCII.GetBytes (key)); -Kgen.init ( -, securerandom); -Secretkey Secretkey =Kgen.generatekey (); in         byte[] Encodeformat =secretkey.getencoded (); -  to         using(Aescryptoserviceprovider Aesprovider =NewAescryptoserviceprovider ()) +         { -Aesprovider.key =Encodeformat; theAesprovider.mode =CIPHERMODE.ECB; *Aesprovider.padding =PADDINGMODE.PKCS7; $             using(ICryptoTransform cryptotransform =aesprovider.createdecryptor ())Panax Notoginseng             { -                 byte[] Inputbuffers =content; the                 byte[] results = Cryptotransform.transformfinalblock (Inputbuffers,0, inputbuffers.length); + aesprovider.clear (); A                 returnEncoding.UTF8.GetString (results); the             } +         } -     } $  $}

C # implements JAVA AES encryption and decryption [original]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.