Android加密AES CR4使用
1.如果不选择填充会默认采用ECB模式和PKCS5Padding填充进行处理
AES是块加密,块的长度是16个字节,如果原文不到16个字节,则需要填充至16个字节后再进行处理。
AES密文长度=(原文长度/16)*16+16;最终长度为N+1;所以一定要选择NoPadding模式。
2. 随机数生成器
在Android加密算法中需要随机数时要使用SecureRandom来获取随机数。
注意不要给SecureRandom设置种子。调用seeded constructor或者setSeed(byte[])是不安全的。 SecureRandom()默认使用的是dev/urandom作为种子产生器,这个种子是不可预测的。
开发者建议:
1、不要使用Random类来获取随机数。
2、在使用SecureRandom时候,不要设置种子
SecureRandomsecureRandom=newSecureRandom();byte[]key=newbyte[16];//随机秘钥secureRandom.nextBytes(key)/***生成随机key**@returnkey*@throwsExceptiongenerateerror*/publicstaticbyte[]getRawKey()throwsException{KeyGeneratorkgen=KeyGenerator.getInstance("AES");SecureRandomsr=null;if(android.os.Build.VERSION.SDK_INT>android.os.Build.VERSION_CODES.JELLY_BEAN){sr=SecureRandom.getInstance("SHA1PRNG","Crypto");}else{sr=SecureRandom.getInstance("SHA1PRNG");}kgen.init(128,sr);//192and256bitsmaynotbeavailableSecretKeyskey=kgen.generateKey();byte[]raw=skey.getEncoded();returnraw;}/***AES加密原文16字节对齐,不足补零**@paramraw加密秘钥*@paramsource被加密数据*@return密文*@throwsException*/publicstaticbyte[]encrypt(byte[]raw,byte[]source)throwsException{byte[]original;if(raw.length!=16)thrownewIllegalArgumentException("keyisnot16bytes");if(source.length%16!=0){original=newbyte[(source.length/16+source.length%16==0?0:1)*16];System.arraycopy(source,0,original,0,source.length);}else{original=Arrays.copyOf(source,source.length);}SecretKeySpecskeySpec=newSecretKeySpec(raw,"AES");Ciphercipher=Cipher.getInstance("AES/ECB/NoPadding");cipher.init(Cipher.ENCRYPT_MODE,skeySpec);byte[]encrypted=cipher.doFinal(original);returnencrypted;}/***AES机密密文,AES/ECB/NoPadding参数要和加密时一样,NoPadding原文密文长度一致。**@paramraw秘钥*@paramencrypted密文*@return原文*@throwsException*/publicstaticbyte[]decrypt(byte[]raw,byte[]encrypted)throwsException{if(encrypted.length%16!=0)returnencrypted;SecretKeySpecskeySpec=newSecretKeySpec(raw,"AES");Ciphercipher=Cipher.getInstance("AES/ECB/NoPadding");cipher.init(Cipher.DECRYPT_MODE,skeySpec);byte[]decrypted=cipher.doFinal(encrypted);returndecrypted;}/***RC4加密原文16字节对齐,不足补零**@paramraw秘钥*@paramsource原文*@return密文*@throwsException*/publicstaticbyte[]rc4(byte[]raw,byte[]source)throwsException{byte[]original;if(raw.length!=16)thrownewIllegalArgumentException("keyisnot16bytes");if(source.length%16!=0){original=newbyte[(source.length/16+source.length%16==0?0:1)*16];System.arraycopy(source,0,original,0,source.length);}else{original=Arrays.copyOf(source,source.length);}SecretKeySpecskeySpec=newSecretKeySpec(raw,"RC4");Ciphercipher=Cipher.getInstance("RC4");cipher.init(Cipher.ENCRYPT_MODE,skeySpec);byte[]encrypted=cipher.doFinal(original);returnencrypted;}/***从大到小排序**@paramsource*/publicstaticvoidsortByDescending(intsource[]){intswap=0;for(intposition=0;position<source.length;position++){for(intlocation=0;location<source.length;location++){if(source[position]>source[location]){swap=source[position];source[position]=source[location];source[location]=swap;}}}}/***生成随机的length长的String。**@paramlength生成的字符长度。*@return生成的随机字符。*/publicstaticStringgetRandomString(intlength){Stringstr="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";Randomrandom=newRandom();StringBuffersb=newStringBuffer();for(inti=0;i<length;i++){intnumber=random.nextInt(62);sb.append(str.charAt(number));}returnsb.toString();}
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。