importjava.util.Scanner;
publicclass加密
{
privatestaticScannersc=newScanner(System.in);
publicstaticvoidmain(String[]Args)
{
System.out.println(" ================字元串加密演示===================== ");
init();
}
//初始化!
privatestaticvoidinit()
{
for(;;)
{
char[]arr=input();
jiaMi(arr,20140908);
jiaMi(20140908,arr);
}
}
//鍵盤錄取!
privatestaticchar[]input()
{
Strings=sc.nextLine();
inta=s.length();
char[]arr=newchar[a];
//char[]arr=s.toCharArray();
for(inti=0;i<s.length();i++)
{
arr[i]=s.charAt(i);
}
returnarr;
}
//加密!!
privatestaticvoidjiaMi(char[]arr,inta)
{
for(inti=0;i<arr.length;i++)
{
arr[i]=((char)(arr[i]^a));
}
System.out.println("加密完成!");
print(arr);
}
//解密!!
privatestaticvoidjiaMi(inta,char[]arr)
{
for(inti=0;i<arr.length;i++)
{
arr[i]=((char)(arr[i]^a));
}
System.out.println("解密完成");
print(arr);
}
//列印!!
privatestaticvoidprint(char[]arr)
{
for(inti=0;i<arr.length;i++)
{
System.out.print(arr[i]);
}
System.out.println(" ========================= ");
}
}
Ⅱ JAVA簡單文件加密 求JAVA源代碼
md5加密小例子:
import java.security.MessageDigest;
public class StringMD5 {
private final static String[] hexDigits = { "0", "1", "2", "3", "4", "5",
"6", "7", "8", "9", "a", "b", "c", "d", "e", "f" };
/**
* 轉換位元組數組為16進制字串
*
* @param b
* 位元組數組
* @return 16進制字串
*/
public static String byteArrayToHexString(byte[] b) {
StringBuffer resultSb = new StringBuffer();
for (int i = 0; i < b.length; i++) {
resultSb.append(byteToHexString(b[i]));
}
return resultSb.toString();
}
private static String byteToHexString(byte b) {
int n = b;
if (n < 0)
n = 256 + n;
int d1 = n / 16;
int d2 = n % 16;
return hexDigits[d1] + hexDigits[d2];
}
public static String MD5Encode(String origin) {
String resultString = null;
try {
resultString = new String(origin);
MessageDigest md = MessageDigest.getInstance("MD5");
resultString = byteArrayToHexString(md.digest(resultString
.getBytes()));
} catch (Exception ex) {
}
return resultString;
}
public static void main(String[] args) {
StringMD5 test = new StringMD5();
String var = test.MD5Encode("123456");
System.out.println(var);
}
}
Ⅲ 用JAVA設計一個簡單的加密、解密演算法,用該演算法來實現對數據的加密、解密
簡單的?
用異或就可以了..!
importjava.util.Scanner;
publicclass加密
{
privatestaticScannersc=newScanner(System.in);
publicstaticvoidmain(String[]Args)
{
System.out.println(" ================字元串加密演示===================== ");
init();
}
//初始化!
privatestaticvoidinit()
{
for(;;)
{
char[]arr=input();
jiaMi(arr,20140908);
jiaMi(20140908,arr);
}
}
//鍵盤錄取!
privatestaticchar[]input()
{
Strings=sc.nextLine();
inta=s.length();
char[]arr=newchar[a];
//char[]arr=s.toCharArray();
for(inti=0;i<s.length();i++)
{
arr[i]=s.charAt(i);
}
returnarr;
}
//加密!!
privatestaticvoidjiaMi(char[]arr,inta)
{
for(inti=0;i<arr.length;i++)
{
arr[i]=((char)(arr[i]^a));
}
System.out.println("加密完成!");
print(arr);
}
//解密!!
privatestaticvoidjiaMi(inta,char[]arr)
{
for(inti=0;i<arr.length;i++)
{
arr[i]=((char)(arr[i]^a));
}
System.out.println("解密完成");
print(arr);
}
//列印!!
privatestaticvoidprint(char[]arr)
{
for(inti=0;i<arr.length;i++)
{
System.out.print(arr[i]);
}
System.out.println(" ========================= ");
}
}
Ⅳ 用java編一個程序(加密解密的)謝謝了,大神幫忙啊
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"
Ⅳ 寫一個java加密程序
publicclass_Test2
{
publicstaticvoidmain(String[]args)throwsFileNotFoundException,IOException
{
//為了便於理解,所以有的部分為了通俗寫得不夠好
Scannersc=newScanner(System.in);
Stringline=sc.nextLine();
line=encrypt(line);
System.out.println(line);
newFileOutputStream("d:\word.txt").write((line+" ").getBytes());
}
//該方法是將一行數據簡單加密(愷撒加密)
privatestaticStringencrypt(Stringline){
char[]chars=line.toCharArray();
for(inti=0;i<chars.length;i++)
{
//由ASCII碼表得知大寫字母是65-90,小寫字母是97-122
if((chars[i]>=65&&chars[i]<90)||(chars[i]>=97&&chars[i]<122))
{
chars[i]=(char)(chars[i]+1);
}
if(chars[i]==90)
{
chars[i]='a';
}
if(chars[i]==122)
{
chars[i]='A';
}
}
returnnewString(chars);
}
}
Ⅵ 怎樣用java編寫一個加密的程序
這是考演算法的,看題題目是這樣
int[] a={'a','b','c',....'x','y','z'};
int[] b={'z','y','x',....'c','b','a'};
if(輸入的值是a[ i ]){
輸出b[ i ];
}
思路就是這樣了
Ⅶ 求一個用java編寫的可逆的加密演算法程序,自己寫的小程序也行。
public class mySecurity {
private static KeyGenerator keygen ;
private static SecretKey secretKey;
private static Cipher cipher;
private static mySecurity security = null;
private mySecurity(){
}
public static mySecurity getInstance() throws Exception{
if(security == null){
security = new mySecurity();
keygen = KeyGenerator.getInstance("AES");
secretKey = keygen.generateKey();
cipher =Cipher.getInstance("AES");
}
return security;
}
//加密
public String encrypt(String str) throws Exception{
cipher.init(Cipher.ENCRYPT_MODE,secretKey);
byte [] src = str.getBytes(); byte [] enc = cipher.doFinal(src);
return parseByte2HexStr(enc); }
//解密
public String decrypt(String str) throws Exception{
cipher.init(Cipher.DECRYPT_MODE,secretKey);
byte[] enc = parseHexStr2Byte(str); byte [] dec = cipher.doFinal(enc);
return new String(dec); }
/**將16進制轉換為二進制
* @param hexStr
* @return
*/
public static byte[] parseHexStr2Byte(String hexStr) {
if (hexStr.length() < 1)
return null;
byte[] result = new byte[hexStr.length()/2];
for (int i = 0;i< hexStr.length()/2; i++) {
int high = Integer.parseInt(hexStr.substring(i*2, i*2+1), 16);
int low = Integer.parseInt(hexStr.substring(i*2+1, i*2+2), 16);
result[i] = (byte) (high * 16 + low);
}
return result;
}
/**將二進制轉換成16進制
* @param buf
* @return
*/
public static String parseByte2HexStr(byte buf[]) {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < buf.length; i++) {
String hex = Integer.toHexString(buf[i] & 0xFF);
if (hex.length() == 1) {
hex = '0' + hex;
}
sb.append(hex.toUpperCase());
}
return sb.toString();
}
public static void main(String[] args) throws Exception{
String str = "abc haha 我";
String ss = mySecurity.getInstance().encrypt(str) ;
System.out.println(ss);
System.out.println(mySecurity.getInstance().decrypt(ss));
}
}
Ⅷ 用JAVA編寫加密程序。加密一個程序後需要密碼才能啟動。能不能說下祥細原理和相關技術。
加密從簡單到尖端可以有各種啊... 最簡單的你定死一個密碼,啟動的時候讓用戶輸入,輸對了就干你該乾的事情。不過聽起來這更像用戶驗證,而非加密
Ⅸ 如何用JAVA實現字元串簡單加密解密
java加密字元串可以使用des加密演算法,實例如下:
package test;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.security.*;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
/**
* 加密解密
*
* @author shy.qiu
* @since http://blog.csdn.net/qiushyfm
*/
public class CryptTest {
/**
* 進行MD5加密
*
* @param info
* 要加密的信息
* @return String 加密後的字元串
*/
public String encryptToMD5(String info) {
byte[] digesta = null;
try {
// 得到一個md5的消息摘要
MessageDigest alga = MessageDigest.getInstance("MD5");
// 添加要進行計算摘要的信息
alga.update(info.getBytes());
// 得到該摘要
digesta = alga.digest();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
// 將摘要轉為字元串
String rs = byte2hex(digesta);
return rs;
}
/**
* 進行SHA加密
*
* @param info
* 要加密的信息
* @return String 加密後的字元串
*/
public String encryptToSHA(String info) {
byte[] digesta = null;
try {
// 得到一個SHA-1的消息摘要
MessageDigest alga = MessageDigest.getInstance("SHA-1");
// 添加要進行計算摘要的信息
alga.update(info.getBytes());
// 得到該摘要
digesta = alga.digest();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
// 將摘要轉為字元串
String rs = byte2hex(digesta);
return rs;
}
// //////////////////////////////////////////////////////////////////////////
/**
* 創建密匙
*
* @param algorithm
* 加密演算法,可用 DES,DESede,Blowfish
* @return SecretKey 秘密(對稱)密鑰
*/
public SecretKey createSecretKey(String algorithm) {
// 聲明KeyGenerator對象
KeyGenerator keygen;
// 聲明 密鑰對象
SecretKey deskey = null;
try {
// 返回生成指定演算法的秘密密鑰的 KeyGenerator 對象
keygen = KeyGenerator.getInstance(algorithm);
// 生成一個密鑰
deskey = keygen.generateKey();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
// 返回密匙
return deskey;
}
/**
* 根據密匙進行DES加密
*
* @param key
* 密匙
* @param info
* 要加密的信息
* @return String 加密後的信息
*/
public String encryptToDES(SecretKey key, String info) {
// 定義 加密演算法,可用 DES,DESede,Blowfish
String Algorithm = "DES";
// 加密隨機數生成器 (RNG),(可以不寫)
SecureRandom sr = new SecureRandom();
// 定義要生成的密文
byte[] cipherByte = null;
try {
// 得到加密/解密器
Cipher c1 = Cipher.getInstance(Algorithm);
// 用指定的密鑰和模式初始化Cipher對象
// 參數:(ENCRYPT_MODE, DECRYPT_MODE, WRAP_MODE,UNWRAP_MODE)
c1.init(Cipher.ENCRYPT_MODE, key, sr);
// 對要加密的內容進行編碼處理,
cipherByte = c1.doFinal(info.getBytes());
} catch (Exception e) {
e.printStackTrace();
}
// 返回密文的十六進制形式
return byte2hex(cipherByte);
}
/**
* 根據密匙進行DES解密
*
* @param key
* 密匙
* @param sInfo
* 要解密的密文
* @return String 返回解密後信息
*/
public String decryptByDES(SecretKey key, String sInfo) {
// 定義 加密演算法,
String Algorithm = "DES";
// 加密隨機數生成器 (RNG)
SecureRandom sr = new SecureRandom();
byte[] cipherByte = null;
try {
// 得到加密/解密器
Cipher c1 = Cipher.getInstance(Algorithm);
// 用指定的密鑰和模式初始化Cipher對象
c1.init(Cipher.DECRYPT_MODE, key, sr);
// 對要解密的內容進行編碼處理
cipherByte = c1.doFinal(hex2byte(sInfo));
} catch (Exception e) {
e.printStackTrace();
}
// return byte2hex(cipherByte);
return new String(cipherByte);
}
// /////////////////////////////////////////////////////////////////////////////
/**
* 創建密匙組,並將公匙,私匙放入到指定文件中
*
* 默認放入mykeys.bat文件中
*/
public void createPairKey() {
try {
// 根據特定的演算法一個密鑰對生成器
KeyPairGenerator keygen = KeyPairGenerator.getInstance("DSA");
// 加密隨機數生成器 (RNG)
SecureRandom random = new SecureRandom();
// 重新設置此隨機對象的種子
random.setSeed(1000);
// 使用給定的隨機源(和默認的參數集合)初始化確定密鑰大小的密鑰對生成器
keygen.initialize(512, random);// keygen.initialize(512);
// 生成密鑰組
KeyPair keys = keygen.generateKeyPair();
// 得到公匙
PublicKey pubkey = keys.getPublic();
// 得到私匙
PrivateKey prikey = keys.getPrivate();
// 將公匙私匙寫入到文件當中
doObjToFile("mykeys.bat", new Object[] { prikey, pubkey });
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
}
/**
* 利用私匙對信息進行簽名 把簽名後的信息放入到指定的文件中
*
* @param info
* 要簽名的信息
* @param signfile
* 存入的文件
*/
public void signToInfo(String info, String signfile) {
// 從文件當中讀取私匙
PrivateKey myprikey = (PrivateKey) getObjFromFile("mykeys.bat", 1);
// 從文件中讀取公匙
PublicKey mypubkey = (PublicKey) getObjFromFile("mykeys.bat", 2);
try {
// Signature 對象可用來生成和驗證數字簽名
Signature signet = Signature.getInstance("DSA");
// 初始化簽署簽名的私鑰
signet.initSign(myprikey);
// 更新要由位元組簽名或驗證的數據
signet.update(info.getBytes());
// 簽署或驗證所有更新位元組的簽名,返回簽名
byte[] signed = signet.sign();
// 將數字簽名,公匙,信息放入文件中
doObjToFile(signfile, new Object[] { signed, mypubkey, info });
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 讀取數字簽名文件 根據公匙,簽名,信息驗證信息的合法性
*
* @return true 驗證成功 false 驗證失敗
*/
public boolean validateSign(String signfile) {
// 讀取公匙
PublicKey mypubkey = (PublicKey) getObjFromFile(signfile, 2);
// 讀取簽名
byte[] signed = (byte[]) getObjFromFile(signfile, 1);
// 讀取信息
String info = (String) getObjFromFile(signfile, 3);
try {
// 初始一個Signature對象,並用公鑰和簽名進行驗證
Signature signetcheck = Signature.getInstance("DSA");
// 初始化驗證簽名的公鑰
signetcheck.initVerify(mypubkey);
// 使用指定的 byte 數組更新要簽名或驗證的數據
signetcheck.update(info.getBytes());
System.out.println(info);
// 驗證傳入的簽名
return signetcheck.verify(signed);
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 將二進制轉化為16進制字元串
*
* @param b
* 二進制位元組數組
* @return String
*/
public String byte2hex(byte[] b) {
String hs = "";
String stmp = "";
for (int n = 0; n < b.length; n++) {
stmp = (java.lang.Integer.toHexString(b[n] & 0XFF));
if (stmp.length() == 1) {
hs = hs + "0" + stmp;
} else {
hs = hs + stmp;
}
}
return hs.toUpperCase();
}
/**
* 十六進制字元串轉化為2進制
*
* @param hex
* @return
*/
public byte[] hex2byte(String hex) {
byte[] ret = new byte[8];
byte[] tmp = hex.getBytes();
for (int i = 0; i < 8; i++) {
ret[i] = uniteBytes(tmp[i * 2], tmp[i * 2 + 1]);
}
return ret;
}
/**
* 將兩個ASCII字元合成一個位元組; 如:"EF"--> 0xEF
*
* @param src0
* byte
* @param src1
* byte
* @return byte
*/
public static byte uniteBytes(byte src0, byte src1) {
byte _b0 = Byte.decode("0x" + new String(new byte[] { src0 }))
.byteValue();
_b0 = (byte) (_b0 << 4);
byte _b1 = Byte.decode("0x" + new String(new byte[] { src1 }))
.byteValue();
byte ret = (byte) (_b0 ^ _b1);
return ret;
}
/**
* 將指定的對象寫入指定的文件
*
* @param file
* 指定寫入的文件
* @param objs
* 要寫入的對象
*/
public void doObjToFile(String file, Object[] objs) {
ObjectOutputStream oos = null;
try {
FileOutputStream fos = new FileOutputStream(file);
oos = new ObjectOutputStream(fos);
for (int i = 0; i < objs.length; i++) {
oos.writeObject(objs[i]);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
oos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 返回在文件中指定位置的對象
*
* @param file
* 指定的文件
* @param i
* 從1開始
* @return
*/
public Object getObjFromFile(String file, int i) {
ObjectInputStream ois = null;
Object obj = null;
try {
FileInputStream fis = new FileInputStream(file);
ois = new ObjectInputStream(fis);
for (int j = 0; j < i; j++) {
obj = ois.readObject();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
ois.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return obj;
}
/**
* 測試
*
* @param args
*/
public static void main(String[] args) {
CryptTest jiami = new CryptTest();
// 執行MD5加密"Hello world!"
System.out.println("Hello經過MD5:" + jiami.encryptToMD5("Hello"));
// 生成一個DES演算法的密匙
SecretKey key = jiami.createSecretKey("DES");
// 用密匙加密信息"Hello world!"
String str1 = jiami.encryptToDES(key, "Hello");
System.out.println("使用des加密信息Hello為:" + str1);
// 使用這個密匙解密
String str2 = jiami.decryptByDES(key, str1);
System.out.println("解密後為:" + str2);
// 創建公匙和私匙
jiami.createPairKey();
// 對Hello world!使用私匙進行簽名
jiami.signToInfo("Hello", "mysign.bat");
// 利用公匙對簽名進行驗證。
if (jiami.validateSign("mysign.bat")) {
System.out.println("Success!");
} else {
System.out.println("Fail!");
}
}
}
Ⅹ 怎樣為一個java程序加密 謝謝
只給編譯後的.jar文件,不給.java文件
不過要說明的是,java因為是位元組碼,所以沒有辦法防止被反編譯。
最多也就是做一下代碼混淆,比如把方法或變數名改成無意義的名稱,或者加一些完全無用的代碼進去,讓惡意攻擊的人難以看懂