当前位置:首页 » 操作系统 » javades算法实现

javades算法实现

发布时间: 2022-02-28 10:45:02

① 谁能帮我将java中的DES算法改成用C#来实现

C#也有DES加密

using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;

/// <summary>
/// DES加密解密
/// </summary>
public static class DES
{

//默认密钥向量
private static byte[] Keys = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };

/// <summary>
/// DES加密字符串
/// </summary>
/// <param name="encryptString">待加密的字符串</param>
/// <param name="encryptKey">加密密钥,要求为8位</param>
/// <returns>加密成功返回加密后的字符串,失败返回源串</returns>
public static string Encode(string encryptString, string encryptKey)
{
encryptKey = StringHelper.GetSubString(encryptKey, 8, "");
encryptKey = encryptKey.PadRight(8, ' ');
byte[] rgbKey = Encoding.UTF8.GetBytes(encryptKey.Substring(0, 8));
byte[] rgbIV = Keys;
byte[] inputByteArray = Encoding.UTF8.GetBytes(encryptString);
DESCryptoServiceProvider dCSP = new DESCryptoServiceProvider();
MemoryStream mStream = new MemoryStream();
CryptoStream cStream = new CryptoStream(mStream, dCSP.CreateEncryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
cStream.Write(inputByteArray, 0, inputByteArray.Length);
cStream.FlushFinalBlock();
return Convert.ToBase64String(mStream.ToArray());

}

/// <summary>
/// DES解密字符串
/// </summary>
/// <param name="decryptString">待解密的字符串</param>
/// <param name="decryptKey">解密密钥,要求为8位,和加密密钥相同</param>
/// <returns>解密成功返回解密后的字符串,失败返源串</returns>
public static string Decode(string decryptString, string decryptKey)
{
try
{
decryptKey = StringHelper.GetSubString(decryptKey, 8, "");
decryptKey = decryptKey.PadRight(8, ' ');
byte[] rgbKey = Encoding.UTF8.GetBytes(decryptKey);
byte[] rgbIV = Keys;
byte[] inputByteArray = Convert.FromBase64String(decryptString);
DESCryptoServiceProvider DCSP = new DESCryptoServiceProvider();

MemoryStream mStream = new MemoryStream();
CryptoStream cStream = new CryptoStream(mStream, DCSP.CreateDecryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
cStream.Write(inputByteArray, 0, inputByteArray.Length);
cStream.FlushFinalBlock();
return Encoding.UTF8.GetString(mStream.ToArray());
}
catch
{
return "";
}

}

}

② DES加密算法 java实现

c语言的源代码,供参考:

http://hi..com/gaojinshan/blog/item/8b2710c4ece4b3ce39db49e9.html

③ DES加密算法用java实现 有图形界面的

校内注册的时候,当我们选择省市,就会弹出一个所有大学的div,我们选择大学,大学名称就会进入文本框。谁有相关代码啊?包括怎么调用的,急!!!

④ java des算法

我用你的数据 测试的

des 加密用的 BASE64Encoder

结果是:TVRlhcWXA9qq7OxmxxF78HI89tF4w//R

⑤ 用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 CBC算法

给我发消息 我发你代码

⑦ Java中 DES加密算法

三个文件:
一:skey_DES.java
//对称秘钥生成及对象化保存
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;

public class Skey_DES
{
public static void main(String args[])throws Exception
{
KeyGenerator kg=KeyGenerator.getInstance("DESede");
kg.init(168);
SecretKey k=kg.generateKey();
FileOutputStream f=new FileOutputStream("key1.txt");
ObjectOutputStream b= new ObjectOutputStream(f);
b.writeObject(k);
}
};

二:SEnc.java
//对称秘钥加密,使用字节码
import java.io.*;
import java.security.*;
import javax.crypto.*;

public class SEnc
{
public static void main(String args[]) throws Exception
{
String s="";

FileInputStream f=new FileInputStream("key1.txt");
ObjectInputStream b=new ObjectInputStream(f);
Key k=(Key)b.readObject();
Cipher cp=Cipher.getInstance("DESede");
cp.init(Cipher.ENCRYPT_MODE,k);

byte ptext[]=s.getBytes("UTF8");
for(int i=0;i<ptext.length;i++)
{
System.out.print(ptext[i]+",");
}
System.out.println("");
byte ctext[]=cp.doFinal(ptext);
for(int i=0;i<ctext.length;i++)
{
System.out.print(ctext[i]+",");
}
FileOutputStream f2=new FileOutputStream("SEnc.txt");
f2.write(ctext);
}
};
三:SDec.java
//使用对称秘钥解密
import java.io.*;
import java.security.*;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

public class SDec
{
public static void main(String args[])throws Exception
{
FileInputStream f=new FileInputStream("SEnc.txt");
int num=f.available();
byte[] ctext=new byte[num];
f.read(ctext);

FileInputStream f2=new FileInputStream("key1.txt");
ObjectInputStream b=new ObjectInputStream(f2);
Key k=(Key)b.readObject();
Cipher cp=Cipher.getInstance("DESede");
cp.init(Cipher.DECRYPT_MODE,k);

byte[] ptext=cp.doFinal(ctext);
String p=new String(ptext,"UTF8");
System.out.println(p);
}
};

⑧ 如题,求Java的DES算法代码,可以用于解密用C语言写的DES加密算法!其中C语言代码见问题补充

加密算法可以加解密有三个条件,一个是随机源,一个是加密算法(aes,des, java和c++都有实现,这个你不用管),三是填充方法,保证这3个条件一致就可以成功加解密

⑨ 用C和Java语言,实现一个的DES算法逻辑

用java现成的库java.security.*;javax.crypto.*
KeyGenerator.getInstance
Cipher.getInstance
Cipher.DECRYPT_MODE
doFinal
等等

⑩ Java DES加密解密算法

bcprov-ext-jdk16-143.jar
应该放到你应用的classpath下。放ext中,修改java.security 我在windows系统下居然无效的。但在linux/unix下是好的

热点内容
ai清除缓存 发布:2024-11-15 23:12:38 浏览:602
电脑版我的世界如何退出服务器 发布:2024-11-15 23:00:39 浏览:312
哪里有存储器零售商 发布:2024-11-15 22:55:42 浏览:46
手机如何设置锁屏密码个性 发布:2024-11-15 22:44:08 浏览:417
mysql导入存储过程 发布:2024-11-15 22:43:18 浏览:638
net连接数据库代码 发布:2024-11-15 22:40:16 浏览:61
编程计算nk 发布:2024-11-15 22:35:07 浏览:833
数据库第一章 发布:2024-11-15 22:27:07 浏览:593
测试php性能工具 发布:2024-11-15 22:19:37 浏览:580
安卓手机怎么给王者荣耀差评 发布:2024-11-15 22:16:47 浏览:270