C # Encryption and decryption and Java encryption decryption, the same result __c#

Source: Internet
Author: User
Tags base64 decrypt null null pkcs7

Java code:

Import Javax.crypto.Cipher;
 
Import Javax.crypto.spec.SecretKeySpec;
Import org.apache.commons.codec.binary.Base64;  /** * * @author Administrator */public class AES {//Encrypt public static string Encrypt (String ssrc, String
            SKey) throws Exception {if (SKey = = null) {System.out.print ("key is null null");
        return null;
            //Determine if key is 16-bit if (Skey.length ()!=) {System.out.print ("key length is not 16 bits");
        return null;
        } byte[] Raw = skey.getbytes ("Utf-8");
        Secretkeyspec Skeyspec = new Secretkeyspec (Raw, "AES"); Cipher Cipher = cipher.getinstance ("aes/ecb/pkcs5padding");//"algorithm/Mode/complement Method" Cipher.init (Cipher.encrypt_mode, Skeyspe
        c);
 
        Byte[] encrypted = cipher.dofinal (Ssrc.getbytes ("Utf-8"));
    return new Base64 (). encodetostring (encrypted);//Here use BASE64 to do the transcoding function, at the same time can play the role of 2 times encryption. }//Decrypt public static string Decrypt (String ssrc, String sKey) throws Exception{try {//judge whether the key is correct if (SKey = = null) {System.out.print ("key is null null");
            return null;
                //Determine if key is 16-bit if (Skey.length ()!=) {System.out.print ("key length is not 16 bits");
            return null;
            } byte[] Raw = skey.getbytes ("Utf-8");
            Secretkeyspec Skeyspec = new Secretkeyspec (Raw, "AES");
            Cipher Cipher = cipher.getinstance ("aes/ecb/pkcs5padding");
            Cipher.init (Cipher.decrypt_mode, Skeyspec); byte[] encrypted1 = new Base64 (). Decode (SSRC);//First Decrypt try {base64] byte[= Original
                NAL (encrypted1);
                String originalstring = new String (Original, "Utf-8");
            return originalstring;
                catch (Exception e) {System.out.println (e.tostring ());
            return null; } catch (Exception ex) {SYstem.out.println (Ex.tostring ());
        return null; } public static void Main (string[] args) throws Exception {/* * use AES-128-ECB encryption mode here, key needs to be 16
         Bit
        */String Ckey = "1234567890123456";
        Strings that need to be encrypted the regulatory commission = "Www.gowhere.so";
        SYSTEM.OUT.PRINTLN (Regulatory Commission); Encrypted String enstring = AES.
        Encrypt (Regulatory Commission, Ckey);
 
        System.out.println ("Encrypted string is:" + enstring); Decrypt String destring = AES.
        Decrypt (enstring, Ckey);
    System.out.println ("Decrypted string is:" + destring); }
}

C # code

<summary>///AES Encryption///</summary>///<param name= "text" > Encrypted character &LT;/PARAM&G
       T <param name= "password" > encrypted password </param>///<param name= "IV" > key </param>///<ret urns></returns> public static string Encrypt (String toencrypt,string key) {byte[] Keyar
           Ray = UTF8Encoding.UTF8.GetBytes (key);
 
           byte[] Toencryptarray = UTF8Encoding.UTF8.GetBytes (Toencrypt);
           RijndaelManaged Rdel = new RijndaelManaged ();
           Rdel.key = Keyarray;
           Rdel.mode = CIPHERMODE.ECB;
 
           rdel.padding = PADDINGMODE.PKCS7;
           ICryptoTransform ctransform = Rdel.createencryptor ();
 
           byte[] Resultarray = Ctransform.transformfinalblock (toencryptarray, 0, toencryptarray.length);
       Return convert.tobase64string (resultarray, 0, resultarray.length);
}///<summary>///AES decryption///</summary>       <param name= "text" ></param>///<param name= "password" ></param>///<p Aram Name= "IV" ></param>///<returns></returns> public static string Decrypt (String ToD
           Ecrypt,string key) {byte[] Keyarray = UTF8Encoding.UTF8.GetBytes (key);
 
           byte[] Toencryptarray = convert.frombase64string (Todecrypt);
           RijndaelManaged Rdel = new RijndaelManaged ();
           Rdel.key = Keyarray;
           Rdel.mode = CIPHERMODE.ECB;
 
           rdel.padding = PADDINGMODE.PKCS7;
           ICryptoTransform ctransform = Rdel.createdecryptor ();
 
           byte[] Resultarray = Ctransform.transformfinalblock (toencryptarray, 0, toencryptarray.length);
       Return UTF8Encoding.UTF8.GetString (resultarray); }

There is another article:

https://blog.csdn.net/softwave/article/details/53939824

Related Article

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.