哈哈!不会是老师留给你的作业吧!在网上做牛!
目前创新互联已为上1000家的企业提供了网站建设、域名、网络空间、网站托管、服务器托管、企业网站设计、城西网站维护等服务,公司将坚持客户导向、应用为本的策略,正道将秉承"和谐、参与、激情"的文化,与客户和合作伙伴齐心协力一起成长,共同发展。
VERSION 4.00
Begin VB.Form Form1
Caption = "Sample"
ClientHeight = 705
ClientLeft = 3915
ClientTop = 3315
ClientWidth = 1800
ControlBox = 0 'False
Height = 1110
Left = 3855
LinkTopic = "Form1"
MaxButton = 0 'False
MinButton = 0 'False
ScaleHeight = 705
ScaleWidth = 1800
Top = 2970
Width = 1920
Begin VB.CommandButton Command1
Caption = "Command1"
Height = 495
Left = 240
TabIndex = 0
Top = 120
Width = 1215
End
End
Attribute VB_Name = "Form1"
Attribute VB_Creatable = False
Attribute VB_Exposed = False
Sub FileEncodeAndDecode(InputFile As String, OutputFile As String, PasswordKey As String)
Dim temp As Single
Dim Char As String * 1
Dim XORMask As Single
Dim temp1 As Integer
Open InputFile For Binary As #1
Open OutputFile For Binary As #2
For x = 1 To Len(PasswordKey)
temp = Asc(Mid$(PasswordKey, x, 1))
For y = 1 To temp
temp1 = Rnd
Next y
' Re-seed to throw off prying eyes
Randomize temp1
Next x
Counter = 0
For z = 1 To FileLen(InputFile)
'Generate random mask
XORMask = Int(Rnd * 256)
'Get the char change it
Get 1, , Char
Char = Chr$((Asc(Char) Xor XORMask))
Put 2, , Char
Counter = Counter + 1
If Counter Len(PasswordKey) Then Counter = 1
' Pull random numbers from the hat
For x = 1 To (Asc(Mid$(PasswordKey, Counter, 1)) * 2)
temp = Rnd
Next x
Next z
Close #1
Close #2
End Sub
Private Sub Command1_Click()
Dim InputFile As String
Dim OutputFile As String
Dim PasswordKey As String
InputFile = InputBox("Enter a filename to encode")
OutputFile = InputBox("Enter the new filename this will become ")
PasswordKey = InputBox("Enter the password (key)")
Call FileEncodeAndDecode(InputFile, OutputFile, PasswordKey)
MsgBox "File written to " + OutputFile
End
End Sub
Private Sub Form_Load()
Command1.Caption = "Code/Decode"
End Sub
Form=enc-dec.frm
Reference=*\G{BEF6E001-A874-101A-8BBA-00AA00300CAB}#2.0#0#D:\WINDOWS\SYSTEM\OLEPRO32.DLL#Standard OLE Types
Reference=*\G{00025E01-0000-0000-C000-000000000046}#3.0#0#D:\PROGRAM FILES\COMMON FILES\MICROSOFT SHARED\DD:\PROGRAM FIL#Microsoft DAO 3.0 Object Library
ProjWinSize=113,438,221,107
ProjWinShow=2
Name="Project1"
HelpContextID="0"
StartMode=0
VersionCompatible32="0"
MajorVer=1
MinorVer=0
RevisionVer=0
AutoIncrementVer=0
ServerSupportFiles=0
VersionCompanyName="Southern Cross Software"
md5加密:
package com.ncs.pki.util;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class MD5Test {
private static MessageDigest digest = null;
public synchronized static final String hash(String data) {
if (digest == null) {
try {
digest = MessageDigest.getInstance("MD5");
} catch (NoSuchAlgorithmException nsae) {
System.err.println(
"Failed to load the MD5 MessageDigest. "
+ "Jive will be unable to function normally.");
nsae.printStackTrace();
}
}
// Now, compute hash.
digest.update(data.getBytes());
return encodeHex(digest.digest());
}
public static final String encodeHex(byte[] bytes) {
StringBuffer buf = new StringBuffer(bytes.length * 2);
int i;
for (i = 0; i bytes.length; i++) {
if (((int) bytes[i] 0xff) 0x10) {
buf.append("0");
}
buf.append(Long.toString((int) bytes[i] 0xff, 16));
}
return buf.toString();
}
public static String test(){
return null;
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println(MD5Test.hash("123456"));
}
}
3des加密:
package com.ncs.pki.util;
import java.security.Key;
import java.security.SecureRandom;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
public class DesEncrypt {
/**
*
* 使用DES加密与解密,可对byte[],String类型进行加密与解密 密文可使用String,byte[]存储.
*
* 方法: void getKey(String strKey)从strKey的字条生成一个Key
*
* String getEncString(String strMing)对strMing进行加密,返回String密文 String
* getDesString(String strMi)对strMin进行解密,返回String明文
*
*byte[] getEncCode(byte[] byteS)byte[]型的加密 byte[] getDesCode(byte[]
* byteD)byte[]型的解密
*/
Key key;
/**
* 根据参数生成KEY
*
* @param strKey
*/
public void getKey(String strKey) {
try {
KeyGenerator _generator = KeyGenerator.getInstance("DES");
_generator.init(new SecureRandom(strKey.getBytes()));
this.key = _generator.generateKey();
_generator = null;
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 加密String明文输入,String密文输出
*
* @param strMing
* @return
*/
public String getEncString(String strMing) {
byte[] byteMi = null;
byte[] byteMing = null;
String strMi = "";
BASE64Encoder base64en = new BASE64Encoder();
try {
byteMing = strMing.getBytes("UTF8");
byteMi = this.getEncCode(byteMing);
strMi = base64en.encode(byteMi);
} catch (Exception e) {
e.printStackTrace();
} finally {
base64en = null;
byteMing = null;
byteMi = null;
}
return strMi;
}
/**
* 解密 以String密文输入,String明文输出
*
* @param strMi
* @return
*/
public String getDesString(String strMi) {
BASE64Decoder base64De = new BASE64Decoder();
byte[] byteMing = null;
byte[] byteMi = null;
String strMing = "";
try {
byteMi = base64De.decodeBuffer(strMi);
byteMing = this.getDesCode(byteMi);
strMing = new String(byteMing, "UTF8");
} catch (Exception e) {
e.printStackTrace();
} finally {
base64De = null;
byteMing = null;
byteMi = null;
}
return strMing;
}
/**
* 加密以byte[]明文输入,byte[]密文输出
*
* @param byteS
* @return
*/
private byte[] getEncCode(byte[] byteS) {
byte[] byteFina = null;
Cipher cipher;
try {
cipher = Cipher.getInstance("DES");
cipher.init(Cipher.ENCRYPT_MODE, key);
byteFina = cipher.doFinal(byteS);
} catch (Exception e) {
e.printStackTrace();
} finally {
cipher = null;
}
return byteFina;
}
/**
* 解密以byte[]密文输入,以byte[]明文输出
*
* @param byteD
* @return
*/
private byte[] getDesCode(byte[] byteD) {
Cipher cipher;
byte[] byteFina = null;
try {
cipher = Cipher.getInstance("DES");
cipher.init(Cipher.DECRYPT_MODE, key);
byteFina = cipher.doFinal(byteD);
} catch (Exception e) {
e.printStackTrace();
} finally {
cipher = null;
}
return byteFina;
}
public static void main(String[] args) {
System.out.println("des demo");
DesEncrypt des = new DesEncrypt();// 实例化一个对像
des.getKey("MYKEY");// 生成密匙
System.out.println("key=MYKEY");
String strEnc = des.getEncString("111111");// 加密字符串,返回String的密文
System.out.println("密文=" + strEnc);
String strDes = des.getDesString(strEnc);// 把String 类型的密文解密
System.out.println("明文=" + strDes);
}
}
public static void main(String[] args) throws Exception {
String data = "itxxz";
System.out.println("字符串:itxxz");
System.err.println("加密:"+encrypt(data));
System.err.println("解密:"+decrypt(encrypt(data)));
}
运行结果:
由于代码太多,可到 itxxz.com/a/javashili/2014/1217/encrypt_decrypt.html 查看,注释也比较完整,清晰易懂
/** * BASE64解密 * * @param key * @return * @throws Exception */
public static byte[] decryptBASE64(String key) throws Exception { return (new BASE64Decoder()).decodeBuffer(key); } /** * BASE64加密 * * @param key * @return * @throws Exception */ public static String encryptBASE64(byte[] key) throws Exception { return (new BASE64Encoder()).encodeBuffer(key); }
最简单的就一个FOR循环要加密的文件 再声明一个字符串 遍历 要加密的文件和字符串进行位与操作或之类操作
类似这样-
import java.util.Arrays;
public class Test {
public static void main(String[] args) {
String str = "hello";
byte[] strCode = str.getBytes();
System.out.println("原始信息字节码:"+Arrays.toString(strCode));
String key = "abcde";
byte[] keyCode = key.getBytes();
System.out.println("密钥字节码:"+Arrays.toString(keyCode));
byte[] finallyCode = new byte[strCode.length];
for(int i=0;istr.length();i++){
finallyCode[i] = (byte) (strCode[i] ^ keyCode[i]);
}
System.out.println("加密后的字节码:"+Arrays.toString(finallyCode));
System.out.println("加密后的字符串:"+new String(finallyCode));
//============解密
for(int i=0;istr.length();i++){
finallyCode[i] = (byte) (finallyCode[i] ^ keyCode[i]);
}
System.out.println("解密后的字节码:"+Arrays.toString(finallyCode));
System.out.println("解密后的字符串:"+new String(finallyCode));
}
}
售后响应及时
7×24小时客服热线数据备份
更安全、更高效、更稳定价格公道精准
项目经理精准报价不弄虚作假合作无风险
重合同讲信誉,无效全额退款