From b87005ddaf9a57eee9e7b300655ba0465818e3d6 Mon Sep 17 00:00:00 2001 From: "nicolson%netscape.com" Date: Thu, 17 Jan 2002 22:54:05 +0000 Subject: [PATCH] add secret decoder ring for JSS 3.1.2, special release for CMS. git-svn-id: svn://10.0.0.236/trunk@112405 18797224-902f-48f8-a5cc-f745e15eee43 --- .../mozilla/jss/crypto/SecretDecoderRing.java | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/mozilla/security/jss/org/mozilla/jss/crypto/SecretDecoderRing.java b/mozilla/security/jss/org/mozilla/jss/crypto/SecretDecoderRing.java index ad67632fa0a..708186320ca 100644 --- a/mozilla/security/jss/org/mozilla/jss/crypto/SecretDecoderRing.java +++ b/mozilla/security/jss/org/mozilla/jss/crypto/SecretDecoderRing.java @@ -36,6 +36,8 @@ package org.mozilla.jss.crypto; +import java.io.UnsupportedEncodingException; + /** * This is a proprietary NSS interface. It is used for encrypting * data with a secret key stored in the NSS key database (which is in turn @@ -58,6 +60,8 @@ package org.mozilla.jss.crypto; */ public class SecretDecoderRing { + public static final String encodingFormat = "UTF-8"; + /** * Encrypts the given plaintext with the Secret Decoder Ring key stored * in the NSS key database. @@ -65,10 +69,39 @@ public class SecretDecoderRing { public native byte[] encrypt(byte[] plaintext) throws TokenException; + /** + * Encrypts the given plaintext string with the Secret Decoder Ring key + * stored in the NSS key database. + */ + public byte[] encrypt(String plaintext) throws TokenException { + try { + return encrypt(plaintext.getBytes(encodingFormat)); + } catch(UnsupportedEncodingException e) { + // this shouldn't happen, because we use a universally-supported + // charset + throw new RuntimeException(e.getMessage()); + } + } + /** * Decrypts the given ciphertext with the Secret Decoder Ring key stored * in the NSS key database. */ public native byte[] decrypt(byte[] ciphertext) throws TokenException; + + /** + * Decrypts the given ciphertext with the Secret Decoder Ring key stored + * in the NSS key database, returning the original plaintext string. + */ + public String decryptToString(byte[] ciphertext) + throws TokenException { + try { + return new String(decrypt(ciphertext), encodingFormat); + } catch(UnsupportedEncodingException e) { + // this shouldn't happen, because we use a universally-supported + // charset + throw new RuntimeException(e.getMessage()); + } + } }