「java中明文加密」文件加密 java
本篇文章给大家谈谈java中明文加密,以及文件加密 java对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:
- 1、如何用java对数据加密,生成的密文是唯一的
- 2、java密码加密与解密
- 3、求java大佬指点一、 编写一个Java应用程序:Ex3_1.java,对下面的明文进行加密处理并输出:
- 4、java的DES加密128位的明文为什么得到的密文却是192位的
- 5、JAVA从控制台中输入一段明文然后加密为ASC
- 6、java对文件编写明文密文问题
如何用java对数据加密,生成的密文是唯一的
用户提供的是明文,数据库里面存储的是密文
不管怎么样,加密也好,解密也好,如果要比较相等性,这两个过程肯定要有一个,这个是没有选择的,需要提高性能的话只能做两点:
1、将用户的明文加密为密文后再与数据库中的比较,原因是这样只加密一次就可以,如果解密的话就要把数据库的密文全部解密,这是不现实的
2、在密文所在的列上建立索引,增加搜索速度,这个速度增长是很显著的,虽然会失去一些插入性能。
3、将对应的SQL写成存储过程。省去预编译的时间。这个速度的提高也是很明显的。
至于你说的“怎么能保证不一样得明文加密后生成不一样得密文”
MD5就可以
MD5有两个特性:
1、任意两段明文数据,加密以后的密文不会是相同的
2、任意一段明文数据,经过加密以后,其结果永远是不变的
网上MD5加密的类应该有写好的
大致上方法就是这样了,都做到的话应该没有问题了,不会影响你的性能的
java密码加密与解密
以下两个类可以很方便的完成字符串的加密和解密
加密 CryptHelper encrypt(password)
解密 CrypHelper decrypt(password)
代码如下
CryptUtils java
[java]
package gdie lab crypt;
import java io IOException;
import javax crypto Cipher;
import javax crypto KeyGenerator;
import javax crypto SecretKey;
import apache xerces internal impl dv util Base ;
public class CryptUtils {
private static String Algorithm = DES ;
private static byte[] DEFAULT_KEY=new byte[] { };
private static String VALUE_ENCODING= UTF ;
/**
* 生成密钥
*
* @return byte[] 返回生成的密钥
* @throws exception
* 扔出异常
*/
public static byte[] getSecretKey() throws Exception {
KeyGenerator keygen = KeyGenerator getInstance(Algorithm)
SecretKey deskey = keygen generateKey()
// if (debug ) System out println ( 生成密钥 +byte hex (deskey getEncoded
// ()))
return deskey getEncoded()
}
/**
* 将指定的数据根据提供的密钥进行加密
*
* @param input
* 需要加密的数据
* @param key
* 密钥
* @return byte[] 加密后的数据
* @throws Exception
*/
public static byte[] encryptData(byte[] input byte[] key) throws Exception {
SecretKey deskey = new javax crypto spec SecretKeySpec(key Algorithm)
// if (debug )
// {
// System out println ( 加密前的二进串 +byte hex (input ))
// System out println ( 加密前的字符串 +new String (input ))
//
// }
Cipher c = Cipher getInstance(Algorithm)
c init(Cipher ENCRYPT_MODE deskey)
byte[] cipherByte = c doFinal(input)
// if (debug ) System out println ( 加密后的二进串 +byte hex (cipherByte ))
return cipherByte;
}
public static byte[] encryptData(byte[] input) throws Exception {
return encryptData(input DEFAULT_KEY)
}
/**
* 将给定的已加密的数据通过指定的密钥进行解密
*
* @param input
* 待解密的数据
* @param key
* 密钥
* @return byte[] 解密后的数据
* @throws Exception
*/
public static byte[] decryptData(byte[] input byte[] key) throws Exception {
SecretKey deskey = new javax crypto spec SecretKeySpec(key Algorithm)
// if (debug ) System out println ( 解密前的信息 +byte hex (input ))
Cipher c = Cipher getInstance(Algorithm)
c init(Cipher DECRYPT_MODE deskey)
byte[] clearByte = c doFinal(input)
// if (debug )
// {
// System out println ( 解密后的二进串 +byte hex (clearByte ))
// System out println ( 解密后的字符串 +(new String (clearByte )))
//
// }
return clearByte;
}
public static byte[] decryptData(byte[] input) throws Exception {
return decryptData(input DEFAULT_KEY)
}
/**
* 字节码转换成 进制字符串
*
* @param byte[] b 输入要转换的字节码
* @return String 返回转换后的 进制字符串
*/
public static String byte hex(byte[] bytes) {
StringBuilder hs = new StringBuilder()
for(byte b : bytes)
hs append(String format( % $ X b))
return hs toString()
}
public static byte[] hex byte(String content) {
int l=content length()》 ;
byte[] result=new byte[l];
for(int i= ;il;i++) {
int j=i《 ;
String s=content substring(j j+ )
result[i]=Integer valueOf(s ) byteValue()
}
return result;
}
/**
* 将字节数组转换为base 编码字符串
* @param buffer
* @return
*/
public static String bytesToBase (byte[] buffer) {
//BASE Encoder en=new BASE Encoder()
return Base encode(buffer)
// return encoder encode(buffer)
}
/**
* 将base 编码的字符串解码为字节数组
* @param value
* @return
* @throws IOException
*/
public static byte[] base ToBytes(String value) throws IOException {
//return Base decodeToByteArray(value)
// System out println(decoder decodeBuffer(value))
// return decoder decodeBuffer(value)
return Base decode(value)
}
/**
* 加密给定的字符串
* @param value
* @return 加密后的base 字符串
*/
public static String encryptString(String value) {
return encryptString(value DEFAULT_KEY)
}
/**
* 根据给定的密钥加密字符串
* @param value 待加密的字符串
* @param key 以BASE 形式存在的密钥
* @return 加密后的base 字符串
* @throws IOException
*/
public static String encryptString(String value String key) throws IOException {
return encryptString(value base ToBytes(key))
}
/**
* 根据给定的密钥加密字符串
* @param value 待加密的字符串
* @param key 字节数组形式的密钥
* @return 加密后的base 字符串
*/
public static String encryptString(String value byte[] key) {
try {
byte[] data=value getBytes(VALUE_ENCODING)
data=CryptUtils encryptData(data key)
return bytesToBase (data)
} catch (Exception e) {
// TODO Auto generated catch block
e printStackTrace()
return null;
}
}
/**
* 解密字符串
* @param value base 形式存在的密文
* @return 明文
*/
public static String decryptString(String value) {
return decryptString(value DEFAULT_KEY)
}
/**
* 解密字符串
* @param value base 形式存在的密文
* @param key base 形式存在的密钥
* @return 明文
* @throws IOException
*/
public static String decryptString(String value String key) throws IOException {
String s=decryptString(value base ToBytes(key))
return s;
}
/**
* 解密字符串
* @param value base 形式存在的密文
* @param key 字节数据形式存在的密钥
* @return 明文
*/
public static String decryptString(String value byte[] key) {
try {
byte[] data=base ToBytes(value)
data=CryptUtils decryptData(data key)
return new String(data VALUE_ENCODING)
}catch(Exception e) {
e printStackTrace()
return null;
}
}
}
package gdie lab crypt;
import java io IOException;
import javax crypto Cipher;
import javax crypto KeyGenerator;
import javax crypto SecretKey;
import apache xerces internal impl dv util Base ;
public class CryptUtils {
private static String Algorithm = DES ;
private static byte[] DEFAULT_KEY=new byte[] { };
private static String VALUE_ENCODING= UTF ;
/**
* 生成密钥
*
* @return byte[] 返回生成的密钥
* @throws exception
* 扔出异常
*/
public static byte[] getSecretKey() throws Exception {
KeyGenerator keygen = KeyGenerator getInstance(Algorithm)
SecretKey deskey = keygen generateKey()
// if (debug ) System out println ( 生成密钥 +byte hex (deskey getEncoded
// ()))
return deskey getEncoded()
}
/**
* 将指定的数据根据提供的密钥进行加密
*
* @param input
* 需要加密的数据
* @param key
* 密钥
* @return byte[] 加密后的数据
* @throws Exception
*/
public static byte[] encryptData(byte[] input byte[] key) throws Exception {
SecretKey deskey = new javax crypto spec SecretKeySpec(key Algorithm)
// if (debug )
// {
// System out println ( 加密前的二进串 +byte hex (input ))
// System out println ( 加密前的字符串 +new String (input ))
//
// }
Cipher c = Cipher getInstance(Algorithm)
c init(Cipher ENCRYPT_MODE deskey)
byte[] cipherByte = c doFinal(input)
// if (debug ) System out println ( 加密后的二进串 +byte hex (cipherByte ))
return cipherByte;
}
public static byte[] encryptData(byte[] input) throws Exception {
return encryptData(input DEFAULT_KEY)
}
/**
* 将给定的已加密的数据通过指定的密钥进行解密
*
* @param input
* 待解密的数据
* @param key
* 密钥
* @return byte[] 解密后的数据
* @throws Exception
*/
public static byte[] decryptData(byte[] input byte[] key) throws Exception {
SecretKey deskey = new javax crypto spec SecretKeySpec(key Algorithm)
// if (debug ) System out println ( 解密前的信息 +byte hex (input ))
Cipher c = Cipher getInstance(Algorithm)
c init(Cipher DECRYPT_MODE deskey)
byte[] clearByte = c doFinal(input)
// if (debug )
// {
// System out println ( 解密后的二进串 +byte hex (clearByte ))
// System out println ( 解密后的字符串 +(new String (clearByte )))
//
// }
return clearByte;
}
public static byte[] decryptData(byte[] input) throws Exception {
return decryptData(input DEFAULT_KEY)
}
/**
* 字节码转换成 进制字符串
*
* @param byte[] b 输入要转换的字节码
* @return String 返回转换后的 进制字符串
*/
public static String byte hex(byte[] bytes) {
StringBuilder hs = new StringBuilder()
for(byte b : bytes)
hs append(String format( % $ X b))
return hs toString()
}
public static byte[] hex byte(String content) {
int l=content length()》 ;
byte[] result=new byte[l];
for(int i= ;il;i++) {
int j=i《 ;
String s=content substring(j j+ )
result[i]=Integer valueOf(s ) byteValue()
}
return result;
}
/**
* 将字节数组转换为base 编码字符串
* @param buffer
* @return
*/
public static String bytesToBase (byte[] buffer) {
//BASE Encoder en=new BASE Encoder()
return Base encode(buffer)
// return encoder encode(buffer)
}
/**
* 将base 编码的字符串解码为字节数组
* @param value
* @return
* @throws IOException
*/
public static byte[] base ToBytes(String value) throws IOException {
//return Base decodeToByteArray(value)
// System out println(decoder decodeBuffer(value))
// return decoder decodeBuffer(value)
return Base decode(value)
}
/**
* 加密给定的字符串
* @param value
* @return 加密后的base 字符串
*/
public static String encryptString(String value) {
return encryptString(value DEFAULT_KEY)
}
/**
* 根据给定的密钥加密字符串
* @param value 待加密的字符串
* @param key 以BASE 形式存在的密钥
* @return 加密后的base 字符串
* @throws IOException
*/
public static String encryptString(String value String key) throws IOException {
return encryptString(value base ToBytes(key))
}
/**
* 根据给定的密钥加密字符串
* @param value 待加密的字符串
* @param key 字节数组形式的密钥
* @return 加密后的base 字符串
*/
public static String encryptString(String value byte[] key) {
try {
byte[] data=value getBytes(VALUE_ENCODING)
data=CryptUtils encryptData(data key)
return bytesToBase (data)
} catch (Exception e) {
// TODO Auto generated catch block
e printStackTrace()
return null;
}
}
/**
* 解密字符串
* @param value base 形式存在的密文
* @return 明文
*/
public static String decryptString(String value) {
return decryptString(value DEFAULT_KEY)
}
/**
* 解密字符串
* @param value base 形式存在的密文
* @param key base 形式存在的密钥
* @return 明文
* @throws IOException
*/
public static String decryptString(String value String key) throws IOException {
String s=decryptString(value base ToBytes(key))
return s;
}
/**
* 解密字符串
* @param value base 形式存在的密文
* @param key 字节数据形式存在的密钥
* @return 明文
*/
public static String decryptString(String value byte[] key) {
try {
byte[] data=base ToBytes(value)
data=CryptUtils decryptData(data key)
return new String(data VALUE_ENCODING)
}catch(Exception e) {
e printStackTrace()
return null;
}
}
}
CryptHelper java
[java]
package gdie lab crypt;
import javax crypto Cipher;
import javax crypto SecretKey;
import javax crypto SecretKeyFactory;
import javax crypto spec DESKeySpec;
import javax crypto spec IvParameterSpec;
import springframework util DigestUtils;
public class CryptHelper{
private static String CRYPT_KEY = zhongqian ;
//加密
private static Cipher ecip;
//解密
private static Cipher dcip;
static {
try {
String KEY = DigestUtils md DigestAsHex(CRYPT_KEY getBytes()) toUpperCase()
KEY = KEY substring( )
byte[] bytes = KEY getBytes()
DESKeySpec ks = new DESKeySpec(bytes)
SecretKeyFactory skf = SecretKeyFactory getInstance( DES )
SecretKey sk = skf generateSecret(ks)
IvParameterSpec iv = new IvParameterSpec(bytes)
ecip = Cipher getInstance( DES/CBC/PKCS Padding )
ecip init(Cipher ENCRYPT_MODE sk iv )
dcip = Cipher getInstance( DES/CBC/PKCS Padding )
dcip init(Cipher DECRYPT_MODE sk iv )
}catch(Exception ex) {
ex printStackTrace()
}
}
public static String encrypt(String content) throws Exception {
byte[] bytes = ecip doFinal(content getBytes( ascii ))
return CryptUtils byte hex(bytes)
}
public static String decrypt(String content) throws Exception {
byte[] bytes = CryptUtils hex byte(content)
bytes = dcip doFinal(bytes)
return new String(bytes ascii )
}
//test
public static void main(String[] args) throws Exception {
String password = gly ;
String en = encrypt(password)
System out println(en)
System out println(decrypt(en))
}
}
package gdie lab crypt;
import javax crypto Cipher;
import javax crypto SecretKey;
import javax crypto SecretKeyFactory;
import javax crypto spec DESKeySpec;
import javax crypto spec IvParameterSpec;
import springframework util DigestUtils;
public class CryptHelper{
private static String CRYPT_KEY = zhongqian ;
//加密
private static Cipher ecip;
//解密
private static Cipher dcip;
static {
try {
String KEY = DigestUtils md DigestAsHex(CRYPT_KEY getBytes()) toUpperCase()
KEY = KEY substring( )
byte[] bytes = KEY getBytes()
DESKeySpec ks = new DESKeySpec(bytes)
SecretKeyFactory skf = SecretKeyFactory getInstance( DES )
SecretKey sk = skf generateSecret(ks)
IvParameterSpec iv = new IvParameterSpec(bytes)
ecip = Cipher getInstance( DES/CBC/PKCS Padding )
ecip init(Cipher ENCRYPT_MODE sk iv )
dcip = Cipher getInstance( DES/CBC/PKCS Padding )
dcip init(Cipher DECRYPT_MODE sk iv )
}catch(Exception ex) {
ex printStackTrace()
}
}
public static String encrypt(String content) throws Exception {
byte[] bytes = ecip doFinal(content getBytes( ascii ))
return CryptUtils byte hex(bytes)
}
public static String decrypt(String content) throws Exception {
byte[] bytes = CryptUtils hex byte(content)
bytes = dcip doFinal(bytes)
return new String(bytes ascii )
}
//test
public static void main(String[] args) throws Exception {
String password = gly ;
String en = encrypt(password)
System out println(en)
System out println(decrypt(en))
}
lishixinzhi/Article/program/Java/hx/201311/26449
求java大佬指点一、 编写一个Java应用程序:Ex3_1.java,对下面的明文进行加密处理并输出:
你好提问者:
如果解决了你的问题,请采纳,若有疑问,请追问,谢谢!
package com.gc.action.baiduTest;
public class ShowHello {
/**
* 加密:大写+5 数字+3 其他不变
* @param str
*/
public static void mdm(String str){
System.out.println("加密前"+str);
StringBuffer sb =new StringBuffer();
for (int i = 0; i str.length(); i++) {
char ch = str.charAt(i);
if(ch=65 ch=85){//A:65 U:85
ch = (char) (ch+5);
sb.append(ch);
}else if (ch85 ch =90) {
ch = (char) (ch-21);
sb.append(ch);
}else if(ch=48 ch=54){
ch =(char) (ch+3);
sb.append(ch);
}else if(ch54 ch=57){
ch =(char) (ch-7);
sb.append(ch);
}else{
sb.append(ch);
}
}
System.out.println("加密后"+sb);
}
public static void main(String[] args) {
String str ="ABC_ 0123 _XZY_ 789";//OPERATION OVERLORD: THE NORMANDY LANDINGS WILL TAKE PLACE ON JUNE 6th ,1944,AT 6:30
mdm(str);
}
}
结果:
加密前ABC_ 0123 _XZY_ 789
加密后FGH_ 3456 _CED_ 012
----------------------------
加密前OPERATION OVERLORD: THE NORMANDY LANDINGS WILL TAKE PLACE ON JUNE 6th ,1944,AT 6:30
加密后TUJWFYNTS TAJWQTWI: YMJ STWRFSID QFSINSLX BNQQ YFPJ UQFHJ TS OZSJ 9th ,4277,FY 9:63
java的DES加密128位的明文为什么得到的密文却是192位的
加密前后的长度不是固定的,而且你用的密匙不一样的话,长度也不一样。
JAVA从控制台中输入一段明文然后加密为ASC
public class Day25B {
static Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
String str ;
while (true) {
System.out.print("输入:");
str = sc.nextLine();
if (str.contains("over")) {
break;
}
show(str);
}
}
// 明文
public static void show(String str) {
System.out.println("明文:" + str);
String strEn = enCode(str);
System.out.println("加密:" + strEn);
String strDe = deCode(strEn);
System.out.println("解密:" + strDe+"\r\n");
}
// 加密!
public static String enCode(String str) {
String src = "";
for (int i = 0; i str.length(); i++) {
int tem = str.charAt(i);
int num=i;
System.out.print((char)tem+"编码:"+tem+"\t索引:"+i+"\r\n");
if(num==0) {
num=1;
}
tem *= num;
src += tem + ",";
}
return src;
}
// 解密!
public static String deCode(String str) {
String[] strs = str.split(",");
String strTem = "";
for (int i = 0; i strs.length; i++) {
int tem = Integer.valueOf(strs[i]);
int num=i;
if(num==0) {
num=1;
}
tem/=num;
strTem += (char) tem;
}
return strTem;
}
}
//你好,说明一下,题目中说要求把大写进行小写转换,这,我个人觉得有点多此一举!
//可以办到,定义一个数组,记录当前索引,然后打个标记就可以了(当然也有其他方法打标记)
//解密的时候,如果符合标记的就进行运算解密,然后在把char大写转换即可还原!
//当前这个类,忽视这中做法,直接按照给定的规则进行:加密,解密,大小写直接还原.简单!
java对文件编写明文密文问题
看样子你是想把输入的字母 根据ALPHABET 码字表 换成 REPLACEMENT 里的内容。比如输入了“fuck”,那么转换以后差不多是 “rfdw” 对吧。如果这样的话。你的问题错在
for(int i = 0;iline.length();i++) {
for(int j = 0;jALPHABET.length();j++) {
if(line.charAt(i) == ALPHABET.charAt(j) ) {s
ALPHABET.charAt(i) = REPLACEMENT.charAt(j);//这里错了。目的是要替换line的第i个字符 } else {
}
}
可以改成:
String A="";
for(int i = 0;iline.length();i++) {
int x = ALPHABET.indexOf(line.charAt(i));
if(x 0)
A+= REPLACEMENT.charAt(i);
else
A+= line.charAt(i);
}
return A;
java中明文加密的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于文件加密 java、java中明文加密的信息别忘了在本站进行查找喔。