喵喔喔
我建议使用一些广泛可用的标准对称密码,如DES, 3 DES或俄歇..虽然这不是最安全的算法,但是有大量的实现,您只需要将密钥给应该解密条形码中信息的任何人。密码你想在这里工作。让我们假设要加密的字节在byte[] input;接下来,你需要钥匙初始化向量字节byte[] keyBytes;byte[] ivBytes;现在,您可以为您选择的算法初始化密码:// wrap key data in Key/IV specs to pass to cipherSecretKeySpec key = new SecretKeySpec(keyBytes, "DES");
IvParameterSpec ivSpec = new IvParameterSpec(ivBytes);// create the cipher with the algorithm you choose
// see javadoc for Cipher class for more info, e.g.Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");加密应该是这样的:cipher.init(Cipher.ENCRYPT_MODE, key, ivSpec);byte[] encrypted= new byte[cipher.getOutputSize(input.length)];
int enc_len = cipher.update(input, 0, input.length, encrypted, 0);enc_len += cipher.doFinal(encrypted, enc_len);解密如下:cipher.init(Cipher.DECRYPT_MODE, key, ivSpec);byte[] decrypted = new byte[cipher.getOutputSize(enc_len)];
int dec_len = cipher.update(encrypted, 0, enc_len, decrypted, 0);dec_len += cipher.doFinal(decrypted, dec_len);