~ubuntu-branches/ubuntu/oneiric/monodevelop/oneiric

« back to all changes in this revision

Viewing changes to contrib/NSch/NSch.Jce/BlowfishCBC.cs

  • Committer: Bazaar Package Importer
  • Author(s): Jo Shields
  • Date: 2011-06-27 17:03:13 UTC
  • mto: (1.8.1 upstream)
  • mto: This revision was merged to the branch mainline in revision 54.
  • Revision ID: james.westby@ubuntu.com-20110627170313-6cvz3s19x6e9hqe9
ImportĀ upstreamĀ versionĀ 2.5.92+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
Copyright (c) 2006-2010 ymnk, JCraft,Inc. All rights reserved.
 
3
 
 
4
Redistribution and use in source and binary forms, with or without
 
5
modification, are permitted provided that the following conditions are met:
 
6
 
 
7
  1. Redistributions of source code must retain the above copyright notice,
 
8
     this list of conditions and the following disclaimer.
 
9
 
 
10
  2. Redistributions in binary form must reproduce the above copyright 
 
11
     notice, this list of conditions and the following disclaimer in 
 
12
     the documentation and/or other materials provided with the distribution.
 
13
 
 
14
  3. The names of the authors may not be used to endorse or promote products
 
15
     derived from this software without specific prior written permission.
 
16
 
 
17
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
 
18
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 
19
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
 
20
INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
 
21
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 
22
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
 
23
OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 
24
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 
25
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
 
26
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
27
 
 
28
This code is based on jsch (http://www.jcraft.com/jsch).
 
29
All credit should go to the authors of jsch.
 
30
*/
 
31
 
 
32
using System;
 
33
using NSch.Jce;
 
34
using Sharpen;
 
35
 
 
36
namespace NSch.Jce
 
37
{
 
38
        public class BlowfishCBC : NSch.Cipher
 
39
        {
 
40
                private const int ivsize = 8;
 
41
 
 
42
                private const int bsize = 16;
 
43
 
 
44
                private Sharpen.Cipher cipher;
 
45
 
 
46
                public override int GetIVSize()
 
47
                {
 
48
                        return ivsize;
 
49
                }
 
50
 
 
51
                public override int GetBlockSize()
 
52
                {
 
53
                        return bsize;
 
54
                }
 
55
 
 
56
                /// <exception cref="System.Exception"></exception>
 
57
                public override void Init(int mode, byte[] key, byte[] iv)
 
58
                {
 
59
                        string pad = "NoPadding";
 
60
                        //  if(padding) pad="PKCS5Padding";
 
61
                        byte[] tmp;
 
62
                        if (iv.Length > ivsize)
 
63
                        {
 
64
                                tmp = new byte[ivsize];
 
65
                                System.Array.Copy(iv, 0, tmp, 0, tmp.Length);
 
66
                                iv = tmp;
 
67
                        }
 
68
                        if (key.Length > bsize)
 
69
                        {
 
70
                                tmp = new byte[bsize];
 
71
                                System.Array.Copy(key, 0, tmp, 0, tmp.Length);
 
72
                                key = tmp;
 
73
                        }
 
74
                        try
 
75
                        {
 
76
                                SecretKeySpec skeySpec = new SecretKeySpec(key, "Blowfish");
 
77
                                cipher = Sharpen.Cipher.GetInstance("Blowfish/CBC/" + pad);
 
78
                                cipher.Init((mode == ENCRYPT_MODE ? Sharpen.Cipher.ENCRYPT_MODE : Sharpen.Cipher.
 
79
                                        DECRYPT_MODE), skeySpec, new IvParameterSpec(iv));
 
80
                        }
 
81
                        catch (Exception e)
 
82
                        {
 
83
                                throw;
 
84
                        }
 
85
                }
 
86
 
 
87
                /// <exception cref="System.Exception"></exception>
 
88
                public override void Update(byte[] foo, int s1, int len, byte[] bar, int s2)
 
89
                {
 
90
                        cipher.Update(foo, s1, len, bar, s2);
 
91
                }
 
92
 
 
93
                public override bool IsCBC()
 
94
                {
 
95
                        return true;
 
96
                }
 
97
        }
 
98
}