java中如何实现对文件和字符串加密. 解密?

Python09

java中如何实现对文件和字符串加密. 解密?,第1张

你好,加密的方式有很多中,如传统加密,后期的分组加密,序列流加密,这些是对称加密,现在有著名的非对称加密。

java的扩展包很好的实现了你需要的功能。这个包在java.security.*当然了还有很多好的加密方法,在sun的第三方jar包中有。目前密码加密使用用的是MD5加密,这个是单向加密,不可以解密。要想实现加密和解密,那么就需要学习密码学的知识。

希望对你有所帮助。

最简单的就一个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=0i<str.length()i++){

finallyCode[i] = (byte) (strCode[i] ^ keyCode[i])

}

System.out.println("加密后的字节码:"+Arrays.toString(finallyCode))

System.out.println("加密后的字符串:"+new String(finallyCode))

//============解密

for(int i=0i<str.length()i++){

finallyCode[i] = (byte) (finallyCode[i] ^ keyCode[i])

}

System.out.println("解密后的字节码:"+Arrays.toString(finallyCode))

System.out.println("解密后的字符串:"+new String(finallyCode))

}

}

哈哈!不会是老师留给你的作业吧!在网上做牛!

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"