test.java

来自「AES C++加密解密和java版加密解密方法」· Java 代码 · 共 125 行 · 第 1/2 页

JAVA
125
字号
			e.printStackTrace();
		}
		
	}
	public String dec(String key ,byte[] aess) {
		System.out.println("======================================================");
		byte[] raw =key.getBytes();
		
		Cipher cipher =null;
		SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
		 try {
			cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
			try {
				cipher.init(Cipher.DECRYPT_MODE, skeySpec);
			} catch (InvalidKeyException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} catch (NoSuchAlgorithmException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NoSuchPaddingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		
		
		
		// byte[] encrypted1 = hex2byte(sSrc);
		String doc = "";
		
			
				//byte[] bytOuta = cipher.doFinal(aess);
				System.out.println(aess.length);
				byte[] bytOut;
				bytOut = cipher.update(aess);
				doc = new String (bytOut);
				
				
				
			
			return doc;
	}
	   public static byte[] hex2byte(String strhex) {
	        if (strhex == null) {
	            return null;
	        }
	        int l = strhex.length();
	        if (l % 2 == 1) {
	            return null;
	        }
	        byte[] b = new byte[l / 2];
	        for (int i = 0; i != l / 2; i++) {
	            b[i] = (byte) Integer.parseInt(strhex.substring(i * 2, i * 2 + 2), 32);
	        }
	        return b;
	    }

}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?