byte加密
⑴ 加密后的byte数组转换String后,再转回 byte数组,会出现数据流失的情况,如何解决~
从简单地说起,bytes->str->bytes,必须要保证编码的统一,比如统一用UTF8
如果这个不是问题
我不知道你说的字符串是不是类似于
byte[] bs = {1,2,3,4};
String s = new String(bs);
然后传输或其它的的操作后再s.getBytes();
个人建议用这样的办法,用两个字符代表一个byte的16进制
比如:byte[] bs = {1,2,3,4,17,32};
转换后的字符串是:010203041120
⑵ 急,请教关于加密算法,密文要求是8个字节
既然有算法,自己试一下不就知道了?16字节就是128位,AES是按128位分组加密,你说应该输出是多少呢?呵呵
⑶ java如何加密int类型数据
String加密 实际上也是对String的 byte[] 加密。
通常一种加密算法,都针对的是字节数组,而非String 或者int。
因为所有上述这些类型都可以用 byte[]表示,只要开发一次就可以针对所有类型加密了
把int转化成 byte[]加密就可以了
byte[4] intbytes = new byte[4]; 然后用位移运算,得到int的每一个byte
int value = 1000 ;
intbytes[0] = (byte)(value & 0x000000FF)
intbytes[1] = (byte)((value & 0x0000FF00) >> 8)
intbytes[2] = (byte)((value & 0x00FF0000) >> 16)
......
String换算成byte就更容易了 String.getBytes("utf-8") ; 参数是字符集名字 可以不用指定,但是你就不确定它到底用的哪种字符集。
⑷ JAVA简单文件加密 求JAVA源代码
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);
}
}
⑸ 国际数据加密算法的算法简介
IDEA加密算法简介
IDEA(International Data Encryption Algorithm)在密码学中属于数据块加密算法(Block Cipher)类。IDEA使用长度为128bit的密钥,数据块大小为64bit。从理论上讲,IDEA属于“强”加密算法,至今还没有出现对该算法的有效攻击算法。
早在1990年,Xuejia Lai等人在EuroCrypt’90年会上提出了分组密码建议PES(Proposed Encryption Standard)。在EuroCrypt’91年会上,Xuejia Lai等人又提出了PES的修正版IPES(Improved PES)。目前IPES已经商品化,并改名为IDEA。IDEA已由瑞士的Ascom公司注册专利,以商业目的使用IDEA算法必须向该公司申请许可。
IDEA是一种由8个相似圈(Round)和一个输出变换(Output Transformation)组成的迭代算法。IDEA的每个圈都由三种函数:模(216+1)乘法、模216加法和按位XOR组成。
在加密之前,IDEA通过密钥扩展(Key Expansion)将128bit的密钥扩展为52Byte的加密密钥EK(Encryption Key),然后由EK计算出解密密钥DK(Decryption Key)。EK和DK分为8组半密钥,每组长度为6Byte,前8组密钥用于8圈加密,最后半组密钥(4Byte)用于输出变换。IDEA的加密过程和解密过程是一样的,只不过使用不同的密钥(加密时用EK,解密时用DK)。
密钥扩展的过程如下:
1. 将128bit的密钥作为EK的前8byte;
2. 将前8byte循环左移25bit,得到下一8byte,将这个过程循环7次;
3. 在第7次循环时,取前4byte作为EK的最后4byte;
4. 至此52byte的EK生成完毕。
密钥扩展的过程如表1所示,为了能够清楚的看出每个8Byte的关系,在表1中用粗线条将将每个8Byte划分开了.
IDEA算法相对来说是一个比较新的算法,其安全性研究也在不断进行之中。在IDEA算法公布后不久,就有学者指出:IDEA的密钥扩展算法存在缺陷,导致在IDEA算法中存在大量弱密钥类,但这个弱点通过简单的修改密钥扩展算法(加入异或算子)即可克服。在1997年的EuroCrypt’97年会上,John Borst等人提出了对圈数减少的IDEA的两种攻击算法:对3.5圈IDEA的截短差分攻击(Truncate Diffrential Attack)和对3圈IDEA的差分线性攻击(Diffrential Linear Attack)。但作者也同时指出,这两种攻击算法对整8.5圈的IDEA算法不可能取得实质性的攻击效果。目前尚未出现新的攻击算法,一般认为攻击整8.5圈IDEA算法唯一有效的方法是穷尽搜索128bit的密钥空间。
⑹ 一个字符串用RSA公钥加密为byte[],在用base64 加密成String,在用base64 解密为String
俺有类似的函数,核心加密是用des
原型:
int WINAPI icePub_encryptText3(char *strInput, char *strOutputHexstring, char *strKey)
输入:strInput 待加密文本数据串
strKey 密钥,任意长度
输出:strOutputHexstring 加密后base64串
返回码:
原型:
int WINAPI icePub_decryptText3(char *strInputHexstring, char *strOutput, char *strKey)
输入:strInputHexstring 待解密数据串
strKey 密钥,任意长度
输出:strOutput 解密后数据
返回码: 解密后数据最大长度,为8的倍数
Private Declare Function icePub_encryptText3 Lib "icePubDll.dll" (ByVal strInput As String,ByVal strOutputHexstring As String, ByVal strKey As String) As Integer
Dim len2 As Integer
Dim buff As String
Dim buff2 As String
Dim key As String
buff="Recall Dream Miss, Keep Silk-silk accept as a souvenir, Between You And Me, Stringed music touching."
key="2286766486"
buff2=Space(1024)
len2=icePub_encryptionText3(buff,buff2,key)
MsgBox buff2
Private Declare Function icePub_decryptText3 Lib "icePubDll.dll" (ByVal strInputHexstring As String, ByVal strOutput As String, ByVal strKey As String) As Integer
Dim len2 As Integer
Dim buff As String
Dim buff2 As String
Dim key As String
buff="vVVq0eaCUs8="
key="11223344"
buff2=Space(1024)
len2= icePub_decryptText3(buff,buff2,key)
MsgBox buff2
⑺ c#中,如何将含有字母和数字的字符串加密成一个全是字母的字符串,并解密
publicclassStringEncrypt
{
///<summary>
///使用缺省密钥字符串加密
///</summary>
///<paramname="original">明文</param>
///<returns>密文</returns>
publicstaticstringEncrypt(stringoriginal)
{
returnEncrypt(original,"xyzABc*$!");
}
///<summary>
///使用缺省密钥解密
///</summary>
///<paramname="original">密文</param>
///<returns>明文</returns>
publicstaticstringDecrypt(stringoriginal)
{
returnDecrypt(original,"xyzABc*$!",System.Text.Encoding.Default);
}
///<summary>
///使用给定密钥解密
///</summary>
///<paramname="original">密文</param>
///<paramname="key">密钥</param>
///<returns>明文</returns>
publicstaticstringDecrypt(stringoriginal,stringkey)
{
returnDecrypt(original,key,System.Text.Encoding.Default);
}
///<summary>
///使用缺省密钥解密,返回指定编码方式明文
///</summary>
///<paramname="original">密文</param>
///<paramname="encoding">编码方式</param>
///<returns>明文</returns>
publicstaticstringDecrypt(stringoriginal,Encodingencoding)
{
returnDecrypt(original,"xyzABc*$!",encoding);
}
///<summary>
///使用给定密钥加密
///</summary>
///<paramname="original">原始文字</param>
///<paramname="key">密钥</param>
///<paramname="encoding">字符编码方案</param>
///<returns>密文</returns>
publicstaticstringEncrypt(stringoriginal,stringkey)
{
byte[]buff=System.Text.Encoding.Default.GetBytes(original);
byte[]kb=System.Text.Encoding.Default.GetBytes(key);
returnConvert.ToBase64String(Encrypt(buff,kb));
}
///<summary>
///使用给定密钥解密
///</summary>
///<paramname="encrypted">密文</param>
///<paramname="key">密钥</param>
///<paramname="encoding">字符编码方案</param>
///<returns>明文</returns>
publicstaticstringDecrypt(stringencrypted,stringkey,Encodingencoding)
{
byte[]buff=Convert.FromBase64String(encrypted);
byte[]kb=System.Text.Encoding.Default.GetBytes(key);
returnencoding.GetString(Decrypt(buff,kb));
}
///<summary>
///生成MD5摘要
///</summary>
///<paramname="original">数据源</param>
///<returns>摘要</returns>
publicstaticbyte[]MakeMD5(byte[]original)
{
=newMD5CryptoServiceProvider();
byte[]keyhash=hashmd5.ComputeHash(original);
hashmd5=null;
returnkeyhash;
}
///<summary>
///使用给定密钥加密
///</summary>
///<paramname="original">明文</param>
///<paramname="key">密钥</param>
///<returns>密文</returns>
publicstaticbyte[]Encrypt(byte[]original,byte[]key)
{
=();
des.Key=MakeMD5(key);
des.Mode=CipherMode.ECB;
returndes.CreateEncryptor().TransformFinalBlock(original,0,original.Length);
}
///<summary>
///使用给定密钥解密数据
///</summary>
///<paramname="encrypted">密文</param>
///<paramname="key">密钥</param>
///<returns>明文</returns>
publicstaticbyte[]Decrypt(byte[]encrypted,byte[]key)
{
=();
des.Key=MakeMD5(key);
des.Mode=CipherMode.ECB;
returndes.CreateDecryptor().TransformFinalBlock(encrypted,0,encrypted.Length);
}
///<summary>
///使用给定密钥加密
///</summary>
///<paramname="original">原始数据</param>
///<paramname="key">密钥</param>
///<returns>密文</returns>
publicstaticbyte[]Encrypt(byte[]original)
{
byte[]key=System.Text.Encoding.Default.GetBytes("xyzABc*$!");
returnEncrypt(original,key);
}
///<summary>
///使用缺省密钥解密数据
///</summary>
///<paramname="encrypted">密文</param>
///<paramname="key">密钥</param>
///<returns>明文</returns>
publicstaticbyte[]Decrypt(byte[]encrypted)
{
byte[]key=System.Text.Encoding.Default.GetBytes("xyzABc*$!");
returnDecrypt(encrypted,key);
}
}
⑻ C# aes加密后返回的byte[]先Encoding.UTF8.GetString再Encoding.UTF8.GetBytes后和原来的值不一样
你自己都说了,加密之后的得出来不一样,你都加密了,那个byte[]中的数据早就变化得不像样了。
加密之后的数组不能直接转成字符串的,要转成16进制的字符
⑼ JAVA Byte数组转int数组 加密用的
label27:int j这一行不符合Java的语法规范,肯定是编译通不过。
⑽ 什么是对称加密算法请举例
对称加密算法简介:
对称加密算法 对称加密算法是应用较早的加密算法,技术成熟。在对称加密算法中,数据发信方将明文(原始数据)和加密密钥一起经过特殊加密算法处理后,使其变成复杂的加密密文发送出去。收信方收到密文后,若想解读原文,则需要使用加密用过的密钥及相同算法的逆算法对密文进行解密,才能使其恢复成可读明文。在对称加密算法中,使用的密钥只有一个,发收信双方都使用这个密钥对数据进行加密和解密,这就要求解密方事先必须知道加密密钥。
特点:
对称加密算法的特点是算法公开、计算量小、加密速度快、加密效率高。
不足之处是,交易双方都使用同样钥匙,安全性得不到保证。此外,每对用户每次使用对称加密算法时,都需要使用其他人不知道的惟一钥匙,这会使得发收信双方所拥有的钥匙数量成几何级数增长,密钥管理成为用户的负担。对称加密算法在分布式网络系统上使用较为困难,主要是因为密钥管理困难,使用成本较高。而与公开密钥加密算法比起来,对称加密算法能够提供加密和认证却缺乏了签名功能,使得使用范围有所缩小。在计算机专网系统中广泛使用的对称加密算法有DES和IDEA等。美国国家标准局倡导的AES即将作为新标准取代DES。
具体算法:
3DES算法,Blowfish算法,RC5算法。 对称加密算法-原理及应用对称加密算法的优点在于加解密的高速度和使用长密钥时的难破解性。假设两个用户需要使用对称加密方法加密然后交换数据,则用户最少需要2个密钥并交换使用,如果企业内用户有n个,则整个企业共需要n×(n-1) 个密钥,密钥的生成和分发将成为企业信息部门的恶梦。对称加密算法的安全性取决于加密密钥的保存情况,但要求企业中每一个持有密钥的人都保守秘密是不可能的,他们通常会有意无意的把密钥泄漏出去——如果一个用户使用的密钥被入侵者所获得,入侵者便可以读取该用户密钥加密的所有文档,如果整个企业共用一个加密密钥,那整个企业文档的保密性便无从谈起。DESCryptoServiceProvider
RC2CryptoServiceProvider
RijndaelManaged
//例加密文本文件(RijndaelManaged )
byte[] key = { 24, 55, 102,24, 98, 26, 67, 29, 84, 19, 37, 118, 104, 85, 121, 27, 93, 86, 24, 55, 102, 24,98, 26, 67, 29, 9, 2, 49, 69, 73, 92 };
byte[] IV ={ 22, 56, 82, 77, 84, 31, 74, 24,55, 102, 24, 98, 26, 67, 29, 99 };
RijndaelManaged myRijndael = new RijndaelManaged();
FileStream fsOut = File.Open(strOutName, FileMode.Create,FileAccess.Write);//strOutName文件名及路径 FileStream fsIn = File.Open(strPath, FileMode.Open,FileAccess.Read);
CryptoStream csDecrypt=new CryptoStream(fsOut,myRijndael.CreateEncryptor(key, IV),CryptoStreamMode.Write);//读加密文本
BinaryReader br = new BinaryReader(fsIn);
csDecrypt.Write(br.ReadBytes((int)fsIn.Length),0, (int)fsIn.Length);
csDecrypt.FlushFinalBlock();
csDecrypt.Close();
fsIn.Close();
fsOut.Close();
//解密文件
byte[] key = { 24, 55, 102, 24, 98, 26, 67, 29, 84, 19, 37, 118,104, 85, 121, 27, 93, 86, 24, 55, 102, 24, 98, 26, 67, 29, 9, 2, 49, 69, 73, 92};
byte[] IV ={ 22, 56, 82, 77, 84, 31, 74, 24, 55, 102, 24, 98, 26,67, 29, 99 };
RijndaelManaged myRijndael = new RijndaelManaged();
FileStream fsOut = File.Open(strPath, FileMode.Open, FileAccess.Read);
CryptoStream csDecrypt = new CryptoStream(fsOut, myRijndael.CreateDecryptor(key,IV), CryptoStreamMode.Read);
StreamReader sr = new StreamReader(csDecrypt);//把文件读出来
StreamWriter sw = new StreamWriter(strInName);//解密后文件写入一个新的文件
sw.Write(sr.ReadToEnd());
sw.Flush();
sw.Close();
sr.Close();f
sOut.Close();
用图片加密(RC2CryptoServiceProvider )
FileStreamfsPic = new FileStream(pictureBox1.ImageLocation,FileMode.Open, FileAccess.Read);
//加密文件流(textBox1.Text是文件名及路径)
FileStream fsText = new FileStream(textBox1.Text, FileMode.Open,FileAccess.Read);
byte[] bykey = new byte[16]; //初始化
Key IVbyte[] byIv = new byte[8];
fsPic.Read(bykey, 0, 16);
fsPic.Read(byIv, 0, 8);
RC2CryptoServiceProvider desc = newRC2CryptoServiceProvider();//desc进行加密
BinaryReader br = new BinaryReader(fsText);//从要加密的文件中读出文件内容
FileStream fsOut = File.Open(strLinPath,FileMode.Create, FileAccess.Write); // strLinPath临时加密文件路径CryptoStream cs = new CryptoStream(fsOut, desc.CreateEncryptor(bykey,byIv), CryptoStreamMode.Write);//写入临时加密文件
cs.Write(br.ReadBytes((int)fsText.Length),0, (int)fsText.Length);//写入加密流
cs.FlushFinalBlock();
cs.Flush();
cs.Close();
fsPic.Close();
fsText.Close();
fsOut.Close();
用图片解密
FileStream fsPic = new FileStream(pictureBox1.ImageLocation, FileMode.Open, FileAccess.Read); //图片流FileStream fsOut = File.Open(textBox1.Text,FileMode.Open, FileAccess.Read);//解密文件流
byte[] bykey = new byte[16]; //初始化
Key IVbyte[] byIv = new byte[8];
fsPic.Read(bykey, 0, 16);
fsPic.Read(byIv, 0, 8);
string strPath = textBox1.Text;//加密文件的路径
int intLent = strPath.LastIndexOf("\\")+ 1;
int intLong = strPath.Length;
string strName = strPath.Substring(intLent, intLong - intLent);//要加密的文件名称
string strLinPath = "C:\\"+ strName;//临时解密文件路径
FileStream fs = new FileStream(strLinPath, FileMode.Create,FileAccess.Write);
RC2CryptoServiceProvider desc = newRC2CryptoServiceProvider();//desc进行解密
CryptoStream csDecrypt = new CryptoStream(fsOut, desc.CreateDecryptor(bykey,byIv), CryptoStreamMode.Read);
//读出加密文件
BinaryReader sr = new BinaryReader(csDecrypt);//从要加密流中读出文件内容
BinaryWriter sw = new BinaryWriter(fs);//写入解密流
sw.Write(sr.ReadBytes(Convert.ToInt32(fsOut.Length)));
//sw.Flush();
sw.Close();
sr.Close();
fs.Close();
fsOut.Close();
fsPic.Close();
csDecrypt.Flush();
File.Delete(textBox1.Text.TrimEnd());//删除原文件
File.Copy(strLinPath, textBox1.Text);//复制加密文件
File.Delete(strLinPath);//删除临时文件