算法需每一步都按顺序进行,并且结果唯一,不能保证可逆,故A不正确; 一个算法必须在有限步内完成,基梁穗不然就不是问题的解了,故B不正确; 一般情况下,完成一件事情的算法不止一个,但是存在一个比较好的,故C不正确; 设渣轮计算法要搏卜尽量运算简单,节约时间,故D正确, 故选D. |
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 = 0i<hexStr.length()/2i++) {
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 = 0i <buf.lengthi++) {
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))
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)