/basiclti/basiclti-common/src/java/org/sakaiproject/basiclti/util/BlowFish.java

https://github.com/sakaiproject/sakai · Java · 130 lines · 61 code · 17 blank · 52 comment · 7 complexity · c29ec244e22b26c7eb60362cc8db3cce MD5 · raw file

  1. /**
  2. * Copyright (c) 2006-2014 The Sakai Foundation
  3. *
  4. * Licensed under the Educational Community License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.opensource.org/licenses/ECL-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. *
  16. * This code was adapted from Chuck Hedreick's Linktool utlity code
  17. */
  18. package org.sakaiproject.basiclti.util;
  19. import javax.crypto.Cipher;
  20. import javax.crypto.SecretKey;
  21. import javax.crypto.spec.SecretKeySpec;
  22. import org.sakaiproject.basiclti.util.PortableShaUtil;
  23. /**
  24. * Support Blowfish Encryption and Decryption
  25. */
  26. public class BlowFish {
  27. // Default JCE only supports 128 bit encryption
  28. public static final int MAX_KEY_LENGTH = 128 / 8;
  29. private static char[] hexChars = {
  30. '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
  31. };
  32. /**
  33. * Strengthen a key which might be too short by extending with a salt text
  34. *
  35. * @param secret
  36. * String
  37. * @param salt
  38. * String
  39. * @throws Exception.
  40. */
  41. public static String strengthenKey(String secret, String salt )
  42. {
  43. if ( secret.length() > BlowFish.MAX_KEY_LENGTH ) return secret;
  44. // Extend the secret to the maximum Blowfish length
  45. secret = secret + salt;
  46. return secret.substring(0, BlowFish.MAX_KEY_LENGTH);
  47. }
  48. /**
  49. * Decrypt a string using Blowfish
  50. *
  51. * @param secret
  52. * A hex-encoded secret - secrets longer than the maximum key length will be truncated
  53. * @param str
  54. * The plain text to be encoded
  55. */
  56. public static String encrypt(String secret, String str) {
  57. if ( secret == null ) return null;
  58. if ( secret.length() > MAX_KEY_LENGTH*2 ) {
  59. secret = secret.substring(0,MAX_KEY_LENGTH*2);
  60. }
  61. try {
  62. byte[] secretBytes = PortableShaUtil.hex2bin(secret);
  63. SecretKey secretKey = new SecretKeySpec(secretBytes, "Blowfish");
  64. Cipher ecipher = Cipher.getInstance("Blowfish");
  65. ecipher.init(Cipher.ENCRYPT_MODE, secretKey);
  66. // Encode the string into bytes using utf-8
  67. byte[] utf8 = str.getBytes("UTF8");
  68. // Encrypt
  69. byte[] enc = ecipher.doFinal(utf8);
  70. // Encode bytes to base64 to get a string
  71. return PortableShaUtil.bin2hex(enc);
  72. } catch (javax.crypto.BadPaddingException e) {
  73. throw new Error(e);
  74. } catch (javax.crypto.IllegalBlockSizeException e) {
  75. throw new Error(e);
  76. } catch (java.security.NoSuchAlgorithmException e) {
  77. throw new Error(e);
  78. } catch (java.security.InvalidKeyException e) {
  79. throw new Error(e);
  80. } catch (javax.crypto.NoSuchPaddingException e) {
  81. throw new Error(e);
  82. } catch (java.io.UnsupportedEncodingException e) {
  83. throw new Error(e);
  84. }
  85. }
  86. /**
  87. * Decrypt a string using Blowfish
  88. *
  89. * @param secret
  90. * A hex-encoded secret - secrets longer than the maximum key length will be truncated
  91. * @param enc
  92. * A hex-encoded ciphertext
  93. */
  94. public static String decrypt (String secret, String enc) {
  95. if ( secret == null ) return null;
  96. if ( secret.length() > MAX_KEY_LENGTH*2 ) {
  97. secret = secret.substring(0,MAX_KEY_LENGTH*2);
  98. }
  99. try {
  100. byte [] secretBytes = PortableShaUtil.hex2bin(secret);
  101. SecretKey secretKey = new SecretKeySpec(secretBytes, "Blowfish");
  102. Cipher dcipher = Cipher.getInstance("Blowfish");
  103. dcipher.init(Cipher.DECRYPT_MODE, secretKey);
  104. byte[] dec = PortableShaUtil.hex2bin(enc);
  105. // Decrypt
  106. byte[] utf8 = dcipher.doFinal(dec);
  107. // Decode using utf-8
  108. return new String(utf8, "UTF8");
  109. } catch (Exception e) {
  110. throw new Error(e);
  111. }
  112. }
  113. }