導航:首頁 > 編程語言 > javades亂碼

javades亂碼

發布時間:2022-03-08 07:20:09

⑴ mysql 解密java 用des加密數據

標准DES算採用約定向量1計算默認情況C#Java向量相同 結致能補位等算造檢查算詳細設置設置行

⑵ DES加密 中文亂碼

讀取的時候使用位元組流,加密之後保存到另一個文件也使用位元組寫進去,解密的時候位元組流出來解了之後然後將位元組數組使用new String(byte[])來生成String應該就不會出問題了。

⑶ Java用des加密後用 linux c語言解密 怎麼互通

同樣的數據 用java和c分別加密看看輸出是否一樣
如果一樣再分別解密 看看輸出是否一樣
就可以判斷是哪裡出問題了

⑷ java的DES加密128位的明文為什麼得到的密文卻是192位的

加密前後的長度不是固定的,而且你用的密匙不一樣的話,長度也不一樣。

⑸ java使用DES加密,解密後文件正常,Linux下解密後英文和漢字都是亂碼,這是怎麼回事

mark一下.
這問題我覺得你還是到專業的社區或論壇提問,比如csdn

⑹ 為什麼 CryptoJS DES 加密的結果和 Java DES 不一樣

1.注意取字元串bytes是編碼保持一致,不如都用Unicode 2.key和Iv保持一致 3.加密模式和填充方式保持一致 比如C#里 algo.Mode=CipherMode.ECB; algo.Padding=PaddingMode.None; 則java里對應的為 final Cipher algo=Cipher.getInstance("DES/ECB/

⑺ java des演算法

我用你的數據 測試的

des 加密用的 BASE64Encoder

結果是:TVRlhcWXA9qq7OxmxxF78HI89tF4w//R

⑻ 如何實現c和java的des互相加解密

/*
* MD5 演算法
*/
public class MD5 {

// 全局數組
private final static String[] strDigits = { "0", "1", "2", "3", "4", "5",
"6", "7", "8", "9", "a", "b", "c", "d", "e", "f" };

public MD5() {
}

// 返回形式為數字跟字元串
private static String byteToArrayString(byte bByte) {
int iRet = bByte;
// System.out.println("iRet="+iRet);
if (iRet < 0) {
iRet += 256;
}
int iD1 = iRet / 16;
int iD2 = iRet % 16;
return strDigits[iD1] + strDigits[iD2];
}

// 返回形式只為數字
private static String byteToNum(byte bByte) {
int iRet = bByte;
System.out.println("iRet1=" + iRet);
if (iRet < 0) {
iRet += 256;
}
return String.valueOf(iRet);
}

// 轉換位元組數組為16進制字串
private static String byteToString(byte[] bByte) {
StringBuffer sBuffer = new StringBuffer();
for (int i = 0; i < bByte.length; i++) {
sBuffer.append(byteToArrayString(bByte[i]));
}
return sBuffer.toString();
}

public static String GetMD5Code(String strObj) {
String resultString = null;
try {
resultString = new String(strObj);
MessageDigest md = MessageDigest.getInstance("MD5");
// md.digest() 該函數返回值為存放哈希值結果的byte數組
resultString = byteToString(md.digest(strObj.getBytes()));
} catch (NoSuchAlgorithmException ex) {
ex.printStackTrace();
}
return resultString;
}

public static void main(String[] args) {
MD5 getMD5 = new MD5();
System.out.println(getMD5.GetMD5Code("000000"));
}
}

⑼ 用java實現des演算法

package des;

import java.io.*;
import java.nio.*;
import java.nio.channels.FileChannel;

public class FileDES{
private static final boolean enc=true; //加密
private static final boolean dec=false; //解密

private String srcFileName;
private String destFileName;
private String inKey;
private boolean actionType;
private File srcFile;
private File destFile;
private Des des;

private void analyzePath(){
String dirName;
int pos=srcFileName.lastIndexOf("/");
dirName=srcFileName.substring(0,pos);
File dir=new File(dirName);
if (!dir.exists()){
System.err.println(dirName+" is not exist");
System.exit(1);
}else if(!dir.isDirectory()){
System.err.println(dirName+" is not a directory");
System.exit(1);
}

pos=destFileName.lastIndexOf("/");
dirName=destFileName.substring(0,pos);
dir=new File(dirName);
if (!dir.exists()){
if(!dir.mkdirs()){
System.out.println ("can not creat directory:"+dirName);
System.exit(1);
}
}else if(!dir.isDirectory()){
System.err.println(dirName+" is not a directory");
System.exit(1);
}
}

private static int replenish(FileChannel channel,ByteBuffer buf) throws IOException{
long byteLeft=channel.size()-channel.position();
if(byteLeft==0L)
return -1;
buf.position(0);
buf.limit(buf.position()+(byteLeft<8 ? (int)byteLeft :8));
return channel.read(buf);
}

private void file_operate(boolean flag){
des=new Des(inKey);
FileOutputStream outputFile=null;
try {
outputFile=new FileOutputStream(srcFile,true);
}catch (java.io.FileNotFoundException e) {
e.printStackTrace(System.err);
}
FileChannel outChannel=outputFile.getChannel();

try{
if(outChannel.size()%2!=0){
ByteBuffer bufTemp=ByteBuffer.allocate(1);
bufTemp.put((byte)32);
bufTemp.flip();
outChannel.position(outChannel.size());
outChannel.write(bufTemp);
bufTemp.clear();
}
}catch(Exception ex){
ex.printStackTrace(System.err);
System.exit(1);
}
FileInputStream inFile=null;
try{
inFile=new FileInputStream(srcFile);
}catch(java.io.FileNotFoundException e){
e.printStackTrace(System.err);
//System.exit(1);
}
outputFile=null;
try {
outputFile=new FileOutputStream(destFile,true);
}catch (java.io.FileNotFoundException e) {
e.printStackTrace(System.err);
}

FileChannel inChannel=inFile.getChannel();
outChannel=outputFile.getChannel();

ByteBuffer inBuf=ByteBuffer.allocate(8);
ByteBuffer outBuf=ByteBuffer.allocate(8);

try{
String srcStr;
String destStr;
while(true){

if (replenish(inChannel,inBuf)==-1) break;
srcStr=((ByteBuffer)(inBuf.flip())).asCharBuffer().toString();
inBuf.clear();
if (flag)
destStr=des.enc(srcStr,srcStr.length());
else
destStr=des.dec(srcStr,srcStr.length());
outBuf.clear();
if (destStr.length()==4){
for (int i = 0; i<4; i++) {
outBuf.putChar(destStr.charAt(i));
}
outBuf.flip();
}else{
outBuf.position(0);
outBuf.limit(2*destStr.length());
for (int i = 0; i<destStr.length(); i++) {
outBuf.putChar(destStr.charAt(i));
}
outBuf.flip();
}

try {
outChannel.write(outBuf);
outBuf.clear();
}catch (java.io.IOException ex) {
ex.printStackTrace(System.err);
}
}
System.out.println (inChannel.size());
System.out.println (outChannel.size());
System.out.println ("EoF reached.");
inFile.close();
outputFile.close();
}catch(java.io.IOException e){
e.printStackTrace(System.err);
System.exit(1);
}
}

public FileDES(String srcFileName,String destFileName,String inKey,boolean actionType){
this.srcFileName=srcFileName;
this.destFileName=destFileName;
this.actionType=actionType;
analyzePath();
srcFile=new File(srcFileName);
destFile=new File(destFileName);
this.inKey=inKey;
if (actionType==enc)
file_operate(enc);
else
file_operate(dec);
}

public static void main(String[] args){
String file1=System.getProperty("user.dir")+"/111.doc";
String file2=System.getProperty("user.dir")+"/222.doc";
String file3=System.getProperty("user.dir")+"/333.doc";
String passWord="1234ABCD";
FileDES fileDes=new FileDES(file1,file2,passWord,true);
FileDES fileDes1=new FileDES(file2,file3,passWord,false);
}

⑽ java des加密後的串比原字元串長

DESPlus.java

/**
* @author李國慶
* @company leemenz (C) right
* @timeNov 1, 200610:18:41 AM
* @version 1.0.0.0
* @package com.des
*/
package com.des;

import java.security.*;
import javax.crypto.*;

public class DESPlus {
private static String strDefaultKey = "national";

private Cipher encryptCipher = null;

private Cipher decryptCipher = null;

/**
* 將byte數組轉換為表示16進制值的字元串, 如:byte[]{8,18}轉換為:0813, 和public static byte[]
* hexStr2ByteArr(String strIn) 互為可逆的轉換過程
*
* @param arrB
*需要轉換的byte數組
* @return 轉換後的字元串
* @throws Exception
* 本方法不處理任何異常,所有異常全部拋出
*/
public static String byteArr2HexStr(byte[] arrB) throws Exception {
int iLen = arrB.length;
// 每個byte用兩個字元才能表示,所以字元串的長度是數組長度的兩倍
StringBuffer sb = new StringBuffer(iLen * 2);
for (int i = 0; i < iLen; i++) {
int intTmp = arrB[i];
// 把負數轉換為正數
while (intTmp < 0) {
intTmp = intTmp + 256;
}
// 小於0F的數需要在前面補0
if (intTmp < 16) {
sb.append("0");
}
sb.append(Integer.toString(intTmp, 16));
}
return sb.toString();
}

/**
* 將表示16進制值的字元串轉換為byte數組, 和public static String byteArr2HexStr(byte[] arrB)
* 互為可逆的轉換過程
*
* @param strIn
*需要轉換的字元串
* @return 轉換後的byte數組
* @throws Exception
* 本方法不處理任何異常,所有異常全部拋出
* @author <a href="mailto:[email protected]">LiGuoQing</a>
*/
public static byte[] hexStr2ByteArr(String strIn) throws Exception {
byte[] arrB = strIn.getBytes();
int iLen = arrB.length;

// 兩個字元表示一個位元組,所以位元組數組長度是字元串長度除以2
byte[] arrOut = new byte[iLen / 2];
for (int i = 0; i < iLen; i = i + 2) {
String strTmp = new String(arrB, i, 2);
arrOut[i / 2] = (byte) Integer.parseInt(strTmp, 16);
}
return arrOut;
}

/**
* 默認構造方法,使用默認密鑰
*
* @throws Exception
*/
public DESPlus() throws Exception {
this(strDefaultKey);
}

/**
* 指定密鑰構造方法
*
* @param strKey
*指定的密鑰
* @throws Exception
*/
public DESPlus(String strKey) throws Exception {
Security.addProvider(new com.sun.crypto.provider.SunJCE());
Key key = getKey(strKey.getBytes());

encryptCipher = Cipher.getInstance("DES");
encryptCipher.init(Cipher.ENCRYPT_MODE, key);

decryptCipher = Cipher.getInstance("DES");
decryptCipher.init(Cipher.DECRYPT_MODE, key);
}

/**
* 加密位元組數組
*
* @param arrB
*需加密的位元組數組
* @return 加密後的位元組數組
* @throws Exception
*/
public byte[] encrypt(byte[] arrB) throws Exception {
return encryptCipher.doFinal(arrB);
}

/**
* 加密字元串
*
* @param strIn
*需加密的字元串
* @return 加密後的字元串
* @throws Exception
*/
public String encrypt(String strIn) throws Exception {
return byteArr2HexStr(encrypt(strIn.getBytes()));
}

/**
* 解密位元組數組
*
* @param arrB
*需解密的位元組數組
* @return 解密後的位元組數組
* @throws Exception
*/
public byte[] decrypt(byte[] arrB) throws Exception {
return decryptCipher.doFinal(arrB);
}

/**
* 解密字元串
*
* @param strIn
*需解密的字元串
* @return 解密後的字元串
* @throws Exception
*/
public String decrypt(String strIn) throws Exception {
return new String(decrypt(hexStr2ByteArr(strIn)));
}

/**
* 從指定字元串生成密鑰,密鑰所需的位元組數組長度為8位 不足8位時後面補0,超出8位只取前8位
*
* @param arrBTmp
*構成該字元串的位元組數組
* @return 生成的密鑰
* @throws java.lang.Exception
*/
private Key getKey(byte[] arrBTmp) throws Exception {
// 創建一個空的8位位元組數組(默認值為0)
byte[] arrB = new byte[8];

// 將原始位元組數組轉換為8位
for (int i = 0; i < arrBTmp.length && i < arrB.length; i++) {
arrB[i] = arrBTmp[i];
}

// 生成密鑰
Key key = new javax.crypto.spec.SecretKeySpec(arrB, "DES");

return key;
}
}

測試程序Test.java

/**
* @author李國慶
* @company leemenz (C) right
* @timeNov 1, 200610:24:06 AM
* @version 1.0.0.0
* @package com.des
*/
package com.des;

/**
* @author Administrator
*
*/
public class Test {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
String test = "Hellow Word!";
//DESPlus des = new DESPlus();//默認密鑰
DESPlus des = new DESPlus("leemenz");//自定義密鑰
System.out.println("加密前的字元:"+test);
System.out.println("加密後的字元:"+des.encrypt(test));
System.out.println("解密後的字元:"+des.decrypt(des.encrypt(test)));
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
}
看看 對你有幫助的 不一定加密長度一樣的

閱讀全文

與javades亂碼相關的資料

熱點內容
優信二手車解壓後過戶 瀏覽:62
Windows常用c編譯器 瀏覽:778
關於改善國家網路安全的行政命令 瀏覽:833
安卓如何下載網易荒野pc服 瀏覽:654
javainetaddress 瀏覽:104
蘋果4s固件下載完了怎麼解壓 瀏覽:1003
命令zpa 瀏覽:286
python編譯器小程序 瀏覽:945
在app上看視頻怎麼光線調暗 瀏覽:540
可以中文解壓的解壓軟體 瀏覽:593
安卓卸載組件應用怎麼安裝 瀏覽:913
使用面向對象編程的方式 瀏覽:339
程序員項目經理的年終總結範文 瀏覽:929
內衣的加密設計用來幹嘛的 瀏覽:433
淮安數據加密 瀏覽:292
魔高一丈指標源碼 瀏覽:982
松下php研究所 瀏覽:168
c回調java 瀏覽:401
夢幻端游長安地圖互通源碼 瀏覽:746
電腦本地文件如何上傳伺服器 瀏覽:313