国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

Python和Java進(jìn)行DES加密和解密的實(shí)例

2020-02-16 11:32:43
字體:
供稿:網(wǎng)友

DES 為 Data Encryption Standard (數(shù)據(jù)加密標(biāo)準(zhǔn))的縮寫,是一種常見的對稱加密算法。有關(guān)對稱加密與非對稱加密的特點(diǎn)及其應(yīng)用場景,本文就不描述了,讀者可以自行 google 。本文說明如何使用 Java 和 Python 兩種語言來實(shí)現(xiàn) DES 的加解密。

最近碰到的應(yīng)用場景是這樣的。我們需要對接一個(gè)系統(tǒng) S,系統(tǒng) S 已經(jīng)對用戶的身份進(jìn)行了驗(yàn)證,新系統(tǒng) N 也需要對用戶的身份進(jìn)行驗(yàn)證。采用的身份驗(yàn)證方法是由舊系統(tǒng) S 對用戶 ID 進(jìn)行加密,然后新系統(tǒng) N 對加密后的用戶 ID 進(jìn)行解密,從而獲取用戶 ID 并進(jìn)行身份驗(yàn)證。

由于舊系統(tǒng) S 是用 Java 實(shí)現(xiàn)的,新系統(tǒng) N 使用 Python 實(shí)現(xiàn)。也就是說,需要使用 Python 語言來對 Java DES 加密的用戶 ID 進(jìn)行解密。

這里貼出 Java 實(shí)現(xiàn)的 DES 加密的代碼。

import javax.crypto.spec.IvParameterSpec;import javax.crypto.Cipher;import javax.crypto.SecretKey;import javax.crypto.SecretKeyFactory;import javax.crypto.spec.DESKeySpec;public class Main { public static void main(String[] args) {  String content = "zx";  String key = "20171117";  System.out.println("加密前:" + content);  byte[] encrypted = DES_CBC_Encrypt(content.getBytes(), key.getBytes());  System.out.println("加密后:" + byteToHexString(encrypted));  byte[] decrypted = DES_CBC_Decrypt(encrypted, key.getBytes());  System.out.println("解密后:" + new String(decrypted)); } public static byte[] DES_CBC_Encrypt(byte[] content, byte[] keyBytes) {  try {   DESKeySpec keySpec = new DESKeySpec(keyBytes);   SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");   SecretKey key = keyFactory.generateSecret(keySpec);   Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");   cipher.init(Cipher.ENCRYPT_MODE, key, new IvParameterSpec(keySpec.getKey()));   byte[] result = cipher.doFinal(content);   return result;  } catch (Exception e) {   System.out.println("exception:" + e.toString());  }  return null; } private static byte[] DES_CBC_Decrypt(byte[] content, byte[] keyBytes) {  try {   DESKeySpec keySpec = new DESKeySpec(keyBytes);   SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");   SecretKey key = keyFactory.generateSecret(keySpec);   Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");   cipher.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(keyBytes));   byte[] result = cipher.doFinal(content);   return result;  } catch (Exception e) {   System.out.println("exception:" + e.toString());  }  return null; } private static String byteToHexString(byte[] bytes) {  StringBuffer sb = new StringBuffer(bytes.length);  String sTemp;  for (int i = 0; i < bytes.length; i++) {   sTemp = Integer.toHexString(0xFF & bytes[i]);   if (sTemp.length() < 2)    sb.append(0);   sb.append(sTemp.toUpperCase());  }  return sb.toString(); }}

Java 代碼采用的 DES 加密采用 CBC 模式,采用 PKCS5Padding 的填充模式,使用的初始化向量是加密的密鑰。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 延庆县| 千阳县| 青冈县| 金塔县| 大冶市| 九江市| 梁平县| 兰考县| 台南县| 犍为县| 施秉县| 清流县| 弋阳县| 甘孜| 剑河县| 河西区| 台江县| 临漳县| 太原市| 泗阳县| 隆化县| 肇源县| 简阳市| 石屏县| 南江县| 永丰县| 竹山县| 乳山市| 泰州市| 文安县| 雷山县| 松溪县| 同心县| 招远市| 嘉黎县| 绥化市| 海伦市| 错那县| 桂阳县| 云林县| 营山县|