Skip to content

Commit

Permalink
[improve][common] improve EncryptUtil class
Browse files Browse the repository at this point in the history
  • Loading branch information
wgzhao committed Jan 7, 2024
1 parent a852982 commit 361aa4c
Showing 1 changed file with 5 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,22 +38,21 @@
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.GCMParameterSpec;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.SecretKeySpec;

import java.nio.charset.StandardCharsets;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.spec.InvalidKeySpecException;
import java.util.Base64;

import static com.wgzhao.addax.common.base.Constant.ENC_PASSWORD_PREFIX;

public class EncryptUtil
{
private static final String SECRET_KEY = "F3M0PxSWod6cyCejYUkpccU9gMsWwgrM";
private static final String SALT = "G2PuhRinJqKKFcBUT4eMaK3FKMx9iGmx";
private static final byte[] SALT = "G2PuhRinJqKKFcBUT4eMaK3FKMx9iGmx".getBytes();

private static final String TRANSFORMATION = "AES/GCM/NoPadding";
private static final String ALGORITHM = "AES";
Expand Down Expand Up @@ -106,9 +105,8 @@ private static SecretKeySpec getSecSpec() throws InvalidKeySpecException, NoSuch
final int keyLength = 128;
byte[] iv = new byte[16];
new SecureRandom().nextBytes(iv);
// new IvParameterSpec(iv);
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA512");
PBEKeySpec keySpec = new PBEKeySpec(SECRET_KEY.toCharArray(), SALT.getBytes(), iterationCount, keyLength);
PBEKeySpec keySpec = new PBEKeySpec(SECRET_KEY.toCharArray(), SALT, iterationCount, keyLength);
SecretKey keyTmp = keyFactory.generateSecret(keySpec);
return new SecretKeySpec(keyTmp.getEncoded(), ALGORITHM);
}
Expand All @@ -121,6 +119,6 @@ public static void main(String[] args)
System.exit(1);
}
String encrypted = encrypt(args[0]);
System.out.println("The encrypt string is : '${enc:" + encrypted + "}', you can paste it to json file.");
System.out.printf("The encrypt string is: '%s%s}', you can paste it into json file.", ENC_PASSWORD_PREFIX, encrypted);
}
}

0 comments on commit 361aa4c

Please sign in to comment.