Home > Back-end >  How JAVA AES encryption into Delphi, Delphi AES encryption and JAVA different
How JAVA AES encryption into Delphi, Delphi AES encryption and JAVA different

Time:09-23

 package com.sjq.com mon. Utils; 

Import the Java. IO. UnsupportedEncodingException;
Import the Java. Security. InvalidKeyException;
import java.security.NoSuchAlgorithmException;
Import the Java. Security. SecureRandom;

The import javax.mail. Crypto. BadPaddingException;
The import javax.mail. Crypto. Cipher;
The import javax.mail. Crypto. IllegalBlockSizeException;
The import javax.mail. Crypto. KeyGenerator;
The import javax.mail. Crypto. NoSuchPaddingException;
The import javax.mail. Crypto. SecretKey;
The import javax.mail. Crypto. Spec. SecretKeySpec;

/* *
* AES encryption and decryption classes: for user password decryption
* @ author lupb
*/
Public class AESPassword {

Public static String key="123321";

Public static void main (String [] args) throws UnsupportedEncodingException {
String password="123456";

String aesPassword=aesPassword. Encrypt (password, key);
System. The out. Println (" by AES encryption: "+ aesPassword);

Password=AESPassword. Decrypt (AESPassword, key);
System. The out. Println (" AES decryption: "+");

}

/* *
* encryption
* @ param content need to encrypt the contents of the
* @ param password encryption password
* @ return aes encryption string
*/
Public static String encrypt (String content, String password) {
Try {
KeyGenerator kgen=KeyGenerator.getInstance("AES");
Kgen. Init (128, new SecureRandom (password) getBytes ()));
SecretKey SecretKey.=kgen generateKey ();
Byte [] enCodeFormat=secretKey. GetEncoded ();
SecretKeySpec key=new SecretKeySpec (enCodeFormat, AES, "");
AES Cipher Cipher=Cipher. GetInstance (" ");//create a password,
Byte [] byteContent=content. getBytes (" utf-8 ");
Cipher. The init (cipher ENCRYPT_MODE, key);//initialize
Result=cipher byte []. DoFinal (byteContent);
Return parseByte2HexStr (result);//. The toString ();//encryption
} the catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} the catch (NoSuchPaddingException e) {
e.printStackTrace();
} the catch (InvalidKeyException e) {
e.printStackTrace();
} the catch (UnsupportedEncodingException e) {
e.printStackTrace();
} the catch (IllegalBlockSizeException e) {
e.printStackTrace();
} the catch (BadPaddingException e) {
e.printStackTrace();
}
return null;
}


/* *
* decryption
* @ param content need to decrypt the contents of the
* @ param password decryption password
* @ return decrypted string
*/
Public static String decrypt (String content, String password) {
Try {
Byte [] contents=parseHexStr2Byte (content);//. GetBytes ();
KeyGenerator kgen=KeyGenerator.getInstance("AES");
Kgen. Init (128, new SecureRandom (password) getBytes ()));
SecretKey SecretKey.=kgen generateKey ();
Byte [] enCodeFormat=secretKey. GetEncoded ();
SecretKeySpec key=new SecretKeySpec (enCodeFormat, AES, "");
AES Cipher Cipher=Cipher. GetInstance (" ");//create a password,
Cipher. The init (cipher DECRYPT_MODE, key);//initialize
Result=cipher byte []. DoFinal (contents).
//return parseByte2HexStr (result);//. The toString ();//encryption
Return a new String (result);
} 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;
}

Public static String parseByte2HexStr (byte buf []) {
StringBuffer sb=new StringBuffer();
for (int i=0; i String hex=Integer. ToHexString (buf [I] & amp; 0 XFF);
If (hex. Length ()==1) {
Hex='0' + hex;
}
Sb. Append (hex. ToUpperCase ());
}
return sb.toString();
}

Public static byte [] parseHexStr2Byte (String hexStr) {
If (hexStr. Length () & lt; 1)
return null;
Byte [] result=new byte [hexStr length ()/2];
for (int i=0; iInt high=Integer. The parseInt (hexStr substring (I * 2, I * 2 + 1), 16);
Int low=Integer. The parseInt (hexStr substring (I * 2 + 1, I * 2 + 2), 16);
The result [I]=(byte) * 16 + low (high);
}
return result;
}
}


CodePudding user response:

With Delphi AES calculated results is not the same as with Java

CodePudding user response:

If write their own logic, should be the same. Can be specified using the unicode character set.

CodePudding user response:

reference 1st floor huangpan_123 response:
with Delphi AES calculated result is different from the Java calculated


ParseByte2HexStr
ParseHexStr2Byte turn these two look like hexadecimal characters, byte array, Delphi aes libraries with the Java turn out the result of the difference is very big, is not the same as online aes encryption is

CodePudding user response:

http://www.cnblogs.com/freeliver54/archive/2011/10/08/2202136.html hateful, docking buggy Java code, over there,

CodePudding user response:

nullnull
  • Related