导航:首页 > 编程语言 > 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:leo841001@163.com">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乱码相关的资料

热点内容
如何进入2b2t服务器网易国服 浏览:530
java二进制转换为文件 浏览:13
java局部变量内存 浏览:633
linux解压tarzip 浏览:146
阿里传pdf 浏览:246
android打开系统相册 浏览:984
plc与单片机的关系 浏览:694
解压系列的动漫 浏览:440
能注册的跑酷游戏源码 浏览:981
wpe源码易语言 浏览:847
算法工程师不玩游戏 浏览:291
浙江ntp校时服务器配置云空间 浏览:834
心理有根源怎么解压 浏览:683
资金爆发指标源码 浏览:426
stata命令缩写 浏览:449
java写入文件内容 浏览:885
加密货购买途径 浏览:438
md5源码查询 浏览:331
单片机测量 浏览:303
deboor算法 浏览:839