JAVA代码
import org.apache.tomcat.util.codec.binary.Base64;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.net.URLEncoder;
import java.security.Key;
import java.security.SecureRandom;
public class EncryptUtil {
public static String encryptAES(String data,String key) throws Exception {
KeyGenerator kgen = KeyGenerator.getInstance("AES");
SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
secureRandom.setSeed(key.getBytes());
kgen.init(128, secureRandom);
SecretKey skey = kgen.generateKey();
byte[] raw = skey.getEncoded();
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
byte[] encryptedData = cipher.doFinal(data.getBytes("UTF-8"));
String hexStr = Base64.encodeBase64String(encryptedData);
return hexStr;
}
public static String decryptAES(String data,String key)throws Exception {
KeyGenerator kgen = KeyGenerator.getInstance("AES");
//kgen.init(128, new SecureRandom(key.getBytes()));
SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
secureRandom.setSeed(key.getBytes());
kgen.init(128, secureRandom);
SecretKey skey = kgen.generateKey();
byte[] raw = skey.getEncoded();
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE, skeySpec);
byte[] decryptedData = cipher.doFinal(Base64.decodeBase64(data));
String respStr = new String(decryptedData,"UTF-8");
return respStr;
}
public static void main(String[] args) throws Exception {
String key="ABCDEFGABCDEFG";
String username = "0001";
String password = "0002";
System.out.println(encryptAES(username, key));
System.out.println(encryptAES(password, key));
}
}
?0001 加密后?81Sd1kn/A2y+eshDtPLlqQ==
?0002 加密后?d3zRD2rsYLsXGl3U2LawNw==
需要对JAVA中的Key密钥进行编码生成一个字符串
public static void main(String[] args) throws NoSuchAlgorithmException {
String encyKey="ABCDEFGABCDEFG";
KeyGenerator kgen = KeyGenerator.getInstance("AES");
java.security.SecureRandom random = java.security.SecureRandom.getInstance("SHA1PRNG");
random.setSeed(encyKey.getBytes());
kgen.init(128, random);
SecretKey secretKey = kgen.generateKey();
byte[] enCodeFormat = secretKey.getEncoded();
System.out.println(Base64.encodeBase64String(enCodeFormat));
}
拿着生成之后的 这段字符串到C#中进行加密 yfGIl68aDih3DamkzIJeYA== ?C#代码
public static string AESEncode(string encryptString, string encryptKey)
{
if (string.IsNullOrEmpty(encryptString)) return null;
Byte[] toEncryptArray = Encoding.UTF8.GetBytes(encryptString);
RijndaelManaged rm = new RijndaelManaged
{
Key = Convert.FromBase64String(encryptKey),
Mode = CipherMode.ECB,
Padding = PaddingMode.PKCS7
};
ICryptoTransform cTransform = rm.CreateEncryptor();
Byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
return Convert.ToBase64String(resultArray, 0, resultArray.Length);
}
调用 AESEncode("0001", "yfGIl68aDih3DamkzIJeYA==");
运行之后得到 81Sd1kn/A2y+eshDtPLlqQ==
这样和Java中的加密的一致
|