當前位置:首頁 » 密碼管理 » encrypt加密java

encrypt加密java

發布時間: 2024-07-10 12:57:18

Ⅰ 鎬庝箞鐢↗AVA緇欑敓鎴愮殑EXCEL鍔犲瘑

Biff8EncryptionKey.setCurrentUserPassword("pass");
NPOIFSFileSystem fs = new NPOIFSFileSystem(new File("file.xls"), true);
HSSFWorkbook hwb = new HSSFWorkbook(fs.getRoot(), true);
Biff8EncryptionKey.setCurrentUserPassword(null);

// RC4 CryptoApi support ppt - decryption
Biff8EncryptionKey.setCurrentUserPassword("pass");
NPOIFSFileSystem fs = new NPOIFSFileSystem(new File("file.ppt"), true);
HSLFSlideShow hss = new HSLFSlideShow(fs);
...
// Option 1: remove password
Biff8EncryptionKey.setCurrentUserPassword(null);
OutputStream os = new FileOutputStream("decrypted.ppt");
hss.write(os);
os.close();
...
// Option 2: change encryption settings (experimental)
// need to cache data (i.e. read all data) before changing the key size
PictureData picsExpected[] = hss.getPictures();
hss.getDocumentSummaryInformation();
EncryptionInfo ei = hss.getDocumentEncryptionAtom().getEncryptionInfo();
((CryptoAPIEncryptionHeader)ei.getHeader()).setKeySize(0x78);
OutputStream os = new FileOutputStream("file_120bit.ppt");
hss.write(os);
os.close();

java題:編寫一個加密程序,對用戶輸入的字元串加密後輸出。加密的方法是將每個字元映射成字母表中的對稱

publicStringgetPass(Stringstr){
Stringstr="";
for(inti=0;i<str.length();i++){
charch=str.charAt(i);//得到單個字元ch;
if(ch>64&&ch<91){//成立=>ch是大寫字母
str+=(char)(155-ch);//大寫對應字母之和:155eg:'A'+'Z'=155
}elseif(ch>96&&ch<123){//成立則ch是小寫字母
str+=(char)(219-ch);//小寫對應字母之和:219如:'a'+'z'=219
}else{
str="";//若進入此處,則說明str中有非字母字元
returnstr;//直接退出
}
}
returnstr;//返回str的值;
}
樓主估計是新手,此處程序鄙人就不進一步優化。(可優化)

Ⅲ 求大神用java實現RC4的加密,解密功能,高分懸賞.

importjavax.crypto.Cipher;
importjavax.crypto.spec.SecretKeySpec;
importjavax.xml.bind.DatatypeConverter;

publicclassTest{
publicstaticvoidmain(String[]args)throwsException{
Ciphercipher=Cipher.getInstance("RC4");
Stringpwd="123456";
Stringptext="HelloWorld你好";
SecretKeySpeckey=newSecretKeySpec(pwd.getBytes("UTF-8"),"RC4");

cipher.init(Cipher.ENCRYPT_MODE,key);
byte[]cdata=cipher.update(ptext.getBytes("UTF-8"));
//解密
cipher.init(Cipher.DECRYPT_MODE,key);
byte[]ddata=cipher.update(cdata);
System.out.println("密碼:"+pwd);
System.out.println("明文:"晌隱+ptext);
System.out.println("密文:"+DatatypeConverter.printHexBinary(cdata));
System.out.println("解密文:"+newString(ddata,"UTF-8"));
}
}
密碼:123456
明文:HelloWorld你鎮戚好
密文:
解密文御謹陵:HelloWorld你好

RC4已經不太安全,只能用於一般加密,不能用於金融等緊要場合。

Ⅳ java加密的幾種方式

朋友你好,很高興為你作答。

首先,Java加密能夠應對的風險包括以下幾個:

1、核心技術竊取

2、核心業務破解

3、通信模塊破解

4、API介面暴露

本人正在使用幾維安全Java加密方式,很不錯,向你推薦,希望能夠幫助到你。

幾維安全Java2C針對DEX文件進行加密保護,將DEX文件中標記的Java代碼翻譯為C代碼,編譯成加固後的SO文件。默認情況只加密activity中的onCreate函數,如果開發者想加密其它類和方法,只需對相關類或函數添加標記代碼,在APK加密時會自動對標記的代碼進行加密處理。

與傳統的APP加固方案相比,不涉及到自定義修改DEX文件的載入方式,所以其兼容性非常好;其次Java函數被完全轉化為C函數,直接在Native層執行,不存在Java層解密執行的步驟,其性能和執行效率更優。

如果操作上有不明白的地方,可以聯系技術支持人員幫你完成Java加密。

希望以上解答能夠幫助到你。

Ⅳ java環境下實現idea演算法的加密解密

基於Java的IDEA加密演算法探討
隨著Internet的迅速發展,電子商務的浪潮勢不可擋,日常工作和數據傳輸都放在Internet網上進行傳輸,大大提高了效率,降低了成本,創造了良好的效益。但是,由於 Internet網路協議本身存在著重要的安全問題(IP包本身並不繼承任何安全特性,很容易偽造出IP包的地址、修改其內容、重播以前的包以及在傳輸途中攔截並查看包的內容),使網上的信息傳輸存在巨大的安全風險電子商務的安全問題也越來越突出。加密是電子商務中最主要的安全技術,加密方法的選取直接影響電子商務活動中信息的安全程度,在電子商務系統中,主要的安全問題都可以通過加密來解決。數據的保密性可通過不同的加密演算法對數據加密來實現。
對我國來講,雖然可以引進很多的外國設備,但加密設備不能依靠引進,因為它涉及到網路安全、國家機密信息的安全,所以必須自己研製。當前國際上有許多加密演算法,其中DES(Data Encryption Standard)是發明最早的用得最廣泛的分組對稱加密演算法,DES用56位蜜鑰加密64位明文,輸出64位密文,DES的56位密鑰共有256 種可能的密鑰,但歷史上曾利用窮舉攻擊破解過DES密鑰,1998年電子邊境基金會(EFF)用25萬美元製造的專用計算機,用56小時破解了DES的密鑰,1999年,EFF用22小時完成了破解工作,使DES演算法受到了嚴重打擊,使它的安全性受到嚴重威脅。因為JAVA語言的安全性和網路處理能力較強,本文主要介紹使用IDEA(Internation Data Encryption Algorithm )數據加密演算法在Java環境下實現數據的安全傳輸。

一、IDEA數據加密演算法

IDEA數據加密演算法是由中國學者來學嘉博士和著名的密碼專家 James L. Massey 於1990年聯合提出的。它的明文和密文都是64比特,但密鑰長為128比特。IDEA 是作為迭代的分組密碼實現的,使用 128 位的密鑰和 8 個循環。這比 DES 提供了更多的 安全性,但是在選擇用於 IDEA 的密鑰時,應該排除那些稱為「弱密鑰」的密鑰。DES 只有四個弱密鑰和 12 個次弱密鑰,而 IDEA 中的弱密鑰數相當可觀,有 2 的 51 次方個。但是,如果密鑰的總數非常大,達到 2 的 128 次方個,那麼仍有 2 的 77 次方個密鑰可供選擇。IDEA 被認為是極為安全的。使用 128 位的密鑰,蠻力攻擊中需要進行的測試次數與 DES 相比會明顯增大,甚至允許對弱密鑰測試。而且,它本身也顯示了它尤其能抵抗專業形式的分析性攻擊。

二、Java密碼體系和Java密碼擴展

Java是Sun公司開發的一種面向對象的編程語言,並且由於它的平台無關性被大量應用於Internet的開發。Java密碼體系(JCA)和Java密碼擴展(JCE)的設計目的是為Java提供與實現無關的加密函數API。它們都用factory方法來創建類的常式,然後把實際的加密函數委託給提供者指定的底層引擎,引擎中為類提供了服務提供者介面在Java中實現數據的加密/解密,是使用其內置的JCE(Java加密擴展)來實現的。Java開發工具集1.1為實現包括數字簽名和信息摘要在內的加密功能,推出了一種基於供應商的新型靈活應用編程介面。Java密碼體系結構支持供應商的互操作,同時支持硬體和軟體實現。Java密碼學結構設計遵循兩個原則:(1)演算法的獨立性和可靠性。(2)實現的獨立性和相互作用性。演算法的獨立性是通過定義密碼服務類來獲得。用戶只需了解密碼演算法的概念,而不用去關心如何實現這些概念。實現的獨立性和相互作用性通過密碼服務提供器來實現。密碼服務提供器是實現一個或多個密碼服務的一個或多個程序包。軟體開發商根據一定介面,將各種演算法實現後,打包成一個提供器,用戶可以安裝不同的提供器。安裝和配置提供器,可將包含提供器的ZIP和JAR文件放在CLASSPATH下,再編輯Java安全屬性文件來設置定義一個提供器。Java運行環境Sun版本時,提供一個預設的提供器Sun。

三、Java環境下的實現

1.加密過程的實現

void idea_enc( int data11[], /*待加密的64位數據首地址*/ int key1[]){

int i ;

int tmp,x;

int zz[]=new int[6];

for ( i = 0 ; i < 48 ; i += 6) { /*進行8輪循環*/

for(int j=0,box=i; j<6; j++,box++){

zz[j]=key1[box];

}

x = handle_data(data11,zz);

tmp = data11[1]; /*交換中間兩個*/

data11[1] = data11[2];

data11[2] = tmp;

}

tmp = data11[1]; /*最後一輪不交換*/

data11[1] = data11[2];

data11[2] = tmp;

data11[0] = MUL(data11[0],key1[48]);

data11[1] =(char)((data11[1] + key1[49])%0x10000);

data11[2] =(char)((data11[2] + key1[50])%0x10000);

data11[3] = MUL(data11[3],key1[51]);

}

2.解密過程的實現

void key_decryExp(int outkey[])/*解密密鑰的變逆處理*/

{ int tmpkey[] = new int[52] ;

int i;

for ( i = 0 ; i < 52 ; i++) {

tmpkey[i] = outkey[ wz_spkey[i] ] ; /*換位*/

}

for ( i = 0 ; i < 52 ; i++) {

outkey[i] = tmpkey[i];

}

for ( i = 0 ; i < 18 ; i++) {

outkey[wz_spaddrever[i]] = (char)(65536-outkey[wz_spaddrever[i]]) ; /*替換成加法逆*/

}

for ( i = 0 ; i < 18 ; i++){

outkey[wz_spmulrevr[i]] =(char)(mulInv(outkey[wz_spmulrevr[i]] )); /*替換成乘法逆*/

}

}

四、總結

在實際應用中,我們可以使用Java開發工具包(JDK)中內置的對Socket通信的支持,通過JCE中的Java流和鏈表,加密基於Socket的網路通信.我們知道,加密/解密是數據傳輸中保證數據完整性的常用方法,Java語言因其平台無關性,在Internet上的應用非常之廣泛.使用Java實現基於IDEA的數據加密傳輸可以在不同的平台上實現並具有實現簡潔、安全性強等優點。

Ⅵ 求一個java演算法,用128位密鑰的AES加密128位明文,得出的密文還是128位...很著急,非常感謝各位大神,求救!

/**
* 加密
*
* @param content 需要加密的內容
* @param password 加密密碼
* @return
*/
public static byte[] encrypt(String content, String password) {
try {
KeyGenerator kgen = KeyGenerator.getInstance("AES");
kgen.init(128, new SecureRandom(password.getBytes()));
SecretKey secretKey = kgen.generateKey();
byte[] enCodeFormat = secretKey.getEncoded();
SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");
Cipher cipher = Cipher.getInstance("AES");// 創建密碼器
byte[] byteContent = content.getBytes("utf-8");
cipher.init(Cipher.ENCRYPT_MODE, key);// 初始化
byte[] result = cipher.doFinal(byteContent);
return result; // 加密
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
}
return null;
}

Ⅶ 怎麼對加密的JAVA class文件進行解密

JAVA class文件加密解密

package com..encrypt;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.RandomAccessFile;
public class FileEncryptAndDecrypt {
/**
* 文件file進行加密
* @param fileUrl 文件路徑
* @param key 密碼
* @throws Exception
*/
public static void encrypt(String fileUrl, String key) throws Exception {
File file = new File(fileUrl);
String path = file.getPath();
if(!file.exists()){
return;
}
int index = path.lastIndexOf("迅掘雀\\");
String destFile = path.substring(0, index)+"\\"+"abc";
File dest = new File(destFile);
InputStream in = new FileInputStream(fileUrl);
OutputStream out = new FileOutputStream(destFile);
byte[] buffer = new byte[1024];
int r;
byte[] buffer2=new byte[1024];
while (( r= in.read(buffer)) > 0) {
for(int i=0;i<r;i++)
{
byte b=buffer[i];
buffer2[i]=b==255?0:++b;
}
out.write(buffer2, 0, r);
out.flush();
}
in.close();
out.close();
file.delete();
dest.renameTo(new File(fileUrl));
appendMethodA(fileUrl, key);
System.out.println("加密成功"散野);
}
/**
*
* @param fileName
* @param content 密鑰
*/
public static void appendMethodA(String fileName, String content) {
try {
// 打開一個隨畝早機訪問文件流,按讀寫方式
RandomAccessFile randomFile = new RandomAccessFile(fileName, "rw");
// 文件長度,位元組數
long fileLength = randomFile.length();
//將寫文件指針移到文件尾。
randomFile.seek(fileLength);
randomFile.writeBytes(content);
randomFile.close();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 解密
* @param fileUrl 源文件
* @param tempUrl 臨時文件
* @param ketLength 密碼長度
* @return
* @throws Exception
*/
public static String decrypt(String fileUrl, String tempUrl, int keyLength) throws Exception{
File file = new File(fileUrl);
if (!file.exists()) {
return null;
}
File dest = new File(tempUrl);
if (!dest.getParentFile().exists()) {
dest.getParentFile().mkdirs();
}
InputStream is = new FileInputStream(fileUrl);
OutputStream out = new FileOutputStream(tempUrl);
byte[] buffer = new byte[1024];
byte[] buffer2=new byte[1024];
byte bMax=(byte)255;
long size = file.length() - keyLength;
int mod = (int) (size%1024);
int div = (int) (size>>10);
int count = mod==0?div:(div+1);
int k = 1, r;
while ((k <= count && ( r = is.read(buffer)) > 0)) {
if(mod != 0 && k==count) {
r = mod;
}
for(int i = 0;i < r;i++)
{
byte b=buffer[i];
buffer2[i]=b==0?bMax:--b;
}
out.write(buffer2, 0, r);
k++;
}
out.close();
is.close();
return tempUrl;
}
/**
* 判斷文件是否加密
* @param fileName
* @return
*/
public static String readFileLastByte(String fileName, int keyLength) {
File file = new File(fileName);
if(!file.exists())return null;
StringBuffer str = new StringBuffer();
try {
// 打開一個隨機訪問文件流,按讀寫方式
RandomAccessFile randomFile = new RandomAccessFile(fileName, "r");
// 文件長度,位元組數
long fileLength = randomFile.length();
//將寫文件指針移到文件尾。
for(int i = keyLength ; i>=1 ; i--){
randomFile.seek(fileLength-i);
str.append((char)randomFile.read());
}
randomFile.close();
return str.toString();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}

Ⅷ java des 加密 解密 密鑰隨機取得方法

java DES 加密 解密 生成隨機密鑰
舉例說明:
//保存生成的key
public static void saveDesKey() {
try {
SecureRandom sr = new SecureRandom();
// 選擇的DES演算法生成一個KeyGenerator對象
KeyGenerator kg = KeyGenerator.getInstance("DES");
kg.init(sr);
// 相對路徑 需要新建 conf 文件夾
// String fileName = "conf/DesKey.xml";
// 絕對路徑
String fileName = "d:/DesKey.xml";
FileOutputStream fos = new FileOutputStream(fileName);
ObjectOutputStream oos = new ObjectOutputStream(fos);
// 生成密鑰
Key key = kg.generateKey();
oos.writeObject(key);
oos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
//獲取生成的key
public static Key getKey() {
Key kp = null;
try {
// 相對路徑 需要新建 conf 文件夾
// String fileName = "conf/DesKey.xml";
// InputStream is = Encrypt.class.getClassLoader().getResourceAsStream(fileName);
// 絕對路徑
String fileName = "d:/DesKey.xml";
FileInputStream is = new FileInputStream(fileName);
ObjectInputStream oos = new ObjectInputStream(is);
kp = (Key) oos.readObject();
oos.close();
} catch (Exception e) {
e.printStackTrace();
}
return kp;
}
//加密開始
public static void encrypt(String file, String dest) throws Exception {
Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.ENCRYPT_MODE, getKey());
InputStream is = new FileInputStream(file);
OutputStream out = new FileOutputStream(dest);
CipherInputStream cis = new CipherInputStream(is, cipher);
byte[] buffer = new byte[1024];
int r;
while ((r = cis.read(buffer)) > 0) {
out.write(buffer, 0, r);
}
cis.close();
is.close();
out.close();
}
//解密開始
public static void decrypt(String file, String dest) throws Exception {
Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.DECRYPT_MODE, getKey());
InputStream is = new FileInputStream(file);
OutputStream out = new FileOutputStream(dest);
CipherOutputStream cos = new CipherOutputStream(out, cipher);
byte[] buffer = new byte[1024];
int r;
while ((r = is.read(buffer)) >= 0) {
cos.write(buffer, 0, r);
}
cos.close();
out.close();
is.close();
}
}
//des加密主方法
public class DES {
public static void main(String[] args) throws Exception {
Encrypt.saveDesKey();
System.out.println("生成key");
Encrypt.getKey();
System.out.println("獲取key");
Encrypt.encrypt("d:\\hello.txt", "d:\\encrypt.txt");
System.out.println("加密");
Encrypt.decrypt("d:\\encrypt.txt", "d:\\decrypt.txt");
System.out.println("解密");
}
以上方法親測可用。

熱點內容
ios應用上傳 發布:2024-09-08 09:39:41 瀏覽:438
ios儲存密碼哪裡看 發布:2024-09-08 09:30:02 瀏覽:869
opensslcmake編譯 發布:2024-09-08 09:08:48 瀏覽:653
linux下ntp伺服器搭建 發布:2024-09-08 08:26:46 瀏覽:744
db2新建資料庫 發布:2024-09-08 08:10:19 瀏覽:173
頻率計源碼 發布:2024-09-08 07:40:26 瀏覽:780
奧迪a6哪個配置帶後排加熱 發布:2024-09-08 07:06:32 瀏覽:101
linux修改apache埠 發布:2024-09-08 07:05:49 瀏覽:209
有多少個不同的密碼子 發布:2024-09-08 07:00:46 瀏覽:566
linux搭建mysql伺服器配置 發布:2024-09-08 06:50:02 瀏覽:995