共享密钥和加密某些东西是完全不同的两件事。如何分享金钥
话虽如此,AES
使用128位元比使用128位元的加密算法要强大得多。3DES
因此,您可以做的是保持PKI基础结构交换AES keys
,然后使用它们进行加密和解密。
为什么不RSA
呢?RSA
需要最少512位才能将其视为最强,如果增加更多位,则将增加加密和解密所需的时间。
SO AES快速安全。
使用SecretKeySpec从字节创建密钥[]
public static void main(String[] args) throws Exception
{
// Initialise secret key with predefined byte array [] like below. I
// have used simple string to array method to generate 16 byte array.
// AES Key must be minimum 16 bytes.
// Now you can put this byte array some where is .so file.
// Generate new Key using this byte []
// Then you can generate a key using device specific information at
// first boot up.
// Use second key to encrypt data and first key to encrypt the second
// key
// I Hope it clears all the doubts
SecretKey key = new SecretKeySpec("ABCDEFGHIJKLMNOP".getBytes(), "AES");
System.out.println(Arrays.toString(key.getEncoded()));
// Initialise Cipher with AES Algorithm
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
// Set The Encrypt Mode
cipher.init(Cipher.ENCRYPT_MODE, key);
// Encrypt some bytes
byte[] encrypted = cipher.doFinal("ABCDEFGH".getBytes());
// Print it to vefiry
System.out.println(Arrays.toString(encrypted));
// Get The IV
byte[] iv = cipher.getIV();
System.out.println(iv.length);
// Now why storing you can create structure like [16 IV][Encrypted Data]
// And while decrypting you can read first [16] bytes IV and then
// decrypt remaining bytes
//byte[] iv = new byte[16];
// System.arraycopy(encrypted, 0, iv, 0, 16)
//Copy remaining bytes to decrypt
// set cipher to decrypt mode
cipher.init(Cipher.DECRYPT_MODE, key,new IvParameterSpec(iv));
// decrypt it
byte[] decrypted = cipher.doFinal(encrypted);
System.out.println(new String(decrypted));
}
现在编写一种算法,该算法将从某些随机数据(例如设备名称,用户名,随机种子等)生成byte []。
您可以通过在算法源代码中编写该算法C并创建.so文件并byte []使用来为其添加更多保护Native calls。
完成所有这些操作有什么好处?