.net的des加密方法,如何用java实现. 谢谢 分不够再加~~~
- C# code
/// <summary> /// 使用指定的加密算法、用随机密钥加密字符串 /// </summary> /// <param name="Source">要加密的字符串</param> /// <returns>加密结果</returns> public string Encrypting(string Source,string Key) { #region temp string strTemp = Key; if(strTemp.Length * 8 > objCryptoService.LegalKeySizes[0].MaxSize) { // 如果密钥长度太长得话,截 strTemp = strTemp.Substring(0,objCryptoService.LegalKeySizes[0].MaxSize / 8); } if(strTemp.Length * 8 < objCryptoService.LegalKeySizes[0].MinSize) { // 如果密钥长度太短得的话,在右边补空格 strTemp = strTemp.PadRight(objCryptoService.LegalKeySizes[0].MinSize / 8); } if(objCryptoService.LegalKeySizes[0].SkipSize != 0 && (strTemp.Length * 8) % objCryptoService.LegalKeySizes[0].SkipSize != 0) { // 如果密钥长度不是密钥长度间隔单位的倍数,则补空格。 strTemp = strTemp.PadRight(objCryptoService.LegalKeySizes[0].MaxSize / 8); } byte[] _Key = System.Text.Encoding.Default.GetBytes(strTemp); objCryptoService.Key = _Key; objCryptoService.IV = _Key; #endregion // 创建内存中的数据流 System.IO.MemoryStream objMemoryStream = new System.IO.MemoryStream(); // 创建加密器 ICryptoTransform objEncryptor = objCryptoService.CreateEncryptor(); // 创建加密转换文件流的加密流 CryptoStream objCryptoStream = new CryptoStream(objMemoryStream, objEncryptor, CryptoStreamMode.Write); StreamWriter writer = new StreamWriter(objCryptoStream); writer.Write(Source); writer.Flush(); objCryptoStream.FlushFinalBlock(); // 获取输出 byte[] bytOut = new byte[objMemoryStream.Length]; objMemoryStream.Position = 0; objMemoryStream.Read(bytOut,0,bytOut.Length); string strDest = System.Convert.ToBase64String(bytOut); objCryptoService.Clear(); objMemoryStream.Close(); writer.Close(); return strDest; }[解决办法]
不懂.net,帮顶~
[解决办法]
我记得JAVA有专门的类来实现DES加密,但是我记不起在哪了。
[解决办法]
网上应该有专门加密的类吧,不过自己没用过,帮顶啦,呵呵.
[解决办法]
看看这个例子对你有用吗?
JAVA里使用 DES 加密解密的例子
[解决办法]
是这个不
http://wang-tao.spaces.live.com/blog/cns!136f98ef1442f5a9!511.entry
听说 java是ECB模式,.net是CBC模式。
[解决办法]
AES算法,看下.
import java.io.*;
import java.security.*;
import javax.crypto.*;
/**
* AES算法生成密和文件加解密的。
* @author Richard
* AES--DES算法的后版本,由于DES算法可以通法破,因此不推使用。
*/
public class AESImpl {
//生AES密
public void createKey() {
try{
KeyGenerator keygen = KeyGenerator.getInstance("AES");
SecureRandom random = new SecureRandom();
keygen.init(random);
SecretKey key = keygen.generateKey();
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("aesKey.txt"));
out.writeObject(key);
out.close();
}catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}
}
public void run(String code){
int mode = Cipher.ENCRYPT_MODE;
String inputFileName = "encode.txt";//要加密的文件名
String outputFileName = "aesDecode.txt";//加密后的文件名
if("DECODE".equals(code)){
mode = Cipher.DECRYPT_MODE;
inputFileName = "aesDecode.txt";//要解密的文件名
outputFileName = "encode.txt";//解密后的文件名
}
try{
//入AES密文件
ObjectInputStream keyin = new ObjectInputStream(new FileInputStream("aesKey.txt"));
Key key = (Key)keyin.readObject();
keyin.close();
InputStream in = new FileInputStream(inputFileName);
OutputStream out = new FileOutputStream(outputFileName);
Cipher cipher = Cipher.getInstance("AES");
cipher.init(mode, key);
//加解密
crypt(in,out,cipher);
in.close();
out.close();
}catch (Exception e) {
e.printStackTrace();
}
}
public static void crypt(InputStream in, OutputStream out, Cipher cipher) throws IOException, ShortBufferException, 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);
}
else more = false;
}
if(inLength > 0){
outBytes = cipher.doFinal(inBytes,0,inLength);
}
else{
outBytes = cipher.doFinal();
}
out.write(outBytes);
}
public static void main(String[] args){
AESImpl _aes = new AESImpl();
//_aes.createKey();
_aes.run("DECODE");
}
}
[解决办法]
up