大家好,我是考100分的小小码 ,祝大家学习进步,加薪顺利呀。今天说一说oracle中的encrypt_des加密对应Java的加密方式,希望您对编程的造诣更进一步.
将18693157906加密后的密文是 FAD42A3BB2A4B9A5B36847714A56FE65
二、java中对应的加密、解密方法
代码100分
代码100分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
跟数据库加密一致
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
转载请注明出处: https://daima100.com/8295.html