我有像s =“ DLP_Classification_Rules”这样的String来转换某个整数,然后再次用该整数将DLP_Classification_Rules之类的字符串转换回去。
是否可以使用base64或其他方法来执行此操作?
答案 0 :(得分:2)
不适用于Base64,因为结果不是整数,而是字符串。 您要寻找的基本上是将字符串无损压缩为数字(它将比int大得多),这并不容易。
这完全取决于您的用例。您是否在单个JVM上运行单个应用程序?您在运行客户端/服务器模块吗?您是否需要在应用程序的运行之间保留信息?
例如,您可以使用Map
答案 1 :(得分:2)
希望它可以为您
package com.test;
import java.security.Key;
import javax.crypto.Cipher;
public class EncrypDES {
private static String strDefaultKey = "des20200903@#$%^&";
private Cipher encryptCipher = null;
private Cipher decryptCipher = null;
/**
* @throws Exception
*/
public EncrypDES() throws Exception {
this(strDefaultKey);
}
/**
* @param strKey
* @throws Exception
*/
public EncrypDES(String strKey) throws Exception {
Key key = getKey(strKey.getBytes());
encryptCipher = Cipher.getInstance("DES");
encryptCipher.init(Cipher.ENCRYPT_MODE, key);
decryptCipher = Cipher.getInstance("DES");
decryptCipher.init(Cipher.DECRYPT_MODE, key);
}
/**
* @param arrBTmp
* @return
* @throws Exception
*/
private Key getKey(byte[] arrBTmp) throws Exception {
byte[] arrB = new byte[8];
for (int i = 0; i < arrBTmp.length && i < arrB.length; i++) {
arrB[i] = arrBTmp[i];
}
Key key = new javax.crypto.spec.SecretKeySpec(arrB, "DES");
return key;
}
/**
* @param strIn
* @return
* @throws Exception
*/
public String encrypt(String strIn) throws Exception {
byte[] encryptByte = encryptCipher.doFinal(strIn.getBytes());
int iLen = encryptByte.length;
StringBuffer sb = new StringBuffer(iLen * 2);
for (int i = 0; i < iLen; i++) {
int intTmp = encryptByte[i];
while (intTmp < 0) {
intTmp = intTmp + 256;
}
if (intTmp < 16) {
sb.append("0");
}
sb.append(Integer.toString(intTmp, 16));
}
return sb.toString();
}
/**
* @param strIn
* @return
* @throws Exception
*/
public String decrypt(String strIn) throws Exception {
byte[] arrB = strIn.getBytes();
int iLen = arrB.length;
byte[] arrOut = new byte[iLen / 2];
for (int i = 0; i < iLen; i = i + 2) {
String strTmp = new String(arrB, i, 2);
arrOut[i / 2] = (byte) Integer.parseInt(strTmp, 16);
}
byte[] decryptByte = decryptCipher.doFinal(arrOut);
return new String(decryptByte);
}
public static void main(String[] args) {
try {
String msg1 = "xincan001";
String key = "20200903#@!";
EncrypDES des1 = new EncrypDES(key);
System.out.println("input value:" + msg1);
System.out.println("After encryption Value:" + des1.encrypt(msg1));
System.out.println("After decryption Value:" + des1.decrypt(des1.encrypt(msg1)));
System.out.println("--------------");
} catch (Exception e) {
e.printStackTrace();
}
}
}
操作结果
input value:xincan001
After encryption Value:c2cc016fae0bd2008282cae3f2c0be62
After decryption Value:xincan001
--------------
答案 2 :(得分:1)
如果要转换的字符串是常量,则可以为其创建一个Enum
:
enum StringInteger {
DLP_Classification_Rules(1),
Some_Other_String(2);
private int value;
StringInteger(int value) {
this.value = value;
}
public static StringInteger toInt(String value) {
try {
return StringInteger.valueOf(value);
} catch (Exception e) {
throw new RuntimeException("This string is not associated with an integer");
}
}
public static StringInteger toString(int value) {
for (StringInteger stringInteger : StringInteger.values()) {
if (stringInteger.value == value) return stringInteger;
}
throw new RuntimeException("This integer is not associated with a string");
}
}