項目開發過程中遇到一個棘手的問題:A系統使用java開發,通過AES加密數據,B系統使用C#開發,需要從A系統獲取數據,但在AES解密的時候遇到麻煩。Java的代碼和C#的代碼無法互通。
Java代碼:
/**
* 加密
*
* @param content 需要加密的內容
* @param password 加密密鑰
* @return
*/
public static String encrypt(String content, String password) {
try {
//如下代碼用於根據原始的password生成加密的key,這段代碼C#是沒有對應的實現的
KeyGenerator kgen = KeyGenerator.getInstance("AES");
java.security.SecureRandom random = java.security.SecureRandom.getInstance("SHA1PRNG");
random.setSeed(password.getBytes());
kgen.init(128, random);
SecretKey secretKey = kgen.generateKey();
byte[] enCodeFormat = secretKey.getEncoded();
//如下代碼是標準的AES加密處理,C#可以實現
SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");</