Home > Back-end >  Evaluate a Delphi under XE5 AES encryption source code, can and Java to achieve mutual understanding
Evaluate a Delphi under XE5 AES encryption source code, can and Java to achieve mutual understanding

Time:09-25

On the Internet and download some AES encryption, but the Java is the solution doesn't open, I was not familiar to Delphi, hope big fairies to help me, thank you!
We are enclosing the Java examples:
 
The import sun. Misc. BASE64Decoder;
The import sun. Misc. BASE64Encoder;

The import javax.mail. Crypto. BadPaddingException;
The import javax.mail. Crypto. Cipher;
The import javax.mail. Crypto. IllegalBlockSizeException;
The import javax.mail. Crypto. NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;
import java.io.IOException;
Import the Java. Security. InvalidKeyException;
Import the Java. Security. NoSuchAlgorithmException;
Public class AES {
Public enum KeySize {
Ks16 (16), ks24 (24), ks32 (32).
Private int value.

KeySize (int value) {
This value=https://bbs.csdn.net/topics/value;
}
}

/* *
* AES encryption Base64 generated after the secondary encryption
*
* @ param content
* @ param passwd
* @ param ks
*
* @ return
*/
Public static String encryptBase64 (byte [] the content, the String passwd, KeySize ks) {
Byte [] bytes=encrypt (the content, the passwd, ks);
if (bytes !=null)
Return new BASE64Encoder (.) encode (bytes);
The else
return null;
}

/* *
* password auto-complete
*
* @ param in
* @ param ks
* if the password length is not 16,24,32 auto-complete (binary 0)
*
* @ return
*/
Private static byte [] ZeroPadding (byte [] in the KeySize ks) {
The Integer copyLen=in. Length;
If (copyLen & gt; Ks. Value) {
CopyLen=ks. Value;
}
Byte [] out=new byte [ks value];
System. Arraycopy (in 0, out, 0, copyLen);
Return the out;
}

/* *
* AES encryption
*
* @ param content
* @ param passwd
* @ param ks
*
* @ return
*/
Public static byte [] encrypt (byte [] the content of the String passwd, KeySize ks) {
Try {
Cipher aesECB=Cipher. GetInstance (" AES/the ECB/PKCS5Padding ");
SecretKeySpec key=new SecretKeySpec (ZeroPadding (passwd. GetBytes (), ks), "AES");
AesECB. Init (Cipher ENCRYPT_MODE, key);
Return aesECB. DoFinal (content);
} the catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} the catch (NoSuchPaddingException e) {
e.printStackTrace();
} the catch (InvalidKeyException e) {
e.printStackTrace();
} the catch (IllegalBlockSizeException e) {
e.printStackTrace();
} the catch (BadPaddingException e) {
e.printStackTrace();
}
return null;
}

/* *
* decryption
*
* @ param content
* @ param passwd
* @ param ks
*
* @ return
*/
Public static byte [] decrypt (byte [] the content of the String passwd, KeySize ks) {
Try {
Cipher Cipher=Cipher. GetInstance (" AES/the ECB/PKCS5Padding ");//create a password,
SecretKeySpec key=new SecretKeySpec (ZeroPadding (passwd. GetBytes (), ks), "AES");
Cipher. The init (cipher DECRYPT_MODE, key);//initialize

Return cipher. DoFinal (content);//decryption
} the catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} the catch (NoSuchPaddingException e) {
e.printStackTrace();
} the catch (InvalidKeyException e) {
e.printStackTrace();
} the catch (IllegalBlockSizeException e) {
e.printStackTrace();
} the catch (BadPaddingException e) {
e.printStackTrace();
}
return null;
}

/* *
* AES decryption, Base64 decryption, first on AES decryption
*
* @ param content
* @ param passwd
* @ param ks
*
* @ return
*
* @ throws IOException
*/
Public static byte [] decryptBase64 (String content, String passwd, KeySize ks) throws IOException {
Byte [] deBytes=new BASE64Decoder () decodeBuffer (content);
Return the decrypt (deBytes passwd, ks);
}

Public static void main (String [] args) throws the Exception {
The String content="I'm fine";
The String key="1234567890123456";
//System. Out. Println (Util. Byte2hex (AES) encrypt (content. getBytes (" utf-8 "), the key, the KeySize. Ks16)));
//System. Out. Println (new String (AES) decrypt (Util. Hex2byte (" 0 da244f228acb14d6f9c02aac527dc52 "), the key, the KeySize. Ks16), "utf-8"));
String eb=AES. EncryptBase64 (content. getBytes (" utf-8 "), the key, the KeySize. Ks16);
System. The out. Println (eb);
System. The out. Println (Util. Byte2hex (eb) getBytes ()));
System. The out. Println (new String (AES) decryptBase64 (eb, the key, the KeySize. Ks16), "utf-8"));
}
}
  • Related