菜鸟求三重DES加密,解密代码 RT...请各位大虾不吝赐教,小弟感激涕零... 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 import java.io.*; import java.security.*; import javax.crypto.*; import javax.crypto.spec.*; /** This program tests the DES cipher. Usage: java DESTest -genkey keyfile java DESTest -encrypt plaintext encrypted keyfile java DESTest -decrypt encrypted decrypted keyfile */ public class DESTest { public static void main(String[] args) { try { if (args[0].equals("-genkey")) { KeyGenerator keygen = KeyGenerator.getInstance("DES"); SecureRandom random = new SecureRandom(); keygen.init(random); SecretKey key = keygen.generateKey(); ObjectOutputStream out = new ObjectOutputStream( new FileOutputStream(args[1])); out.writeObject(key); out.close(); } else { int mode; if (args[0].equals("-encrypt")) mode = Cipher.ENCRYPT_MODE; else mode = Cipher.DECRYPT_MODE; ObjectInputStream keyIn = new ObjectInputStream( new FileInputStream(args[3])); Key key = (Key) keyIn.readObject(); keyIn.close(); InputStream in = new FileInputStream(args[1]); OutputStream out = new FileOutputStream(args[2]); Cipher cipher = Cipher.getInstance("DES"); cipher.init(mode, key); crypt(in, out, cipher); in.close(); out.close(); } } catch (IOException exception) { exception.printStackTrace(); } catch (GeneralSecurityException exception) { exception.printStackTrace(); } catch (ClassNotFoundException exception) { exception.printStackTrace(); } } /** Uses a cipher to transform the bytes in an input stream and sends the transformed bytes to an output stream. @param in the input stream @param out the output stream @param cipher the cipher that transforms the bytes */ public static void crypt(InputStream in, OutputStream out, Cipher cipher) throws IOException, GeneralSecurityException { int blockSize = cipher.getBlockSize(); int outputSize = cipher.getOutputSize(blockSize); byte[] inBytes = new byte[blockSize]; byte[] outBytes = new byte[outputSize]; int inLength = 0;; boolean more = true; while (more) { inLength = in.read(inBytes); if (inLength == blockSize) { int outLength = cipher.update(inBytes, 0, blockSize, outBytes); out.write(outBytes, 0, outLength); System.out.println(outLength); } else more = false; } if (inLength > 0) outBytes = cipher.doFinal(inBytes, 0, inLength); else outBytes = cipher.doFinal(); System.out.println(outBytes.length); out.write(outBytes); } }自己再改改吧 感谢rukhwill(志) ...只是我现在想把加密后的信息保存至数据库表中,而不是输出至文件,不知道代码应该作何改动呢? ?请问打印在屏幕上和文件中为什么结果不同呢 麻烦帮我修改一个正则表达式吧~ 怎样在jni中转换“public String myrun2(String name)”函数 有没有用JAVA做的好看点的界面呢? 请教内存泄漏 wati,notify,notifyAll为什么要在同步的情况下调用? 文件路径问题 一个简单的正则表达式求助,多谢! 短信問題 事件类问题 SQL JDBC 大家来帮忙看看这个程序,谢谢,100分,在线!!!
import java.security.*;
import javax.crypto.*;
import javax.crypto.spec.*; /**
This program tests the DES cipher. Usage:
java DESTest -genkey keyfile
java DESTest -encrypt plaintext encrypted keyfile
java DESTest -decrypt encrypted decrypted keyfile
*/
public class DESTest
{
public static void main(String[] args)
{
try
{
if (args[0].equals("-genkey"))
{
KeyGenerator keygen
= KeyGenerator.getInstance("DES");
SecureRandom random = new SecureRandom();
keygen.init(random);
SecretKey key = keygen.generateKey();
ObjectOutputStream out = new ObjectOutputStream(
new FileOutputStream(args[1]));
out.writeObject(key);
out.close();
}
else
{
int mode;
if (args[0].equals("-encrypt"))
mode = Cipher.ENCRYPT_MODE;
else
mode = Cipher.DECRYPT_MODE; ObjectInputStream keyIn = new ObjectInputStream(
new FileInputStream(args[3]));
Key key = (Key) keyIn.readObject();
keyIn.close(); InputStream in = new FileInputStream(args[1]);
OutputStream out = new FileOutputStream(args[2]);
Cipher cipher = Cipher.getInstance("DES");
cipher.init(mode, key); crypt(in, out, cipher);
in.close();
out.close();
}
}
catch (IOException exception)
{
exception.printStackTrace();
}
catch (GeneralSecurityException exception)
{
exception.printStackTrace();
}
catch (ClassNotFoundException exception)
{
exception.printStackTrace();
}
} /**
Uses a cipher to transform the bytes in an input stream
and sends the transformed bytes to an output stream.
@param in the input stream
@param out the output stream
@param cipher the cipher that transforms the bytes
*/
public static void crypt(InputStream in, OutputStream out,
Cipher cipher) throws IOException, GeneralSecurityException
{
int blockSize = cipher.getBlockSize();
int outputSize = cipher.getOutputSize(blockSize);
byte[] inBytes = new byte[blockSize];
byte[] outBytes = new byte[outputSize]; int inLength = 0;;
boolean more = true;
while (more)
{
inLength = in.read(inBytes);
if (inLength == blockSize)
{
int outLength
= cipher.update(inBytes, 0, blockSize, outBytes);
out.write(outBytes, 0, outLength);
System.out.println(outLength);
}
else more = false;
}
if (inLength > 0)
outBytes = cipher.doFinal(inBytes, 0, inLength);
else
outBytes = cipher.doFinal();
System.out.println(outBytes.length);
out.write(outBytes);
}
}
自己再改改吧
只是我现在想把加密后的信息保存至数据库表中,而不是输出至文件,不知道代码应该作何改动呢?