oracle中的encrypt_des加密对应Java的加密方式

database

将18693157906加密后的密文是 FAD42A3BB2A4B9A5B36847714A56FE65

二、java中对应的加密、解密方法

public class Utils {

#密钥

private static String key = "test#5&124*!de";

/**

* 加密

* @param inStr

* @return

*/

public static String ENCRYPT_DES(String inStr) {

DESKeySpec desKey;

SecretKey securekey;

Cipher cipher;

try {

desKey = new DESKeySpec(key.getBytes());

securekey = SecretKeyFactory.getInstance("DES").generateSecret(desKey);

cipher = Cipher.getInstance("DES/CBC/NoPadding");

cipher.init(Cipher.ENCRYPT_MODE, securekey, new IvParameterSpec(new byte[8]));

byte[] inBytes = new byte[((int) (inStr.length() / 8) + 1) * 8];

for (int i = 0; i < inStr.length(); i++) {

inBytes[i] = inStr.getBytes()[i];

}

byte[] enBytes = cipher.doFinal(inBytes);

String hexStr = DatatypeConverter.printHexBinary(enBytes);

return hexStr;

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

return null;

}

/**

* 解密

* @param encryptStr

* @return

*/

public static String DECRYPT_DES(String encryptStr) {

DESKeySpec desKey;

SecretKey securekey;

Cipher cipher;

try {

desKey = new DESKeySpec(key.getBytes());

securekey = SecretKeyFactory.getInstance("DES").generateSecret(desKey);

cipher = Cipher.getInstance("DES/CBC/NoPadding");

cipher.init(Cipher.DECRYPT_MODE, securekey, new IvParameterSpec(new byte[8]));

byte[] decryptBytes = cipher.doFinal(Hex.decodeHex(encryptStr.toCharArray()));

return new String(decryptBytes).trim();

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

return null;

}

public static void main(String[] args) {

System.out.println("加密:"+ENCRYPT_DES("18693157906"));

System.out.println("解密:"+DECRYPT_DES("FAD42A3BB2A4B9A5B36847714A56FE65"));

}

}

三、运行代码得到结果

可以看到加密后的密文是FAD42A3BB2A4B9A5B36847714A56FE65

解密后的明文是18693157906

跟数据库加密一致

以上是 oracle中的encrypt_des加密对应Java的加密方式 的全部内容, 来源链接: utcz.com/z/533785.html

回到顶部