java加解密視頻,java加密解密字符串

本文目錄一覽:

java中如何實現對文件和字符串加密. 解密?

DES 密鑰生成,加解密方法,,你可以看一下

//DES 密鑰生成工具

import java.io.File;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.ObjectOutputStream;

import java.security.InvalidKeyException;

import java.security.NoSuchAlgorithmException;

import java.security.SecureRandom;

import java.security.spec.InvalidKeySpecException;

import javax.crypto.KeyGenerator;

import javax.crypto.SecretKey;

import javax.crypto.SecretKeyFactory;

import javax.crypto.spec.DESKeySpec;

public class GenKey {

private static final String DES = “DES”;

public static final String SKEY_NAME = “key.des”;

public static void genKey1(String path) {

// 密鑰

SecretKey skey = null;

// 密鑰隨機數生成

SecureRandom sr = new SecureRandom();

//生成密鑰文件

File file = genFile(path);

try {

// 獲取密鑰生成實例

KeyGenerator gen = KeyGenerator.getInstance(DES);

// 初始化密鑰生成器

gen.init(sr);

// 生成密鑰

skey = gen.generateKey();

// System.out.println(skey);

ObjectOutputStream oos = new ObjectOutputStream(

new FileOutputStream(file));

oos.writeObject(skey);

oos.close();

} catch (NoSuchAlgorithmException e) {

e.printStackTrace();

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

}

/**

* @param file : 生成密鑰的路徑

* SecretKeyFactory 方式生成des密鑰

* */

public static void genKey2(String path) {

// 密鑰隨機數生成

SecureRandom sr = new SecureRandom();

// byte[] bytes = {11,12,44,99,76,45,1,8};

byte[] bytes = sr.generateSeed(20);

// 密鑰

SecretKey skey = null;

//生成密鑰文件路徑

File file = genFile(path);

try {

//創建deskeyspec對象

DESKeySpec desKeySpec = new DESKeySpec(bytes,9);

//實例化des密鑰工廠

SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);

//生成密鑰對象

skey = keyFactory.generateSecret(desKeySpec);

//寫出密鑰對象

ObjectOutputStream oos = new ObjectOutputStream(

new FileOutputStream(file));

oos.writeObject(skey);

oos.close();

} catch (NoSuchAlgorithmException e) {

e.printStackTrace();

} catch (InvalidKeyException e) {

e.printStackTrace();

} catch (InvalidKeySpecException e) {

e.printStackTrace();

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

}

private static File genFile(String path) {

String temp = null;

File newFile = null;

if (path.endsWith(“/”) || path.endsWith(“\\”)) {

temp = path;

} else {

temp = path + “/”;

}

File pathFile = new File(temp);

if (!pathFile.exists())

pathFile.mkdirs();

newFile = new File(temp+SKEY_NAME);

return newFile;

}

/**

* @param args

*/

public static void main(String[] args) {

// TODO Auto-generated method stub

genKey2(“E:/a/aa/”);

}

}

//DES加解密方法

import java.io.BufferedInputStream;

import java.io.BufferedOutputStream;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.ObjectInputStream;

import javax.crypto.Cipher;

import javax.crypto.CipherInputStream;

import javax.crypto.SecretKey;

import org.apache.commons.logging.Log;

import org.apache.commons.logging.LogFactory;

/**

*制卡文件加/解密 加密方式DES

*/

public class SecUtil {

public static final Log log = LogFactory.getLog(SecUtil.class);

/**

* 解密

*

* @param keyPath

* 密鑰路徑

* @param source

* 解密前文件

* @param dest

* 解密後文件

*/

public static void decrypt(String keyPath, String source, String dest) {

SecretKey key = null;

try {

ObjectInputStream keyFile = new ObjectInputStream(

// 讀取加密密鑰

new FileInputStream(keyPath));

key = (SecretKey) keyFile.readObject();

keyFile.close();

} catch (FileNotFoundException ey1) {

log.info(“Error when read keyFile”);

throw new RuntimeException(ey1);

} catch (Exception ey2) {

log.info(“error when read the keyFile”);

throw new RuntimeException(ey2);

}

// 用key產生Cipher

Cipher cipher = null;

try {

// 設置算法,應該與加密時的設置一樣

cipher = Cipher.getInstance(“DES”);

// 設置解密模式

cipher.init(Cipher.DECRYPT_MODE, key);

} catch (Exception ey3) {

log.info(“Error when create the cipher”);

throw new RuntimeException(ey3);

}

// 取得要解密的文件並解密

File file = new File(source);

String filename = file.getName();

try {

// 輸出流,請注意文件名稱的獲取

BufferedOutputStream out = new BufferedOutputStream(

new FileOutputStream(dest));

// 輸入流

CipherInputStream in = new CipherInputStream(

new BufferedInputStream(new FileInputStream(file)), cipher);

int thebyte = 0;

while ((thebyte = in.read()) != -1) {

out.write(thebyte);

}

in.close();

out.close();

} catch (Exception ey5) {

log.info(“Error when encrypt the file”);

throw new RuntimeException(ey5);

}

}

/**

* 加密

* @param keyPath 密鑰路徑

* @param source 加密前文件

* @param dest 加密後文件

*/

public static void encrypt(String keyPath, String source, String dest) {

SecretKey key = null;

try {

ObjectInputStream keyFile = new ObjectInputStream(

// 讀取加密密鑰

new FileInputStream(keyPath));

key = (SecretKey) keyFile.readObject();

keyFile.close();

} catch (FileNotFoundException ey1) {

log.info(“Error when read keyFile”);

throw new RuntimeException(ey1);

} catch (Exception ey2) {

log.info(“error when read the keyFile”);

throw new RuntimeException(ey2);

}

// 用key產生Cipher

Cipher cipher = null;

try {

// 設置算法,應該與加密時的設置一樣

cipher = Cipher.getInstance(“DES”);

// 設置解密模式

cipher.init(Cipher.ENCRYPT_MODE, key);

} catch (Exception ey3) {

log.info(“Error when create the cipher”);

throw new RuntimeException(ey3);

}

// 取得要解密的文件並解密

File file = new File(source);

String filename = file.getName();

try {

// 輸出流,請注意文件名稱的獲取

BufferedOutputStream out = new BufferedOutputStream(

new FileOutputStream(dest));

// 輸入流

CipherInputStream in = new CipherInputStream(

new BufferedInputStream(new FileInputStream(file)), cipher);

int thebyte = 0;

while ((thebyte = in.read()) != -1) {

out.write(thebyte);

}

in.close();

out.close();

} catch (Exception ey5) {

log.info(“Error when encrypt the file”);

throw new RuntimeException(ey5);

}

}

}

如何使用java對視頻進行編碼和解碼

攝像頭採集的如果是1080P的,文件太大不可能直接輸送到客戶端。很多攝像頭廠家自帶SDK,讓你去調用,得到的是H264和AAC格式的源文件。要用FFMPEG合成MP4之類的。

如何用JAVA實現字符串簡單加密解密?

java加密字符串可以使用des加密算法,實例如下:

package test;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.ObjectInputStream;

import java.io.ObjectOutputStream;

import java.security.*;

import javax.crypto.Cipher;

import javax.crypto.KeyGenerator;

import javax.crypto.SecretKey;

/**

* 加密解密

*

* @author shy.qiu

* @since

*/

public class CryptTest {

/**

* 進行MD5加密

*

* @param info

* 要加密的信息

* @return String 加密後的字符串

*/

public String encryptToMD5(String info) {

byte[] digesta = null;

try {

// 得到一個md5的消息摘要

MessageDigest alga = MessageDigest.getInstance(“MD5”);

// 添加要進行計算摘要的信息

alga.update(info.getBytes());

// 得到該摘要

digesta = alga.digest();

} catch (NoSuchAlgorithmException e) {

e.printStackTrace();

}

// 將摘要轉為字符串

String rs = byte2hex(digesta);

return rs;

}

/**

* 進行SHA加密

*

* @param info

* 要加密的信息

* @return String 加密後的字符串

*/

public String encryptToSHA(String info) {

byte[] digesta = null;

try {

// 得到一個SHA-1的消息摘要

MessageDigest alga = MessageDigest.getInstance(“SHA-1”);

// 添加要進行計算摘要的信息

alga.update(info.getBytes());

// 得到該摘要

digesta = alga.digest();

} catch (NoSuchAlgorithmException e) {

e.printStackTrace();

}

// 將摘要轉為字符串

String rs = byte2hex(digesta);

return rs;

}

// //////////////////////////////////////////////////////////////////////////

/**

* 創建密匙

*

* @param algorithm

* 加密算法,可用 DES,DESede,Blowfish

* @return SecretKey 秘密(對稱)密鑰

*/

public SecretKey createSecretKey(String algorithm) {

// 聲明KeyGenerator對象

KeyGenerator keygen;

// 聲明 密鑰對象

SecretKey deskey = null;

try {

// 返回生成指定算法的秘密密鑰的 KeyGenerator 對象

keygen = KeyGenerator.getInstance(algorithm);

// 生成一個密鑰

deskey = keygen.generateKey();

} catch (NoSuchAlgorithmException e) {

e.printStackTrace();

}

// 返回密匙

return deskey;

}

/**

* 根據密匙進行DES加密

*

* @param key

* 密匙

* @param info

* 要加密的信息

* @return String 加密後的信息

*/

public String encryptToDES(SecretKey key, String info) {

// 定義 加密算法,可用 DES,DESede,Blowfish

String Algorithm = “DES”;

// 加密隨機數生成器 (RNG),(可以不寫)

SecureRandom sr = new SecureRandom();

// 定義要生成的密文

byte[] cipherByte = null;

try {

// 得到加密/解密器

Cipher c1 = Cipher.getInstance(Algorithm);

// 用指定的密鑰和模式初始化Cipher對象

// 參數:(ENCRYPT_MODE, DECRYPT_MODE, WRAP_MODE,UNWRAP_MODE)

c1.init(Cipher.ENCRYPT_MODE, key, sr);

// 對要加密的內容進行編碼處理,

cipherByte = c1.doFinal(info.getBytes());

} catch (Exception e) {

e.printStackTrace();

}

// 返回密文的十六進制形式

return byte2hex(cipherByte);

}

/**

* 根據密匙進行DES解密

*

* @param key

* 密匙

* @param sInfo

* 要解密的密文

* @return String 返回解密後信息

*/

public String decryptByDES(SecretKey key, String sInfo) {

// 定義 加密算法,

String Algorithm = “DES”;

// 加密隨機數生成器 (RNG)

SecureRandom sr = new SecureRandom();

byte[] cipherByte = null;

try {

// 得到加密/解密器

Cipher c1 = Cipher.getInstance(Algorithm);

// 用指定的密鑰和模式初始化Cipher對象

c1.init(Cipher.DECRYPT_MODE, key, sr);

// 對要解密的內容進行編碼處理

cipherByte = c1.doFinal(hex2byte(sInfo));

} catch (Exception e) {

e.printStackTrace();

}

// return byte2hex(cipherByte);

return new String(cipherByte);

}

// /////////////////////////////////////////////////////////////////////////////

/**

* 創建密匙組,並將公匙,私匙放入到指定文件中

*

* 默認放入mykeys.bat文件中

*/

public void createPairKey() {

try {

// 根據特定的算法一個密鑰對生成器

KeyPairGenerator keygen = KeyPairGenerator.getInstance(“DSA”);

// 加密隨機數生成器 (RNG)

SecureRandom random = new SecureRandom();

// 重新設置此隨機對象的種子

random.setSeed(1000);

// 使用給定的隨機源(和默認的參數集合)初始化確定密鑰大小的密鑰對生成器

keygen.initialize(512, random);// keygen.initialize(512);

// 生成密鑰組

KeyPair keys = keygen.generateKeyPair();

// 得到公匙

PublicKey pubkey = keys.getPublic();

// 得到私匙

PrivateKey prikey = keys.getPrivate();

// 將公匙私匙寫入到文件當中

doObjToFile(“mykeys.bat”, new Object[] { prikey, pubkey });

} catch (NoSuchAlgorithmException e) {

e.printStackTrace();

}

}

/**

* 利用私匙對信息進行簽名 把簽名後的信息放入到指定的文件中

*

* @param info

* 要簽名的信息

* @param signfile

* 存入的文件

*/

public void signToInfo(String info, String signfile) {

// 從文件當中讀取私匙

PrivateKey myprikey = (PrivateKey) getObjFromFile(“mykeys.bat”, 1);

// 從文件中讀取公匙

PublicKey mypubkey = (PublicKey) getObjFromFile(“mykeys.bat”, 2);

try {

// Signature 對象可用來生成和驗證數字簽名

Signature signet = Signature.getInstance(“DSA”);

// 初始化簽署簽名的私鑰

signet.initSign(myprikey);

// 更新要由位元組簽名或驗證的數據

signet.update(info.getBytes());

// 簽署或驗證所有更新位元組的簽名,返回簽名

byte[] signed = signet.sign();

// 將數字簽名,公匙,信息放入文件中

doObjToFile(signfile, new Object[] { signed, mypubkey, info });

} catch (Exception e) {

e.printStackTrace();

}

}

/**

* 讀取數字簽名文件 根據公匙,簽名,信息驗證信息的合法性

*

* @return true 驗證成功 false 驗證失敗

*/

public boolean validateSign(String signfile) {

// 讀取公匙

PublicKey mypubkey = (PublicKey) getObjFromFile(signfile, 2);

// 讀取簽名

byte[] signed = (byte[]) getObjFromFile(signfile, 1);

// 讀取信息

String info = (String) getObjFromFile(signfile, 3);

try {

// 初始一個Signature對象,並用公鑰和簽名進行驗證

Signature signetcheck = Signature.getInstance(“DSA”);

// 初始化驗證簽名的公鑰

signetcheck.initVerify(mypubkey);

// 使用指定的 byte 數組更新要簽名或驗證的數據

signetcheck.update(info.getBytes());

System.out.println(info);

// 驗證傳入的簽名

return signetcheck.verify(signed);

} catch (Exception e) {

e.printStackTrace();

return false;

}

}

/**

* 將二進制轉化為16進制字符串

*

* @param b

* 二進制位元組數組

* @return String

*/

public String byte2hex(byte[] b) {

String hs = “”;

String stmp = “”;

for (int n = 0; n b.length; n++) {

stmp = (java.lang.Integer.toHexString(b[n] 0XFF));

if (stmp.length() == 1) {

hs = hs + “0” + stmp;

} else {

hs = hs + stmp;

}

}

return hs.toUpperCase();

}

/**

* 十六進制字符串轉化為2進制

*

* @param hex

* @return

*/

public byte[] hex2byte(String hex) {

byte[] ret = new byte[8];

byte[] tmp = hex.getBytes();

for (int i = 0; i 8; i++) {

ret[i] = uniteBytes(tmp[i * 2], tmp[i * 2 + 1]);

}

return ret;

}

/**

* 將兩個ASCII字符合成一個位元組; 如:”EF”– 0xEF

*

* @param src0

* byte

* @param src1

* byte

* @return byte

*/

public static byte uniteBytes(byte src0, byte src1) {

byte _b0 = Byte.decode(“0x” + new String(new byte[] { src0 }))

.byteValue();

_b0 = (byte) (_b0 4);

byte _b1 = Byte.decode(“0x” + new String(new byte[] { src1 }))

.byteValue();

byte ret = (byte) (_b0 ^ _b1);

return ret;

}

/**

* 將指定的對象寫入指定的文件

*

* @param file

* 指定寫入的文件

* @param objs

* 要寫入的對象

*/

public void doObjToFile(String file, Object[] objs) {

ObjectOutputStream oos = null;

try {

FileOutputStream fos = new FileOutputStream(file);

oos = new ObjectOutputStream(fos);

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

oos.writeObject(objs[i]);

}

} catch (Exception e) {

e.printStackTrace();

} finally {

try {

oos.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

/**

* 返回在文件中指定位置的對象

*

* @param file

* 指定的文件

* @param i

* 從1開始

* @return

*/

public Object getObjFromFile(String file, int i) {

ObjectInputStream ois = null;

Object obj = null;

try {

FileInputStream fis = new FileInputStream(file);

ois = new ObjectInputStream(fis);

for (int j = 0; j i; j++) {

obj = ois.readObject();

}

} catch (Exception e) {

e.printStackTrace();

} finally {

try {

ois.close();

} catch (IOException e) {

e.printStackTrace();

}

}

return obj;

}

/**

* 測試

*

* @param args

*/

public static void main(String[] args) {

CryptTest jiami = new CryptTest();

// 執行MD5加密”Hello world!”

System.out.println(“Hello經過MD5:” + jiami.encryptToMD5(“Hello”));

// 生成一個DES算法的密匙

SecretKey key = jiami.createSecretKey(“DES”);

// 用密匙加密信息”Hello world!”

String str1 = jiami.encryptToDES(key, “Hello”);

System.out.println(“使用des加密信息Hello為:” + str1);

// 使用這個密匙解密

String str2 = jiami.decryptByDES(key, str1);

System.out.println(“解密後為:” + str2);

// 創建公匙和私匙

jiami.createPairKey();

// 對Hello world!使用私匙進行簽名

jiami.signToInfo(“Hello”, “mysign.bat”);

// 利用公匙對簽名進行驗證。

if (jiami.validateSign(“mysign.bat”)) {

System.out.println(“Success!”);

} else {

System.out.println(“Fail!”);

}

}

}

如何用JAVA對視頻和圖片等多媒體文件進行加密解密

創建一個虛擬解密文件設備,傳遞給該設備的參數就是它的真實物理地址,多媒體那邊像正常文件操作一樣。這個虛擬解密設備的驅動則負責解碼。注意,你使用的加密方式必須是流加密,否則視頻播放會有問題。

原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/257692.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
小藍的頭像小藍
上一篇 2024-12-15 12:46
下一篇 2024-12-15 12:46

相關推薦

  • Python字符串寬度不限制怎麼打代碼

    本文將為大家詳細介紹Python字符串寬度不限制時如何打代碼的幾個方面。 一、保持代碼風格的統一 在Python字符串寬度不限制的情況下,我們可以寫出很長很長的一行代碼。但是,為了…

    編程 2025-04-29
  • Python中將字符串轉化為浮點數

    本文將介紹在Python中將字符串轉化為浮點數的常用方法。在介紹方法之前,我們先來思考一下這個問題應該如何解決。 一、eval函數 在Python中,最簡單、最常用的將字符串轉化為…

    編程 2025-04-29
  • AES加密解密算法的C語言實現

    AES(Advanced Encryption Standard)是一種對稱加密算法,可用於對數據進行加密和解密。在本篇文章中,我們將介紹C語言中如何實現AES算法,並對實現過程進…

    編程 2025-04-29
  • Java判斷字符串是否存在多個

    本文將從以下幾個方面詳細闡述如何使用Java判斷一個字符串中是否存在多個指定字符: 一、字符串遍歷 字符串是Java編程中非常重要的一種數據類型。要判斷字符串中是否存在多個指定字符…

    編程 2025-04-29
  • Python學習筆記:去除字符串最後一個字符的方法

    本文將從多個方面詳細闡述如何通過Python去除字符串最後一個字符,包括使用切片、pop()、刪除、替換等方法來實現。 一、字符串切片 在Python中,可以通過字符串切片的方式來…

    編程 2025-04-29
  • Python如何將字符串1234變成數字1234

    Python作為一種廣泛使用的編程語言,對於數字和字符串的處理提供了很多便捷的方式。如何將字符串「1234」轉化成數字「1234」呢?下面將從多個方面詳細闡述Python如何將字符…

    編程 2025-04-29
  • Python int轉二進制字符串

    本文將從以下幾個方面對Python中將int類型轉換為二進制字符串進行詳細闡述: 一、int類型和二進制字符串的定義 在Python中,int類型表示整數,二進制字符串則是由0和1…

    編程 2025-04-29
  • 用title和capitalize美觀處理Python字符串

    在Python中,字符串是最常用的數據類型之一。對字符串的美觀處理是我們在實際開發中經常需要的任務之一。Python內置了一些方法,如title和capitalize,可以幫助我們…

    編程 2025-04-28
  • Python 提取字符串中的電話號碼

    Python 是一種高級的、面向對象的編程語言,它具有簡單易學、開發迅速、代碼簡潔等特點,廣泛應用於 Web 開發、數據科學、人工智能等領域。在 Python 中,提取字符串中的電…

    編程 2025-04-28
  • Python如何打印帶雙引號的字符串

    Python作為一種廣泛使用的編程語言,在日常開發中經常需要打印帶雙引號的字符串。那麼,如何打印帶雙引號的字符串呢? 一、使用轉義字符 在Python中,我們可以通過使用轉義字符\…

    編程 2025-04-28

發表回復

登錄後才能評論