Skip to Main Content

Java Card

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

encryption

848588Mar 29 2011 — edited Nov 21 2011
hello,

I am working with a applet which give data and I would like encrypt this data with DES. Problem is that I use JCWDE which accept only "ALG_DES_CBC_ISO9797_M2" for DES encryption and I don't found the same thing with java.crypto.
I have tested that:
cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
but I need AlgorithmParameter and I can't use that with JavaCard.

There is another solution?


card code:
	public void initialisation(){
        key = (DESKey)KeyBuilder.buildKey(KeyBuilder.TYPE_DES,KeyBuilder.LENGTH_DES, false);
        key.setKey(Crypto, (short)0);
        ecipher = Cipher.getInstance(Cipher.ALG_DES_CBC_ISO9797_M2,false);
		
	}
	
	// decrypt method
	public byte[] decrypter(byte[] aDecrypter){
		ecipher.init(key, Cipher.MODE_DECRYPT);
		donneeDecrypte = new byte[aDecrypter.length]; 
		ecipher.doFinal(aDecrypter, (short)0, (short)aDecrypter.length, donneeDecrypte, (short)0);	
		return donneeDecrypte;
	}
	
	//encrypt method
	public byte[] cryter(byte[] Crypter){
			
		ecipher.init(key, Cipher.MODE_ENCRYPT);
		
		short taille = (short)Crypter.length;
		short temp = (short) ( taille & 0x0007 );
		taille = (short) (taille + (8 - temp));
	
		donneeCrypter = new byte[taille];
		short toto = ecipher.doFinal(Crypter, (short)0, (short)Crypter.length, donneeCrypter, (short)0);
		ecipher.
		return donneeCrypter;
	}
Client code:

	public void initialiserCrypto(){


		
		
		KeyGenerator keyGen;
		try {
			keyGen = KeyGenerator.getInstance("DES");
			keyGen.init(56);
			key = keyGen.generateKey();
			
			crypto = key.getEncoded();
		} catch (NoSuchAlgorithmException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

        
        
        try {
			cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");		
			//cipher = Cipher.getInstance("DES");
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		} catch (NoSuchPaddingException e) {
			e.printStackTrace();
		} 
	}
	
	// decrypt method
	public byte[] decrypter(byte[] aDecrypter) {

		AlgorithmParameters params;
		try {
			params = AlgorithmParameters.getInstance("DES");
			params.init(encodedParams);
			cipher.init(Cipher.DECRYPT_MODE, key, params);
			return cipher.doFinal(aDecrypter);
		} catch (NoSuchAlgorithmException e1) {
			e1.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (InvalidKeyException e) {
			e.printStackTrace();
		} catch (InvalidAlgorithmParameterException e) {
			e.printStackTrace();
		} catch (IllegalBlockSizeException e) {
			e.printStackTrace();
		} catch (BadPaddingException e) {
			e.printStackTrace();
		}
	
		return null;
	}
	
	//encrypt method
	public byte[] cryter(byte[] Crypter) {
		
		
		try {
			cipher.init(Cipher.ENCRYPT_MODE, key);
		} catch (InvalidKeyException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
			
		
		short taille = (short)Crypter.length;
		short temp = (short) ( taille & 0x0007 );
		taille = (short) (taille + (8 - temp));

		
		
		donneeCrypter = new byte[taille];
		try {
			encodedParams = cipher.getParameters().getEncoded();
			cipher.doFinal(Crypter, (short)0, (short)Crypter.length, donneeCrypter, (short)0);
		} catch (ShortBufferException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalBlockSizeException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (BadPaddingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		

		return donneeCrypter;
	}
somebody can help me?
This post has been answered by safarmer on Mar 29 2011
Jump to Answer
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Dec 19 2011
Added on Mar 29 2011
8 comments
429 views