-1

目前我正在研究 SHA-1 哈希算法,有人可以建议我如何使用密钥开发这种算法吗?

谢谢 :-)

类似的问题: 如何在 Android 中计算带有密钥的字符串的 SHA-256 哈希?.

谢谢。

4

1 回答 1

1

你说的是盐还是什么的?我认为您想使用可以反转的加密,例如 AES。

  public static byte[] encrypt(String plainText, String encryptionKey) throws Exception   
  {
    Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding", "SunJCE");
    SecretKeySpec key = new SecretKeySpec(encryptionKey.getBytes("UTF-8"), "AES");
    cipher.init(Cipher.ENCRYPT_MODE, key,new IvParameterSpec(new byte[cipher.getBlockSize()]));
    return cipher.doFinal(plainText.getBytes("UTF-8"));
  }

  public static String decrypt(byte[] cipherText, String encryptionKey) throws Exception
  {
    Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding", "SunJCE");
    SecretKeySpec key = new SecretKeySpec(encryptionKey.getBytes("UTF-8"), "AES");
    cipher.init(Cipher.DECRYPT_MODE, key,new IvParameterSpec(new byte[cipher.getBlockSize()]));
    return new String(cipher.doFinal(cipherText),"UTF-8");
  }

一个简单的使用方法是

String plaintext = "My plain text.";
String encryptionKey = "Super secret encryption key";
Log.d("AES", "Plain text is : " + plaintext );

byte[] cipher = encrypt(plaintext, encryptionKey);
Log.d("AES", "Encrypted string is : " + new String(cipher));

String decrypted = decrypt(cipher, encryptionKey);
Log.d("AES", "Decrypted string is : " + decrypted);

if (plaintext.equals(decrypted)) Log.d("AES", "Strings match !");
于 2014-02-24T09:08:57.660 回答