java可逆演算法
1. 求一個用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));
}
}
2. java中,用不定長字元串生成定長字元串,要求可逆
你說的這個事情是不可能完成的,謝謝
簡單的抽屜原理就能說明呀
如果 被轉換的 字元串的長度為a,轉換後的長度為b
那麼由字元的范圍c可知,a能表示的各種組合為 c的a次方,b能表示的各種組合為c的b次方
當 a>b 的時候,將所有可能出現的字元串都進行轉化,無論轉換的方式為什麼
那麼必然有超過2個源字元串指向了同一個目標,那麼目標還原的時候就不知道是哪一個了
所以說 MD5本身不是加密演算法,是簽名演算法!!這個很重要!
希望你看懂了……
3. android,java 通用的加密解密方式有幾種
移動端越來越火了,我們在開發過程中,總會碰到要和移動端打交道的場景,比如.NET和android或者iOS的打交道。為了讓數據交互更安全,我們需要對數據進行加密傳輸。今天研究了一下,把幾種語言的加密都實踐了一遍,實現了.NET,java(android),iOS都同一套的加密演算法,下面就分享給大家。
AES加密有多種演算法模式,下面提供兩套模式的可用源碼。
加密方式:
先將文本AES加密
返回Base64轉碼
解密方式:
將數據進行Base64解碼
進行AES解密
一、CBC(Cipher Block Chaining,加密塊鏈)模式
是一種循環模式,前一個分組的密文和當前分組的明文異或操作後再加密,這樣做的目的是增強破解難度.
密鑰
密鑰偏移量
java/adroid加密AESOperator類:
package com.bci.wx.base.util;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
/**
* AES 是一種可逆加密演算法,對用戶的敏感信息加密處理 對原始數據進行AES加密後,在進行Base64編碼轉化;
*/
public class AESOperator {
/*
* 加密用的Key 可以用26個字母和數字組成 此處使用AES-128-CBC加密模式,key需要為16位。
*/
private String sKey = "smkldospdosldaaa";//key,可自行修改
private String ivParameter = "0392039203920300";//偏移量,可自行修改
private static AESOperator instance = null;
private AESOperator() {
}
public static AESOperator getInstance() {
if (instance == null)
instance = new AESOperator();
return instance;
}
public static String Encrypt(String encData ,String secretKey,String vector) throws Exception {
if(secretKey == null) {
return null;
}
if(secretKey.length() != 16) {
return null;
}
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
byte[] raw = secretKey.getBytes();
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
IvParameterSpec iv = new IvParameterSpec(vector.getBytes());// 使用CBC模式,需要一個向量iv,可增加加密演算法的強度
cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
byte[] encrypted = cipher.doFinal(encData.getBytes("utf-8"));
return new BASE64Encoder().encode(encrypted);// 此處使用BASE64做轉碼。
}
// 加密
public String encrypt(String sSrc) throws Exception {
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
byte[] raw = sKey.getBytes();
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
IvParameterSpec iv = new IvParameterSpec(ivParameter.getBytes());// 使用CBC模式,需要一個向量iv,可增加加密演算法的強度
cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
byte[] encrypted = cipher.doFinal(sSrc.getBytes("utf-8"));
return new BASE64Encoder().encode(encrypted);// 此處使用BASE64做轉碼。
}
// 解密
public String decrypt(String sSrc) throws Exception {
try {
byte[] raw = sKey.getBytes("ASCII");
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
IvParameterSpec iv = new IvParameterSpec(ivParameter.getBytes());
cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);
byte[] encrypted1 = new BASE64Decoder().decodeBuffer(sSrc);// 先用base64解密
byte[] original = cipher.doFinal(encrypted1);
String originalString = new String(original, "utf-8");
return originalString;
} catch (Exception ex) {
return null;
}
}
public String decrypt(String sSrc,String key,String ivs) throws Exception {
try {
byte[] raw = key.getBytes("ASCII");
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
IvParameterSpec iv = new IvParameterSpec(ivs.getBytes());
cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);
byte[] encrypted1 = new BASE64Decoder().decodeBuffer(sSrc);// 先用base64解密
byte[] original = cipher.doFinal(encrypted1);
String originalString = new String(original, "utf-8");
return originalString;
} catch (Exception ex) {
return null;
}
}
public static String encodeBytes(byte[] bytes) {
StringBuffer strBuf = new StringBuffer();
for (int i = 0; i < bytes.length; i++) {
strBuf.append((char) (((bytes[i] >> 4) & 0xF) + ((int) 'a')));
strBuf.append((char) (((bytes[i]) & 0xF) + ((int) 'a')));
}
return strBuf.toString();
}
4. 我想在C++里加密一個中文名字,如張三,然後在JAVA里解密,怎麼做
這取決於加密演算法比如說RSA演算法只要在java裡面保存私有密鑰然後C++傳遞給java共有密鑰然後再解開就好了.如果你要一個簡單的方法就將張三轉換為byte數組之後每一項加1,然後java這邊每項減1然後再再轉換為字元串就行,在復雜點就是有個規律C++和java都按這個規律加減就行(注意界限255+1之後變為0).
5. java 密碼加密後為什麼不能解密
首先確認你的加密演算法是可逆的,比如DES/RSA,如果是MD5/SHA這類的哈希演算法是沒有對應演算法解密的。
其次確認你的解密演算法與加密時是匹配的,演算法和參數都要匹配。
6. java求逆演算法的解析,告訴我詳細的演算法,或者是什麼公式要大神幫忙,要快...
是矩陣的逆嗎?
A^(-1)=(Aij)^T/|A|
其中Aij是元素aij的代數餘子式。
7. Java 加密解密的方法都有哪些
加密解密並非java才有的,所有編程語言都有加密和解密。
目前的加密解密主要可分為以下2大類:
對稱秘鑰加密:如DES演算法,3DES演算法,TDEA演算法,Blowfish演算法,RC5演算法,IDEA演算法等。其主要特點是加密方和解密方都有同一個密碼,加密方和解密方可以使用秘鑰任意加密解密。
非對稱密碼加密:這種加密方式加密方僅有加密秘鑰,對加密後的密文無法反向解密,解密方僅有解密秘鑰,無法對明文進行加密。
另外還有一些摘要演算法,比如MD5和HASH此類演算法不可逆,但經常用來作為確認欄位或者對一些重要匹配信息簽名防止明文內容被修改。
8. 用JAVA設計一個簡單的加密、解密演算法,用該演算法來實現對數據的加密、解密
給你個MD5的加密演算法
package test;
import java.security.MessageDigest;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Company: </p>
* @author unascribed
* @version 1.0
*/
public class StringUtil {
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){
System.err.println(MD5Encode("a"));
}
}
在RFC 1321中,給出了Test suite用來檢驗你的實現是否正確:
MD5 ("") =
MD5 ("a") =
MD5 ("abc") =
MD5 ("message digest") =
MD5 ("abcdefghijklmnopqrstuvwxyz") =
9. JAVA如何對URL進行加密和解密啊
URLDecoder和URLEncoder應該是不行的,程序員輕易的就能解碼修改參數後重新編碼。
比較合適的就是RSA加密了,只要兩個伺服器共用一個密鑰,一個加密,另一個收到後再用密鑰解密就行。因為是整數加密,所以在沒有證書的情況下基本無法解密的。
des加密也是不錯的選擇,比RSA簡單。
如果有能力也可以自己寫一個簡單的加密方法。