Abstract: The project development process encountered a thorny problem: a system using Java development, encryption data through AES, B system using C # Development, need to get data from a system, but in the AES decryption time encountered trouble. Java code is not interoperable with C # code. Java code:/** * Encryption * * @param content needs to be encrypted inside ...
The development of the project encountered a thorny problem: a system using Java development, encryption data through AES, B system using C # Development, need to get data from a system, but in the decryption of AES trouble. Java code is not interoperable with C # code.
Java code:
/** * Encryption * * @param content requires encryption * @param password Encryption key * @return */public static string encrypt (string Conten T, String Password) {try {//code below is used to generate an encrypted key based on the original password, this code C # has no corresponding implementation Keyge Nerator KGen = keygenerator.getinstance ("AES"); Java.security.SecureRandom random = java.security.SecureRandom.getInstance ("sha1prng"); Random.setseed (Password.getbytes ()); Kgen.init (n, Random); Secretkey Secretkey = Kgen.generatekey (); byte[] Encodeformat = secretkey.getencoded (); The following code is standard AES encryption processing, C # can implement Secretkeyspec key = new Secretkeyspec (Encodeformat, "AES"); Cipher Cipher = cipher.getinstance ("AES"); byte[] bytecontent = content.getbytes ("Utf-8"); Cipher.init (Cipher.encrypt_mode, key); Return Codec.encodebase64 (Cipher.dofinal (bytecontent)); } catch (Exception e) {logger.error (E, "AES encryption exception"); } return null;
Find some information on the net, did not find satisfactory solution, so tried a trickery method, the concrete realization is as follows:
1) Extract the processing code of key in Java and write a simple tool class named Testgenaesbytekey.
Testgenaesbytekey converts the original password to the byte required by AES encryption, and then Base64 encodes it to get the string
2) The strings obtained from the above steps are manually copied into C # code, decrypted as a secret key
The specific code is as follows:
Testgenaesbytekey (Java language)
Package Api;import Java.io.unsupportedencodingexception;import Java.security.nosuchalgorithmexception;import Javax.crypto.keygenerator;import Javax.crypto.secretkey;import Sun.misc.base64encoder;public Class testgenaesbytekey{/** * @param args * @throws unsupportedencodingexception * @throws nosuchalgorithmexception */public static void Main (string[] args) throws Unsupportedencodingexception, nosuchalgorithmexception { Keygenerator KGen = keygenerator.getinstance ("AES"); Java.security.SecureRandom random = java.security.SecureRandom.getInstance ("sha1prng"); Random.setseed (Args[0].getbytes ()); Kgen.init (n, random); Secretkey Secretkey = Kgen.generatekey (); byte[] Encodeformat = secretkey.getencoded (); Base64encoder coder = new Base64encoder (); System.out.println (Coder.encode (Encodeformat));}}
Decryption code for C #:
public static string decrypt (String todecrypt,string key) { byte[] Keyarray = convert.frombase64string (key); Convert the string output from the Testgenaesbytekey class to a byte array byte[] Toencryptarray = convert.frombase64string (todecrypt); RijndaelManaged Rdel = new RijndaelManaged (); Rdel.key = Keyarray; Rdel.mode = CIPHERMODE.ECB; Must be set to ECB rdel.padding = PADDINGMODE.PKCS7; Must be set to PKCS7 icryptotransform ctransform = Rdel.createdecryptor (); byte[] Resultarray = Ctransform.transformfinalblock (toencryptarray, 0, toencryptarray.length); Return UTF8Encoding.UTF8.GetString (resultarray); }
For example: The original password is 123456, after testgenaesbytekey processing, the output a7sdfrddkrbe5fan2n3gfg==
The value of a7sdfrddkrbe5fan2n3gfg== as the key parameter of the C # function decrypt is passed in, and it can be decoded normally.
Several points to note:
1) C # default operation mode is Cbc,java default to ECB, so to change C # encryption to ECB
2) C # padding mode to be set to PADDINGMODE.PKCS7, otherwise decrypted after the end may be garbled
Trickery method for Java AES Encryption C # decryption