.net的des加密方法,如何用java实现. 谢谢 分不够再加~~~
/// <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; }
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